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

查看:17
本文介绍了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 的类型参数;对于具有上限 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 编译器报告的错误 这里这里a>,因为如果您将您的行更改为下面的那一行,它将同时适用于两者,这似乎正是错误报告中描述的内容.

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天全站免登陆