Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telling IntelliJ IDEA which methods not to identify as unused

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

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.

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 @RequestMapping is doing). Understandably, IntelliJ has no idea what does my annotation stand for and and marks such a method as unused, adding unnecessary noise.

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.

Can anyone suggest any ideas how to solve this problem?

like image 918
mindas Avatar asked Mar 12 '11 16:03

mindas


People also ask

How do I delete unused methods in IntelliJ?

That would be helpful ! @Snicolas right-click on the inspection result (Declaration redundancy->Unused declaration) and choose "Safe delete". Or if you want button, there's the light-bulb in the left toolbar.

How do I delete unused variables in IntelliJ?

In IntelliJ, in a . java file, some unused code is greyed out indicating that the declared variable or function is never used. Unused imports are removed using Ctrl+Alt+O.

How check method calls in IntelliJ?

on the toolbar in the Find tool window or press Ctrl+Alt+Shift+F7 . While in the Find tool window, you can also use the Preview area to check the places where the usages were found, to see a call hierarchy for methods, data flow for fields, and so on.


1 Answers

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

It should be a quick fix all you have to do is hit <Alt>+<Enter> and select 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

like image 109
Peter Lawrey Avatar answered Oct 16 '22 12:10

Peter Lawrey