java IntelliJ IDEA 中的“Variable is never assignment”警告只能“部分”抑制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41124617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:42:10  来源:igfitidea点击:

“Variable is never assigned” warning in IntelliJ IDEA can be suppressed only "partially"

javaintellij-ideacode-inspectionunused-variables

提问by rychu

Java EE + IntelliJ Idea 2016.3: I've written a class and declared a private field with a @Injectannotation.

Java EE + IntelliJ Idea 2016.3:我编写了一个类并声明了一个带有@Inject注释的私有字段。

I have successfully got rid of the "unused declaration" notification from the "inspection results" window by adding javax.inject.Injectto settings -> editor -> inspections -> Java -> declaration redundancy -> unused declarations -> entry points -> annotations -> mark field as implicitly written when annotated by(based on this post).

通过添加javax.inject.Inject设置 -> 编辑器 -> 检查 -> Java -> 声明冗余 -> 未使用的声明 -> 入口点 -> 注释 -> 标记,我已成功摆脱了“检查结果”窗口中的“未使用声明”通知(基于此帖子注释时隐式写入的字段

Unfortunately the field is still underlined and a yellow marker is present on the scroll bar. Do I configure this separately or am I doing something wrong?

不幸的是,该字段仍带有下划线,并且滚动条上存在黄色标记。我是单独配置还是我做错了什么?

回答by MarcG

There are two separate warnings. One says the field was never assigneda value, which is the one you have disabled. The other says the field was never used, which is the warning you are getting.

有两个单独的警告。一个说该字段从未被分配过一个值,这是您已禁用的值。另一个说从未使用过该字段,这是您收到的警告。

Add Injectto settings -> editor -> inspections -> java -> declaration redundancy -> unused declarations -> entry points -> annotations -> Mark as entry point if annotated by

添加Inject设置 -> 编辑器 -> 检查 -> java -> 声明冗余 -> 未使用的声明 -> 入口点 -> 注释 -> 标记为入口点,如果被注释

回答by Ronny Shibley

Highlight the field, type Alt + Enter "Windows" or ?? "Mac" or Just click on the Light Bulb. Select the "Suppress for fields annotated by {annotation name}.

突出显示该字段,键入 Alt + Enter "Windows" 或 ?? “Mac”或只需单击灯泡。选择“抑制由 {annotation name} 注释的字段。

回答by rychu

It seems that it was a bug in IDEA. I've just updated to IDEA 2016.3.1and now @Injectand @Resourceannotations put in the "entry points" section do not work - they have to be put in the "fields" window (although when they are in "entry points" section I can still observe the "old" behavior i.e. annotated fields are not detected by code inspection yet they are underlined in the editor and yellow marker is visible).

看来这是IDEA中的一个错误。我刚刚更新到IDEA 2016年3月1日,现在@Inject@Resource注释放在“切入点”一节不工作-他们必须被放在了“田”窗口(尽管当他们在“切入点”一节,我可以仍然观察“旧”行为,即代码检查未检测到注释字段,但它们在编辑器中带有下划线并且黄色标记可见)。