英文:
A Checkstyle Rule for OpenJDK's Local Variable Type Inference Style Guidelines
问题
# 背景:
OpenJDK发布了一个[样式指南][1],用于局部变量类型推断。
我在寻找一个内置的checkstyle规则来解决这个问题,但没有找到一个。
我想要的内容非常简单,但我对编写自定义模块不是很熟悉。
# 示例:
### 允许:
- 实例创建,清楚地知道正在创建什么
```java
var something = new Anything(); // 可以
- 这是一个文字,很明显知道它的类型
var something = "literal"; // 可以
违规:
- 即使命名得很好,也不知道类型是什么
var something = anything.doAnything(); // 违规
- 在这里使用var可以提高可读性
Anything<Foo<Bar>> something = new Anything<>(); // 违规
- 不是很重要,但只是为了保持一致性
String something = "literal"; // 违规
问题:
是否有现有的规则可以扩展/调整,以满足上述要求?我应该尝试编写自己的规则吗?我想正则表达式可以处理一些情况,但不能处理所有情况。
<details>
<summary>英文:</summary>
# Background:
OpenJDK have released a [style guide][1] for local variable type inference.
I was looking for a checkstyle built in rule that addresses it but didn't found one.
What I'm trying to get is pretty simple but I'm not that familiar with writing custom Modules.
# Example:
### Allow:
- instance creation, it's clear what's being created
```java
var something = new Anything(); // OK
- it's a literal, it's obvious what type it has
var something = "literal"; // OK
Violation:
- even with good naming, no clue what's the type
var something = anything.doAnything(); // Violation
- it improves readability to use var here
Anything<Foo<Bar>> something = new Anything<>(); // Violation
- not a big deal but just for consistency
String something = "literal"; // Violation
Question:
Is there any existing rule I can extend / tweak to get the above? Should I try writing my own rule? I assume RegExp can handle some cases but not all.
答案1
得分: 1
你可以在这里找到它:https://checkstyle.sourceforge.io/config_coding.html#MatchXpath
注意:MatchXpath 是从 Checkstyle 8.39 版本开始支持的。
英文:
find it here : https://checkstyle.sourceforge.io/config_coding.html#MatchXpath
Note : MatchXpath is Since Checkstyle 8.39
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论