Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>I don't think it's possible to remove the compiler warnings</em></p> <p>The best case scenario is reducing all the errors into one like @tangens does.</p> <p>Found two forum threads that show unsuccessful answers and explains the why a bit more.</p> <ul> <li><a href="http://forums.sun.com/thread.jspa?threadID=5215278" rel="nofollow noreferrer">http://forums.sun.com/thread.jspa?threadID=5215278</a></li> <li><a href="http://forums.sun.com/thread.jspa?threadID=694354" rel="nofollow noreferrer">http://forums.sun.com/thread.jspa?threadID=694354</a></li> </ul> <p>So I put together a full example to demonstrate the issue as I see it.</p> <pre><code>import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; import java.util.List; public class Test { @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Detail { String example(); } public enum ExampleEnum { FOO_BAR, HELLO_WORLD } @Detail(example = "FOO_BAR") public ExampleEnum test; public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { populate(new Test()); } public static void populate(Object o) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException { final Field field = o.getClass().getField("test"); final Detail detail = field.getAnnotation(Detail.class); System.out.println("Annotation = " + detail); final String example = detail.example(); final Class&lt;?&gt; type = field.getType(); System.out.println("Field Class = " + type.getName()); if (List.class.isAssignableFrom(type)) { } else if (Enum.class.isAssignableFrom(type)) { Class&lt;? extends Enum&gt; enumType = type.asSubclass(Enum.class); // Enum is a raw type. References to generic type Enum&lt;E&gt; should be parameterized Enum val = Enum.valueOf(enumType, example); // 1) Enum is a raw type. References to generic type Enum&lt;E&gt; should be parameterized // 2) Type safety: Unchecked invocation valueOf(Class&lt;capture#7-of ? extends Enum&gt;, String) of the generic // method valueOf(Class&lt;T&gt;, String) of type Enum field.set(o, val); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload