javac错误:不可逆类型与泛型? [英] javac error: inconvertible types with generics?

查看:190
本文介绍了javac错误:不可逆类型与泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个其他的SO问题谈论泛型编译OK / Eclipse的编译器,但不是javac(即 Java:泛型处理差异在Eclipse和javac 泛型在Eclipse中编译和运行,但不在javac中编译) - 但是此看起来略有不同。

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.

我有一个枚举 class:

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

这在Eclipse中很好,但是当我做一个干净的 ant ,我得到一对错误,一个在 instanceof 行,另一个在投放线: / p>

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

这篇关于javac错误:不可逆类型与泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