Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: specific enums and generic Enum<?> parameters
    text
    copied!<p>I want to pass any enum value to method in utility class and get another enum value of same enum type. Something like this:</p> <pre><code>public class XMLUtils { public static Enum&lt;?&gt; getEnumAttribute(Element element, String name, Enum&lt;?&gt; defaultValue) { if (element.hasAttribute(name)) { String valueName = element.getAttribute(name); // search for value for (Enum&lt;?&gt; value: defaultValue.getClass().getEnumConstants()) if (value.toString().equalsIgnoreCase(valueName)) return value; } // not found, return default value return defaultValue; } } </code></pre> <p>Use of method <code>getEnumAttribute()</code>:</p> <pre><code>// simple enum public enum EUploadMethod { INSERT, UPDATE, DELETE } // read enum value from XML config file EUploadMethod method = XMLUtils.getEnumAttribute(element, "method", EUploadMethod.INSERT); </code></pre> <p>This code is fully functional, <strong>Eclipse compiles and runs it without warnings</strong> or errors and it works like a charm.</p> <p>But when I clean and compile project from command line <strong>by Maven2, it fails with error</strong> on line where is <code>getEnumAttribute</code> called:</p> <pre><code>$ mvn clean compile .... [ERROR] /home/.... DataUploader.java:[173,53] inconvertible types found : java.lang.Enum&lt;capture#414 of ?&gt; required: .....DataUploader.EUploadMethod </code></pre> <p>I am using Sun JDK 1.6 in either Eclipse and Maven:</p> <pre><code>$ mvn -version Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200) Java version: 1.6.0_14 Java home: /usr/lib/jvm/java-6-sun-1.6.0.14/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux" version: "2.6.27-17-generic" arch: "i386" Family: "unix" </code></pre> <p>Questions:</p> <ol> <li><p>Why this code is compilable and <strong>functional in Eclipse</strong>, and compile <strong>fails in Maven</strong> which using as far as I know same javac compiler?</p></li> <li><p>What's wrong with passing specific enums to generic <code>Enum&lt;?&gt;</code> parameters?</p></li> </ol> <p>Thanks,</p> <p>Martin Schayna</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