Reputation: 8570
I'm getting a compiler warning for the @SuppressWarnings
annotation in eclipse for the code:
@Override
public boolean doSomething(@SuppressWarnings("unused") String whatever) throws AnException {
throw new AnException("I'm still in bed and can't do anything until I've had a shower!");
}
It looks like a yellow squiggle under the word "unused" and on mouse hover I get the tooltip Unnecessary @SuppressWarnings("unused")
.
I think another developer is being prompted to put in these annotations by eclipse and I'm basically being prompted to take them out. How can I configure eclipse to prompt me to put the @SuppressWarnings
annotation in instead of it complaining about it?
If anyone would like to comment on best practice here then that would also be most welcome.
Upvotes: 71
Views: 121883
Reputation: 193
While I agree with what you're saying, there are specific situations where there is no alternative but to use this method. As another colleague mentioned, there may be functions used indirectly within a project that do not use this method, and removing it without knowledge of its purpose could cause issues.
An example of this could be validations done using the @AssertTrue
annotation. The following example shows a validation added to check the possible values for the Status field, but this method is used by Spring and not called from any other part of the project. In this scenario, I see no harm in using the @SuppressWarnings("unused")
annotation.
@AssertTrue
@SuppressWarnings("unused")
private boolean isStatusValid() {
if (this.status == null) {
return true;
}
EnumSet<Status> acceptedStatuses = EnumSet.of(Status.A, Status.P);
return EnumValidation.anyOf(status, acceptedStatuses);
}
Upvotes: 0
Reputation: 508
In my code there's no inheritance defining the 3 methods with @SuppressWarnings("unused")
This code gives 'Unnecessary @SuppressWarnings("unused")' in Eclipse Juno (latest version), but if I remove the @SuppressWarnings("unused"), I get "Constructor/Method is never used" warnings in IntelliJ IDEA 11.1.3
The methods aren't directly used in the project, only by 3rd party products Jackson, JAXB & GSON, so IntelliJ is right, I would say ...
public class EmailUnsendable extends SkjemaError {
private NestedCommand command; // Can't be Command (interface) because of GSON!
@SuppressWarnings("unused") // Used by Jackson/JAXB/GSON
public EmailUnsendable() {
}
public EmailUnsendable(String referenceNumber, String stackTrace, NestedCommand command) {
super(referenceNumber, stackTrace);
this.command = command;
}
@SuppressWarnings("unused") // Used by Jackson/JAXB/GSON
public NestedCommand getCommand() {
return command;
}
@SuppressWarnings("unused") // Used by Jackson/JAXB/GSON
public void setCommand(NestedCommand command) {
this.command = command;
}
}
I believe this is an error in Eclipse.
Upvotes: 6
Reputation: 2156
Alternatively, if you think it's more correct to delete the SuppressWarnings annotation:
Window -> Preferences -> Java -> Compiler -> Errors/Warnings -> Unnecessary code -> Value of parameter is not used
and select Ignore in overriding and implementing methods
Upvotes: 5
Reputation: 236004
In the code in your question, the @SuppressWarnings("unused")
annotation is unnecessary because the method is either overriding another method from a superclass or implementing an interface. Even if you don't actually use the whatever
parameter it's mandatory to declare it, otherwise the @Override
annotation will produce an error (you'd be changing the signature of the overridden method if you removed the parameter.)
In some older versions of Eclipse the code as shown would not cause a warning, but in more recent releases it does. I believe it's a valid warning, and I'd rather remove the @SuppressWarnings("unused")
in this case.
Upvotes: 71
Reputation: 420951
Go to
Window → Preferences → Java → Compiler → Errors/Warnings → Annotations.
And select Ignore for Unused '@SuppressWarnings` token.
Upvotes: 26