IntelliJ suppress unused warning for API methods

2020-07-02 09:28发布

问题:

I recently switched to IntelliJ IDEA from Eclipse and I really like the inspectors and find them marking potential errors with warnings for me really useful. I ran into a problem with them that I am unable to solve:

I have some Java projects that are used as APIs in other project, therefore it contains unused methods, which are marked as such: Unused warning

How can i suppress this for the API methods? Is there an alternative to @SuppressWarnings("unused"), since this also suppresses warnings about unused warnings inside the method and doesn't make it clear to the reader that this method is designed for API use instead of just not being used in the current project?

回答1:

@Sebastian's suggestion to have your class implement an interface is probably the best way to solve this issue from a good design standpoint. But when that is impractical, there is an alternative...

The "Unused declaration" inspection allows you to configure "entry points" (i.e. a public external API) to ignore. As part of that you can configure "annotations". Anything annotated with the configured annotation is seen as an entry point.

Just configure the inspection to use an annotation that you annotate your public API methods with, either one from a library -- such as @API Guardian (used by some open source projects such as JUnit 5) -- or one you create. Using a library will of course make things easier and consistent across projects. In the below example I used a @PublicApi annotation I created. Notice the method is not highlighted as unused. But the foo String still is highlighted as unused, which is what you want:

As an alternative to opening the Settings dialog, and to limit the impact on your programming flow, you can also use a Quick Fix Intention to add the desired annotation to the "Unused Declaration" settings. After annotating with the desired annotation, place your cursor on the highlighted unused method (or class) name, and invoke the "intention actions and quick-fixes" popup via Alt+Enter or (or by clicking on the light bulb icon ) and select "Suppress for methods annotated by '{annotation}':



回答2:

Write an interface for your class. Methods that implement an interface method are not marked as unused. instead the unused methods from the interface are marked as unused but here you can safely use @SuppressWarnings("unused") because you do not have a method body. You could even write @SuppressWarnings("unused") above the whole interface.



回答3:

In short, no and this isn't really anything to do with IntelliJ, Javac the Java compiler will produce these if you ask it to. If your method needs this annotation, then that will be for the entire method. However if you just wish a field to be annotated, then that is possible:

@SuppressWarnings("unused")
int iii = 0;

In summary, the method annotation will cover the whole method, placing it per field or instruction will cover that single line.



回答4:

intellij can do this for you.Just hit Alt+Enter on the occurance that gives you the warning. Some suggestions will pop-up one of them being remove field. there will be an arrow field at the end of the suggestion. Using your arrow keys navigate to the option and use the right arrow to bring up another menu. One of the options will be suppress this for method and suppress this for statement etc. Suppress for statement will cause this :

//noinspection unused
int iii = 0;

However I have to urge you to heed to the warnings being provided by Intellij and not blindly suppress them.