Telling IntelliJ IDEA which methods not to identify as unused - java

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?

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.

#SuppressWarnings("unused") should work.

#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.

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

Related

Java annotation that sets target as "used" [duplicate]

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?
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.
#SuppressWarnings("unused") should work.
#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.
In the "Settings" you can "uncheck" Settings - Inspections - Declaration redundancy - Unused Declaration code inspection.

Sonarlint [squid:S1186]: Method should not be empty on constructor in Spring Tool Suite

I am using Sonarlint V3.5.0 in Spring Tool Suite. I am receiving the squid:S1186 warning when I have the default constructor inside the code like this:
public class TestClass{
public TestClass() {}
}
It is a little bit annoying to have this warning all the time. As I have found, SonarSource have solved this as a bug in version 3.5, But the newest Sonarlint still gives me this warning.
How can I solve this using Sonarlint? Thanks.
You can not remove the rule check using SonarLint by itself. You can ignore the offending rule using a SonarQube server and connect your SonarLint to the SQ server in order to ignore the rule, as suggested here.
As stated in the OP, the SonarSource team is having issues with the use-case described in the question.
Please consider the following BEFORE thinking of ignoring the rule:
The squid:S1186 issue is a good practice concept - having an empty scope in general in our code, not just in a constructor, is a code anti-pattern.
In your case, the default constructor has been explicitly implemented, which is apparently required by the Sprint Tool Suite you are using (this sort of use case is discussed nicely here).
Considering the fact that the empty scope is indeed an anti-pattern, I would suggest adding a comment inside the constructor's empty scope explaining that the explicit public default constructor is required due to the usage of STS.
Doing this will obviously solve the issue raised by SonarLint.
In case you have many instances of auto-generated empty default constructors - you should be able to use your IDEs find/replace solution with a regexp in order to replace the empty scopes with scopes containing a comment explaining why this is so.

Letting a single Java annotation implicitly set multiple annotations

I have written a Java library that defines and uses a custom annotation to find methods that are then called via reflection.
See this example
#YauaaField("DeviceClass")
public void setDeviceClass(TestRecord record, String value) {
record.deviceClass = value;
}
So IDEs like IntelliJ and probably other code analysis tools will report most of the functions annotated this way as "Unused".
What I would like is an automated way to say that anything that has been annotated with #YauaaField is automatically also annotated with #SuppressWarnings("unused").
I've done quite a bit of googling, read through several online manuals, tutorials and java documentation. Yet I have not yet been able to find how to do that. The annotations do not seem to support 'inheritance' of any kind.
So is what I want even possible?
If not then what other options do I have?
So far I have only found these two ways to suppress these needless warnings:
In IntelliJ I found the manual option to Suppress this warning on all methods annotated with YauaaField. But that is a manual option.
Manually set the #SupressWarnings("unused") on all of those methods/classes.
Is there a better way?
Not entirely automated, but a nice workaround. In IntelliJ you can set up your own Live Template to suggest an autocomplete when typing the annotation:

Using an #interface to suppress certain warnings in IntelliJ

I'm currently working on a game API that is used as a Core dependency in a bunch of my other projects. The issue I'm having is IntelliJ is giving me all sorts warnings because the API's methods/classes/variables need to be public, have a different parameter value, are only used externally, etc. I would like to simply annotate these fields with #API instead of going through and adding #SupressWarning({(10 different checks)}) every time I add something to the API. I don't want to disable these checks entirely as they can be a helpful reminder, but I can't seem to figure out how to suppress a warning type for an #interface. There is an option for this with the unused warning where you "suppress for methods annotated by ...", but I can't find anything for the other warnings that pop up as a result of the implementation being situationally incorrect because the IDE can't see how it's being used in the other projects.
I tried annotating my API class with the #SuppressWarnings hoping that the annotated members would inherit it
#SuppressWarnings({"WeakerAccess", "unused", "SameParameterValue", ...})
public #interface API{}
but that didn't work, and I've looked for ways to add my other projects as "downstream projects" but I haven't found anything yet. Is this even possible or is it just a limitation of IntelliJ?
Thanks in advance!

Annotation for framework-used methods

I am currently a bit annoyed with my IDE, which is complaining about unused methods. These methods are used predominantly by Camel or Spring and not all covered by Unit Tests. I would like to annotate those methods to let my IDE know that they are unused for a reason.
Which annotation is most suitable for this purpose?
Have you tried #SuppressWarnings("unused") ?

Categories