67

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?

mindas
  • 26,463
  • 15
  • 97
  • 154

5 Answers5

94

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    unfortunately I do not have this option, my annotation is `@WebMethod` and I am using Intellij 14.1.1 . Any idea why I do not have this option available? – Zarathustra Apr 13 '15 at 07:46
  • 2
    This works with recent versions like a charm. IntelliJ really comes with some magic. – Salim May 20 '16 at 08:01
  • 4
    I've accidentally suppressed this for "fields annotated with xx". To undo this operation find `` in the `.idea/misc.xml` file and adapt it (don't forget to update the `list` attribute, when you change the count of items) – TmTron Sep 20 '16 at 08:00
  • Maybe this action is hidden at the "More actions..." link at the bottom right corner in a warning's hint window – Nashev Apr 30 '21 at 20:26
49

@SuppressWarnings("unused") should work.

Vance Maverick
  • 812
  • 6
  • 4
  • Thanks for the answer. Ideally I would want my custom annotation to be able to handle this automagically so I don't have to add @SuppressWarnings("unused") on each method. Or are you suggesting I should annotate my annotation (rather than method) itself? – mindas Mar 12 '11 at 22:10
  • No, I wasn't suggesting this. I don't know how to "tell IDEA which methods not to identify as unused" except by using the standard annotation. Peter's suggestion is nice, but requires just as many annotaions. – Vance Maverick Mar 13 '11 at 03:44
  • I think Peter's @UsedViaReflection is just a pseudo code, it can be any annotation really. It isn't ideal (as I was mentioning in my question), but I guess there are no other alternatives. – mindas Mar 13 '11 at 12:14
7

@Peter Lawrey s solution did not help in my version of 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.

Zarathustra
  • 2,853
  • 4
  • 33
  • 62
0

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

Andrew
  • 36,676
  • 11
  • 141
  • 113
0

In case anyone is finding trouble with this at the moment, here is how you can configure it in IntelliJ IDEA's current version: Route viewn

File > Settings > Editor > Inspections > Unused declaration > Entry Points > Annotations... > Mark as entry point if annotated by

Really useful for @Beans!