是 eclipse 还是 maven-compiler-plugin 错误,泛型类转换问题? [英] Is it an eclipse or maven-compiler-plugin bug, the generics Class cast issue?

查看:35
本文介绍了是 eclipse 还是 maven-compiler-plugin 错误,泛型类转换问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

protected <T> T doSomething(String someParam, Class<T> clazz) {
...
}

我在 TestCase 类中使用的:

which I use in a TestCase class:

Class clazz = MyClass.class;
MyClass MyClass = someObject.doSomething(someString, clazz);

这段代码在eclipse中给出警告:

Class 是原始类型.对泛型类型 Class 的引用应该被参数化

Class is a raw type. References to generic type Class should be parameterized

此行有多个标记
- 类型安全:MyClass
类型的泛型方法 doSomething(String, Class) 的未检查调用 doSomething(String, Class)- 类型安全:Class 类型的表达式需要未经检查的转换以符合 Class

Multiple markers at this line
- Type safety: Unchecked invocation doSomething(String, Class) of the generic method doSomething(String, Class) of type MyClass
- Type safety: The expression of type Class needs unchecked conversion to conform to Class

当我在 Eclipse 中运行此代码(测试)时 - 一切正常.当我通过命令提示符通过mvn clean install"执行此操作时,我得到:

C:pathToProjectsrc estjavapackagesPathMyTestCase.java:[xx,xxx] 发现不兼容的类型:
java.lang.Object
需要:com.somePackagePath.MyClass

C:pathToProjectsrc estjavapackagesPathMyTestCase.java:[xx,xxx] incompatible types found :
java.lang.Object
required: com.somePackagePath.MyClass

但提供时:

Class<MyClass> clazz = MyClass.class;
MyClass MyClass = someObject.doSomething(someString, clazz);

我没有收到任何错误或警告..
我知道 java 编译器擦除类型信息,所以:
eclipse 不应该提供编译器错误而不是警告,还是 maven-plugin-compiler 造成了问题?
maven插件是:

I don't get any errors nor warnings. .
I understand that java compiler erases type info, so:
Shouldn't eclipse provide a compiler error instead of warning, or is it the maven-plugin-compiler that creates the problem?
The maven plugin is:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <showDeprecation>false</showDeprecation>
                <debug>true</debug>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

亲切的问候,
专制

Kind Regards,
Despot

推荐答案

这是 Eclipse 中的已知错误:333011: [compiler][1.5] Eclipse 编译 javac 拒绝的代码:不兼容的类型.请随意为该错误投票并将您的用例添加到其中.

This is a known bug in Eclipse: 333011: [compiler][1.5] Eclipse compiles codes which javac rejects: incompatible types . Feel free to vote for the bug and add your use case to it.

javac 和 Eclipse 存在不一致的情况,您应该将它们报告为错误.最好有一个包含尽可能少的 Java 文件的独立测试用例,这些文件可以在 Eclipse 中复制/粘贴,并使用 javac 从命令行编译,无需任何依赖.

There are situations when javac and Eclipse disagree, and you should report them as bugs. It is preferred to have a standalone test case of as few Java files as possible which can be copy/pasted in Eclipse and compiled using javac from the command line without any dependencies.

这篇关于是 eclipse 还是 maven-compiler-plugin 错误,泛型类转换问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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