What Is the List of Valid @Suppresswarnings Warning Names in Java

What is the list of valid @SuppressWarnings warning names in Java?

It depends on your IDE or compiler.

Here is a list for Eclipse Galileo:

  • all to suppress all warnings
  • boxing to suppress warnings relative to boxing/unboxing operations
  • cast to suppress warnings relative to cast operations
  • dep-ann to suppress warnings relative to deprecated annotation
  • deprecation to suppress warnings relative to deprecation
  • fallthrough to suppress warnings relative to missing breaks in switch
    statements
  • finally to suppress warnings relative to finally block that don’t
    return
  • hiding to suppress warnings relative to locals that hide variable
  • incomplete-switch to suppress warnings relative to missing entries
    in a switch statement (enum case)
  • nls to suppress warnings relative to non-nls string literals
  • null to suppress warnings relative to null analysis
  • restriction to suppress warnings relative to usage of discouraged or
    forbidden references
  • serial to suppress warnings relative to missing serialVersionUID
    field for a serializable class
  • static-access to suppress warnings relative to incorrect static
    access
  • synthetic-access to suppress warnings relative to unoptimized
    access from inner classes
  • unchecked to suppress warnings relative to unchecked operations
  • unqualified-field-access to suppress warnings relative to field
    access unqualified
  • unused to suppress warnings relative to unused code

List for Indigo adds:

  • javadoc to suppress warnings relative to javadoc warnings
  • rawtypes to suppress warnings relative to usage of raw types
  • static-method to suppress warnings relative to methods that could be declared as static
  • super to suppress warnings relative to overriding a method without super invocations

List for Juno adds:

  • resource to suppress warnings relative to usage of resources of type Closeable
  • sync-override to suppress warnings because of missing synchronize when overriding a synchronized method

Kepler and Luna use the same token list as Juno (list).

Others will be similar but vary.

all SuppressWarnings values?

Here; http://www.breakitdownblog.com/supported-values-for-suppresswarnings/. Note these are for main Java libraries; other APIs might have their own.

  • all to suppress all warnings
  • boxing to suppress warnings relative to boxing/unboxing operations
  • cast to suppress warnings relative to cast operations
  • dep-ann to suppress warnings relative to deprecated annotation
  • deprecation to suppress warnings relative to deprecation
  • fallthrough to suppress warnings relative to missing breaks in switch statements
  • finally to suppress warnings relative to finally block that don’t return
  • hiding to suppress warnings relative to locals that hide variable
  • incomplete-switch to suppress warnings relative to missing entries in a switch statement (enum case)
  • nls to suppress warnings relative to non-nls string literals
  • null to suppress warnings relative to null analysis
  • rawtypes to suppress warnings relative to un-specific types when using generics on class params
  • restriction to suppress warnings relative to usage of discouraged or forbidden references
  • serial to suppress warnings relative to missing serialVersionUID field for a serializable class
  • static-access to suppress warnings relative to incorrect static access
  • synthetic-access to suppress warnings relative to unoptimized access from inner classes
  • unchecked to suppress warnings relative to unchecked operations
  • unqualified-field-access to suppress warnings relative to field access unqualified
  • unused to suppress warnings relative to unused code
  • varargs to suppress warnings about unsafe usages of variable arguments (varargs) methods, in particular, those that contain non-reifiable arguments.

How to get the @SuppressWarnings warning name for an IntelliJ warning?

By putting your cursor on the warning and pressing Alt+Enter, it should open up a menu. In this menu, there should be an option to remove the field. Navigate to this option and press . This opens up a sub-menu which contains the options to suppress the warning. By doing so, IntelliJ will generate the appropriate annotation.

In your case, the annotation should probably be along the lines of @SuppressWarnings("unused") or @SuppressWarnings("UnusedAssignment").

Java: complete list of @SuppressWarnings(...) parameters (in Netbeans)?

After a brief look at the NB-sourcecode, I found these in some of the java.hint -classes:

