Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The signature</p> <pre><code>static private &lt;E extends Enum&lt;E&gt;&gt; getEnum(String enumFullName) </code></pre> <p>isn't doing you any good here.</p> <p>The <code>&lt;E extends Enum&lt;E&gt;&gt;</code> allows the caller of the method to assign the result of <code>getEnum</code> to any <code>enum</code> type they want without any casting:</p> <pre><code>SomeEnum e = getEnum("com.foo.SomeOtherEnum.SOMETHING"); // ClassCastException! </code></pre> <p>However, that doesn't make any sense... if the caller knew what specific type of <code>enum</code> the method would return, they could do something more sensible like <code>SomeEnum.valueOf("SOMETHING")</code>.</p> <p>The only thing that makes sense here is for <code>getEnum</code> to just return <code>Enum&lt;?&gt;</code>, which seems like what you really want to do anyway:</p> <pre><code>static private Enum&lt;?&gt; getEnum(String enumFullName) { String[] x = enumFullName.split("\\.(?=[^\\.]+$)"); if(x.length == 2) { String enumClassName = x[0]; String enumName = x[1]; try { @SuppressWarnings("unchecked") Class&lt;Enum&gt; cl = (Class&lt;Enum&gt;) Class.forName(enumClassName); return Enum.valueOf(cl, enumName); } catch(ClassNotFoundException e) { e.printStackTrace(); } } return null; } </code></pre> <p>The above compiles with no warnings and works properly. The cast to <code>Class&lt;Enum&gt;</code> warning is suppressed because we know that it isn't safe to do that and that <code>Enum.valueOf</code> will blow up if the class with the given name isn't an <code>enum</code> class and that's what we want to do.</p>
 

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