Note that there are some explanatory texts on larger screens.

plurals
  1. POjava: reflection to obtain an Enum
    primarykey
    data
    text
    <p>This is similar but not quite the same as <a href="https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection">Java: instantiating an enum using reflection</a></p> <p>I have a <code>Map&lt;Enum&lt;?&gt;, FooHandler&gt;</code> that I want to use to map <code>Enum</code>s (I don't care which type or even if they are the same type, just as long as they are enum constants) to my <code>FooHandler</code> class.</p> <p>I would like to populate this map using a text file that I read. I can get it to work, but I have two warnings I would like to get around:</p> <pre><code>static private &lt;E extends Enum&lt;E&gt;&gt; E getEnum(String enumFullName) { // see https://stackoverflow.com/questions/4545937/ String[] x = enumFullName.split("\\.(?=[^\\.]+$)"); if (x.length == 2) { String enumClassName = x[0]; String enumName = x[1]; try { Class&lt;E&gt; cl = (Class&lt;E&gt;)Class.forName(enumClassName); // #1 return Enum.valueOf(cl, enumName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return null; } public void someMethod(String enumName, String fooHandlerName) { FooHandler fooHandler = getFooHandler(fooHandlerName); Enum e = getEnum(enumName); // #2 map.put(e, fooHandler); } </code></pre> <p>Warning #1: unchecked cast Warning #2: Enum is a raw type.</p> <p>I get #1 and could just put a warning I suppose, but I can't seem to beat warning #2; I've tried <code>Enum&lt;?&gt;</code> and that just gives me an error about generic type capture bound mismatch.</p> <hr> <p>Alternative implementations that are worse: Before my <code>&lt;E extends Enum&lt;E&gt;&gt;</code> generic return value, I tried returning Enum and it didn't work; I got these warnings/errors:</p> <pre><code>static private Enum&lt;?&gt; getEnum(String enumFullName) { ... Class&lt;?&gt; cl = (Class&lt;?&gt;)Class.forName(enumClassName); // 1 return Enum.valueOf(cl, enumName); // 2 } </code></pre> <ol> <li><p>warnings:</p> <pre><code> - Type safety: Unchecked cast from Class&lt;capture#3-of ?&gt; to Class&lt;Enum&gt; - Enum is a raw type. References to generic type Enum&lt;E&gt; should be parameterized - Enum is a raw type. References to generic type Enum&lt;E&gt; should be parameterized - Unnecessary cast from Class&lt;capture#3-of ?&gt; to Class&lt;?&gt; </code></pre></li> <li><p>errors:</p> <pre><code>- Type mismatch: cannot convert from capture#5-of ? to Enum&lt;?&gt; - Type safety: Unchecked invocation valueOf(Class&lt;Enum&gt;, String) of the generic method valueOf(Class&lt;T&gt;, String) of type Enum - Bound mismatch: The generic method valueOf(Class&lt;T&gt;, String) of type Enum&lt;E&gt; is not applicable for the arguments (Class&lt;capture#5-of ?&gt;, String). The inferred type capture#5-of ? is not a valid substitute for the bounded parameter &lt;T extends Enum&lt;T&gt;&gt; </code></pre></li> </ol> <p>and this:</p> <pre><code>static private Enum&lt;?&gt; getEnum(String enumFullName) { ... Class&lt;Enum&lt;?&gt;&gt; cl = (Class&lt;Enum&lt;?&gt;&gt;)Class.forName(enumClassName); // 1 return Enum.valueOf(cl, enumName); // 2 </code></pre> <ol> <li>warning: <code>Type safety: Unchecked cast from Class&lt;capture#3-of ?&gt; to Class&lt;Enum&lt;?&gt;&gt;</code></li> <li>error: <code>Bound mismatch: The generic method valueOf(Class&lt;T&gt;, String) of type Enum&lt;E&gt; is not applicable for the arguments (Class&lt;Enum&lt;?&gt;&gt;, String). The inferred type Enum&lt;?&gt; is not a valid substitute for the bounded parameter &lt;T extends Enum&lt;T&gt;&gt;</code></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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