Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javac error: inconvertible types with generics?

Tags:

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?

like image 719
Jason S Avatar asked Jan 28 '11 14:01

Jason S


1 Answers

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...

like image 140
Jon Skeet Avatar answered Oct 23 '22 17:10

Jon Skeet