java 告诉 IntelliJ IDEA 哪些方法不被识别为未使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5283972/
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-10-30 10:24:41  来源:igfitidea点击:

Telling IntelliJ IDEA which methods not to identify as unused

javaspringannotationsintellij-ideaaop

提问by mindas

IntelliJ IDEA has a handy feature to detect unused methods and show them in grey, hinting a potential warning for dead code.

IntelliJ IDEA 有一个方便的功能来检测未使用的方法并将它们显示为灰色,暗示潜在的死代码警告。

Some methods, however, are not executed directly but via reflection. A good example would be @RequestMapping-annotated methods which are executed by Spring. IntelliJ has decent Spring integration hence it detects this annotation and does not mark such a method as unused.

但是,有些方法不是直接执行的,而是通过反射执行的。一个很好的例子@RequestMapping是由 Spring 执行的带注释的方法。IntelliJ 具有不错的 Spring 集成,因此它会检测此注释并且不会将此类方法标记为未使用。

I have a tiny AJAX framework where I use my own annotation to point which method to execute based on certain HTTP request properties (very similar to what @RequestMappingis doing). Understandably, IntelliJ has no idea what does my annotation stand for and and marks such a method as unused, adding unnecessary noise.

我有一个很小的 ​​AJAX 框架,我在其中使用自己的注释根据某些 HTTP 请求属性来指出要执行的方法(与@RequestMapping正在执行的操作非常相似)。可以理解的是,IntelliJ 不知道我的注释代表什么,并将这种方法标记为未使用,从而增加了不必要的噪音。

I was thinking of:

我在想:

  • annotating my annotation with another annotation, but are there any standard ones that would do the job without any extra effort?
  • finding a particular setting in IntelliJ to identify custom annotation for marking methods as used, but this would require other team members to do the same, basically a pain.
  • 用另一个注释来注释我的注释,但是否有任何标准的注释可以在不付出任何额外努力的情况下完成这项工作?
  • 在 IntelliJ 中找到一个特定的设置来识别用于标记方法的自定义注释,但这需要其他团队成员做同样的事情,基本上很痛苦。

Can anyone suggest any ideas how to solve this problem?

任何人都可以提出任何想法来解决这个问题吗?

回答by Peter Lawrey

You can tell IntelliJ to not to warn about used for any method/field annotated with the annotation the "unused" method has.

您可以告诉 IntelliJ 不要警告使用“未使用”方法所具有的注释的任何方法/字段。

It should be a quick fix all you have to do is hit <Alt>+<Enter> and select Suppress for methods annotated by ...

它应该是一个快速修复您所要做的就是点击 <Alt>+<Enter> 并选择 Suppress for methods annotated by ...

You don't need to add anything to you code and you only have to do this once per annotation.

您不需要向代码中添加任何内容,并且每个注释只需执行一次。

enter image description here

在此处输入图片说明

回答by Vance Maverick

@SuppressWarnings("unused")should work.

@SuppressWarnings("unused")应该管用。

回答by Zarathustra

@Peter Lawrey s solution did not help in my version of Intellij (14.1.1).

@Peter Lawrey 的解决方案对我的 Intellij (14.1.1) 版本没有帮助。

I used the hard way around:Settings-Editor->Inspections->Unused declarion Now there is an Options point, scroll down to Configure annotations...and you can add your annotation there.

我使用了困难的方法:设置-编辑器->检查->未使用的声明现在有一个选项点,向下滚动到Configure annotations...,您可以在那里添加注释。

回答by Andrey

In the "Settings" you can "uncheck" Settings - Inspections - Declaration redundancy - Unused Declaration code inspection.

在“设置”中,您可以“取消选中”设置 - 检查 - 声明冗余 - 未使用的声明代码检查。