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

53,721

Solution 1

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 :


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();
}

Solution 2

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.

Share:
53,721

Related videos on Youtube

eold
Author by

eold

Updated on June 14, 2020

Comments

  • eold
    eold almost 4 years

    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?

    • musiKk
      musiKk over 13 years
      Which version of Eclipse suggests rawtypes? I've never seen that. As far as I remember Eclipse always suggested unchecked when I encountered this case.
  • whiskeysierra
    whiskeysierra over 13 years
    The eclipse compiler in helios rejects @SuppressWarnings("unchecked") and suggests what leden said.
  • Colin Hebert
    Colin Hebert over 13 years
    @Willi, You're right, I updated my post with more informations for helios.