Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document @SuppressWarnings("unused")?

I extracted a module from a monolithic java project. The module contains public constants, which are used only outside of that module, but all over the application. Now I need to ignore the unused warnings via @SuppressWarnings("unused"), because there are a lot of constants and if I don't ignore those, they hide other warnings.

I actually dislike this approach, but semantically those constants belong to the module and at the very least I want to document why I chose to suppress the warnings in the first place. The javadoc page does not provide any hint on how to do this.

I am thankful for any advice?

like image 212
tgr Avatar asked Jan 23 '18 11:01

tgr


People also ask

What does @SuppressWarnings unused mean?

The @SuppressWarnings annotation disables certain compiler warnings. In this case, the warning about deprecated code ( "deprecation" ) and unused local variables or unused private methods ( "unused" ).

What does @SuppressWarnings mean in Java?

Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.

What is the use of @SuppressWarnings unchecked?

@SuppressWarnings("unchecked") is used when Java generics just don't let you do what you want to, and thus, you need to explicitly specify to the compiler that whatever you are doing is legal and can be executed at the time of execution.

What is @SuppressWarnings Rawtypes?

Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that you believe the code is safe and won't cause unexpected exceptions. rawtypes warns that you are using a raw type instead of a parameterized type. It is like unchecked , but it is used on fields and variables.


1 Answers

As J.Bloch sugested in Effective Java (probably Item 27, Eliminate unchecked warnings), I just add simple non-javadoc comment on the line with this annotation, which explains why it is ok to suppress warnings there.

It doesn't seem to me, that it is interesting to user of your API what warnings are suppressed, so I wouldn't worry about javadoc and for developer actually reading the code the comment is enough.

So in your case I'd probably go like:

//Many of the constants are unused, but removing them would require figuring out their values
//and names some day
@SuppressWarnings("unused")  
public class Foo {
like image 193
Igand Avatar answered Oct 13 '22 18:10

Igand