抑制“变量从未被分配” IntelliJ IDEA 中的警告
我们广泛使用反射来在代码中设置类字段值。这些字段是在代码中访问的,但除了通过反射之外,它们永远不会被分配。因此IDEA显示“从未分配”警告。如果我要求 IDEA 禁止检查,它会插入
@SuppressWarnings({"UnusedDeclaration"})
,但这也会禁用对该字段是否使用的检查,这是我们不希望的。
无论如何,是否可以仅禁用“未分配”检查并仅对特定字段保留“未使用”检查?
IDEA版本是10.5
We use reflection extensively to set class field values in our code. The fields are accessed in code but they are never assigned except via reflection. So IDEA displays "is never assigned" warning. If I ask IDEA to suppress the inspection, it inserts
@SuppressWarnings({"UnusedDeclaration"})
but this also disables the check of whether the field is used or not, which we do not want.
Is it anyhow possible to disable only "not assigned" check and leave "not used" check for specific fields only?
IDEA version is 10.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用注释将其标记为注入字段。 (类似于它对待
@EJB
的方式)。 IntelliJ 检查(至少在版本 10.5 中)允许您配置自己的注释以将字段标记为已注入。从菜单中选择分析、检查代码,然后转到未使用的声明检查,您可以配置注释。
You could use an annotation to mark it as an injected field. (similar to how it would treat
@EJB
). The IntelliJ inspections (at least with version 10.5) allow you to configure your own annotations to mark fields as being injected.Select Analyze, Inspect Code from the menu and then go to the unused declaration inspection and you can configure an annotation.
设置->编辑->检查->声明冗余->未使用的声明->入口点->注释
将打开一个新的弹出窗口,分为 2 个不同的区域:“如果注释为,则标记为入口点”和“如果注释为,则将字段标记为隐式写入”。 仅在后者中,单击“+”图标(添加注释类)并选择自动装配注释(或用于依赖项注入的任何注释)。
这将禁用警告“私有字段从未分配”,但不会禁用警告“私有字段从未使用”,以防您从未在类代码中使用该字段,这是理想的行为。
这适用于 IntelliJ IDEA 社区版 2018.3.3 和 2020.2
Settings -> Editor -> Inspections -> Declaration Redundancy -> Unused declaration -> Entry points -> Annotations
A new popup opens, split into 2 different areas: "Mark as entry point if annotated by" and "Mark field as implicitly written if annotated by". Only in the latter you click the "+" icon (Add Annotation Class) and select Autowired annotation (or whatever annotation you're using for dependency injection).
This will disable the warning "private field is never assigned" but will not disable the warning "private field is never used" in case you never use the field in the class code, which is the desirable behaviour.
This worked on IntelliJ IDEA Community Edition 2018.3.3 and 2020.2
要在检查代码后在 Android Studio 1.1.0 中配置注释,请右键单击未使用的声明 ->编辑设置->配置注释。
To configure annotations in Android Studio 1.1.0 after inspecting your code right click Unused declaration -> Edit Settings -> Configure annotations.
如果弹出操作提示窗口(在 Mac 上按 Alt + Enter),它应该建议您抑制“注入注释”的警告
If you popup the actions hint window (alt + Enter on Mac), it should suggest you to suppress warning for Inject annotation
不对,看来IDEA的检查没有那么细粒度。即使使用基于注释的依赖注入字段,对于使用
@Inject
注释的字段也可以抑制相同的警告。自动抑制“未使用”警告。我刚刚尝试针对该类运行 FindBugs-IDEA,并且没有引发任何警告或错误。
Nope, it seems that IDEA's inspection is not that fine grained. Even with annotation based dependency injected fields the same warning can be suppressed for fields annotated with
@Inject
. Automatically the warning "not used" is suppressed.I've just tried running FindBugs-IDEA against the class and no warnings or errors were raised.