Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 6: Unsupported @SuppressWarnings("rawtypes") warning

I moved to a new machine which has the latest Sun's Java compiler and noticed some warnings in the existing Java 6 code. The Eclipse IDE, suggested that I annotate the assignment with:

@SuppressWarnings("rawtypes")

For example:

class Foo<T> {
...
}
...
@SuppressWarnings("rawtypes")
Foo foo = new Foo();

When I moved back to the machine with the older compiler (JDK 1.6.0_20), I have noticed that this older compiler now warns about the suppression of "rawtypes" warnings, claiming that this suppression is unsupported and proposing to replace it with @SuppressWarnings("unchecked"). Also, there were some places which the newest compiler, by default, made me to put both "unchecked" and "rawtypes" - compiling that code with the older compiler reproduces the same warning.

How can I enforce backward/forward compatibility between the two, so that neither compiler produces warnings?

like image 587
eold Avatar asked Sep 05 '10 16:09

eold


2 Answers

You can use the @SuppressWarnings("unchecked") which is supported by both the eclipse compiler and javac.

But remember the @SuppressWarnings annotation is used by your compiler which can have its own values. The JLS only forces the compiler to understand the values "unchecked" and "deprecated" (for now).

Compiler vendors should document the warning names they support in conjunction with this annotation type. They are encouraged to cooperate to ensure that the same names work across multiple compilers.

If you use Helios, you will need to set a specific option to allow @SuppressWarnings("unchecked") instead of @SuppressWarnings("rawtypes"),

In case it is not possible to update the code with the new token, the suppressRawWhenUnchecked=true system property can be set when starting Eclipse.


Resources :

  • JLS - @SuppressWarnings()
  • Eclipse JDT (Java Compiler, New "rawtypes" token for @SuppressWarnings annotation)

EDIT: Here is the now unavailable knol article that was used as a reference, originally written by Alex Miller.

@SuppressWarnings Annotation in Java

Standard annotation for suppressing various warnings

The SuppressWarnings annotation was added as a standard annotation in Java SE 5.

Definition

The @SuppressWarnings annotation is defined in the Java Language Specification section 9.6.1.5. This section states:

The annotation type SuppressWarnings supports programmer control over warnings otherwise issued by the Java compiler. It contains a single element that is an array of String. If a program declaration is annotated with the annotation @SuppressWarnings(value = {S1, ... , Sk}), then a Java compiler must not report any warning identified by one of S1, ... , Sk if that warning would have been generated as a result of the annotated declaration or any of its parts.

Unchecked warnings are identified by the string "unchecked".

The subsequent section on @Deprecation also mentions that these warnings can be suppressed with @SuppressWarnings("deprecation").

Valid Warning Types

The only two warning strings that are mentioned in the specification itself are "unchecked" and "deprecation". However, the Sun JDK uses a larger set of strings in the compiler. You can determine the current set by executing:

javac -X

which will show you (among other things) the valid settings for -Xlint.

For example, Sun JDK 1.5 shows:

  • all - suppress all warnings from this code
  • deprecation - suppress warnings from using deprecated code
  • unchecked - suppress warnings from an unchecked call or an unchecked cast
  • fallthrough - suppress warnings if a switch falls through without finding a valid case (and no default)
  • path -
  • serial - suppress warnings if a Serializable class does not define a serialVersionUID
  • finally - suppress warnings from return within a finally (which will ignore return with the try)

And Sun JDK 1.6 adds:

  • cast
  • divzero - suppress warnings if integer divide by zero is detected
  • empty
  • overrides
  • none

IDEs and static analysis tools typically support a large number of other possible values for @SuppressWarnings. These values correspond to specific static analysis checks performed by the IDE.

Eclipse

The Eclipse warning values for Eclipse 3.3 are documented in the JDT docs.

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

IntelliJ

NetBeans

Examples

An example of specifying a single warning:

@SuppressWarnings("unchecked")
public void methodWithScaryWarnings() {
    List rawList = new ArrayList();
    List<String> stringList = (List<String>)rawList;
}

An example of using two warnings:

@SuppressWarnings({"unchecked","deprecation"})
public void methodWithScaryWarnings() {
    callDeprecatedMethod();
}
like image 69
Colin Hebert Avatar answered Nov 02 '22 18:11

Colin Hebert


Note that Eclipse 3.5 doesnt understand rawtypes and flags a warning to switch to unchecked. It is frustrating that Eclipse came up with rawtypes annotation which causes more problems than solving. They should have just stuck with the standard one.

like image 42
inder Avatar answered Nov 02 '22 19:11

inder