@Hint(category="bitwise_operations", suppressWarnings="IncompatibleBitwiseMaskOperation")
@Hint(category="initialization", suppressWarnings="LeakingThisInConstructor")
@Hint(category="logging", suppressWarnings={"NonConstantLogger"}) //NOI18N
@Hint(category="logging", suppressWarnings={"ClassWithMultipleLoggers"}) //NOI18N
@Hint(category="logging", suppressWarnings={"ClassWithoutLogger"}, enabled=false) //NOI18N
@Hint(category="code_maturity", suppressWarnings="UseOfObsoleteCollectionType")
@Hint(category="initialization", suppressWarnings="OverridableMethodCallInConstructor")
@Hint(category="bitwise_operations", suppressWarnings="PointlessBitwiseExpression")
@Hint(category="code_maturity", suppressWarnings="CallToThreadDumpStack")
@Hint(category="bitwise_operations", suppressWarnings="ShiftOutOfRange")
@Hint(category="initialization", suppressWarnings="StaticNonFinalUsedInInitialization")
@Hint(category="code_maturity", enabled = false, suppressWarnings="UseOfSystemOutOrSystemErr")
@Hint(category="code_maturity", suppressWarnings="CallToPrintStackTrace")

Apparently, not all IDE-hints that are displayed as warnings are made suppressable...
Don't know why though, 'cause the AbstractHint class wich many of them extends easily provides this ability...
These are just the suppress-names though, so to find the mapping to the names of the warnings they represent, a deeper dig in the source is needed.

SuppressWarnings annotation when assigning to instance variable

You expected to suppress warning when there's an issue, the issue is inside doSomething in line

this.map = Library.getMap();

You can't suppress the assignment itself, so you need to go to the outer scope which is the method or the class

As a matter of style, programmers should always use this annotation on the most deeply nested element where it is effective. If you want to suppress a warning in a particular method, you should annotate that method rather than its class.

If you assignment would be in initialization, you could suppress it:

@SuppressWarnings("unchecked" )
private Map<String, String> map = Library.getMap();

How to @SuppressWarnings: Resource leak: 'client' is never closed

Adding the @SuppressWarnings({ "resource" }) should remove the warning for a potential resource leak. Adding a list for other ones, it's for Eclipse but they should be fairly similar for reference.

What is the list of valid @SuppressWarnings warning names in Java?

How to avoid using @SuppressWarnings for casting a List

There is no way to do exactly this without a warning. A cast to a generic type simply cannot be check during runtime, and the compiler will inform you about that. I think that it is often totally legitimate to suppress that warning, so I don't think you really need another solution.

But if you can, follow @KarolDowbecki's suggestion, I think that is the best solution.

If you want to avoid SuppressWarning anyway, the following are some work-arounds:

Perform the cast when when you are using the CaseData elements, instead of casting the whole list:

List<?> cases = (List<?>) model.get("cases");

// No warning here, since we're casting the element instead of the list
CaseData data = (CaseData) cases.get(0);

Copy the cases list and check all elements before hand:

// No warning here, since we're casting to List<?>
List<CaseData> cases = ((List<?>) model.get("cases")).stream()
// No warning here, since we're casting the elements instead of the list
.map(d -> (CaseData) d).collect(toList());

What is the purpose of @SuppressWarnings(hiding) in eclipse?

From xyzws,

A class can declare a variable with the same name as an inherited variable from its parent class, thus "hiding" or shadowing the inherited version. (This is like overriding, but for variables.)

So hiding basically means that you've created a variable with the same name as a variable from an inherited scope, and the warning is just letting you know that you've done it (in case you needed access to the inherited variable as well as the local variable).

An example is:

public class Base {
public String name = "Base";
public String getName() { return name; }
}

public class Sub extends Base {
public String name = "Sub";
public String getName() { return name; }
}

In this example, Sub hides the value of name given by Base with it's own value - "Sub". Eclipse will warn you - just in case you needed the original value of the variable name - "Base".



Related Topics



Leave a reply



Submit