eclipse编译器或javac中的错误(“T的类型参数无法确定”) [英] Bug in eclipse compiler or in javac ("type parameters of T cannot be determined")

查看:149
本文介绍了eclipse编译器或javac中的错误(“T的类型参数无法确定”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码

public class GenericsTest2 {

    public static void main(String[] args) throws Exception {
        Integer i = readObject(args[0]);
        System.out.println(i);
    }

    public static <T> T readObject(String file) throws Exception {
        return readObject(new ObjectInputStream(new FileInputStream(file)));
        // closing the stream in finally removed to get a small example
    }

    @SuppressWarnings("unchecked")
    public static <T> T readObject(ObjectInputStream stream) throws Exception {
        return (T)stream.readObject();
    }
}

在eclipse中编译,但不使用javac(类型参数不能确定;对于具有上限T,java.lang.Object的类型变量T,不存在唯一的最大实例)。

compiles in eclipse, but not with javac (type parameters of T cannot be determined; no unique maximal instance exists for type variable T with upper bounds T,java.lang.Object).

当我将readObject(String file)更改为

When I change readObject(String file) to

    @SuppressWarnings("unchecked")
    public static <T> T readObject(String file) throws Exception {
        return (T)readObject(new ObjectInputStream(new FileInputStream(file)));
    }

它在eclipse和javac中编译。谁是正确的,eclipse编译器或javac?

it compiles in eclipse and with javac. Who is correct, the eclipse compiler or javac?

推荐答案

我会说这是Sun编译器报告的错误这里这里,因为如果你的行更改为下面的行,这两者都可以正常工作,这似乎正是错误报告中描述的。 / p>

I'd say it's the bug in the Sun compiler reported here and here, because if you change your line to the one below it works with both, which seems to be exactly what is described in the bug reports.

return GenericsTest2.<T>readObject(new ObjectInputStream(new FileInputStream(file)));

这篇关于eclipse编译器或javac中的错误(“T的类型参数无法确定”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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