Warnings getting past @SuppressWarnings

Raluca Sauciuc ralucas at google.com
Wed Aug 28 09:08:09 PDT 2013


Hi compiler-dev,

We ran into an unexpected behavior with javac7 and unchecked warnings. Code
that compiled without any warnings, by using @SuppressWarnings in a few
places, has started to display them again.

Here's the simplest example I could find:

public class Test {

 @SuppressWarnings("unchecked")
 public static <T extends Bar> Foo<T> of(Class<T> type) {
   return (Foo<T>) cache.get(type);
 }

 private static final Cache<Class<? extends Bar>,Foo<?>> cache = null;

 interface Cache<K,V> {
   V get(K key);
 }

 interface Foo<T extends Bar> {
   T newFoo(String name);
 }

 interface Bar { }
}

$ javac -Xlint:unchecked Test.java
Test.java:5: warning: [unchecked] unchecked cast
   return (Foo<T>) cache.get(type);
                            ^
 required: Foo<T>
 found:    Foo<CAP#1>
 where T is a type-variable:
   T extends Bar declared in method <T>of(Class<T>)
 where CAP#1 is a fresh type-variable:
   CAP#1 extends Bar from capture of ?
1 warning

The warning is legitimate, as Cache<Class<? extends Bar>,Foo<?>> loses the
constraint between key and value type, but we expect @SuppressWarnings to
silence it. Note that javac6 doesn't emit the warning.

Is this expected behavior?

Thanks,
Raluca
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20130828/b8537244/attachment.html 


More information about the compiler-dev mailing list