javac error: inconvertible types with generics?

后端 未结 4 1923
醉梦人生
醉梦人生 2020-12-24 02:33

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 a

4条回答
  •  滥情空心
    2020-12-24 03:02

    In order to use instanceof both operands have to inherit/implement the same class/interface.
    E just can't be cast to LogEvent.Type

    I don't know what your full method looks like, but this should solve your issue by using interfaces and not Generics.

    public interface EventType { }
    public class LogEvent  {
        public enum Type implements EventType {}
    }
    
    public void postEvent(Context context, EventType code, Object additionalData) {
        if(code instanceof LogEvent.Type) {
        }
    }
    

提交回复
热议问题