There are several other SO questions talking about generics compiling OK w/ Eclipse's compiler but not javac (i.e. Java: Generics handled differenlty in Eclipse and javac and Generics compiles and runs in Eclipse, but doesn't compile in javac) -- however this looks like a slightly different one.
I have an enum
class:
public class LogEvent { public enum Type { // ... values here ... } ... }
and I have another class with a method that takes in arbitrary objects of types descended from Enum
:
@Override public <E extends Enum<E>> void postEvent( Context context, E code, Object additionalData) { if (code instanceof LogEvent.Type) { LogEvent.Type scode = (LogEvent.Type)code; ...
This works fine in Eclipse, but when I do a clean built with ant
, I am getting a pair of errors, one on the instanceof
line, the other on the casting line:
443: inconvertible types [javac] found : E [javac] required: mypackage.LogEvent.Type [javac] if (code instanceof LogEvent.Type) [javac] ^ 445: inconvertible types [javac] found : E [javac] required: com.dekaresearch.tools.espdf.LogEvent.Type [javac] LogEvent.Type scode = (LogEvent.Type)code; [javac] ^
Why does this happen, and how can I get around this problem so it will compile properly?
I don't know why it's happening, but a workaround is easy:
@Override public <E extends Enum<E>> void postEvent( Context context, E code, Object additionalData) { Object tmp = code; if (tmp instanceof LogEvent.Type) { LogEvent.Type scode = (LogEvent.Type)tmp; ...
It's ugly, but it works...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With