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

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

问题描述

我有一个这样的代码:

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

我在TestCase类中使用:

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

此代码在eclipse中发出警告


类是一个原始类型。参考泛型类应该参数化



此行上的多个标记

- 类型安全性:未检查的调用类型MyClass的通用方法doSomething(String,Class)的doSomething(String,Class)

- 类型安全性:类型类的表达式需要未经检查的转换才能符合Class


当我在eclipse中运行此代码(测试) - 一切都很完美。当我通过mvn clean install通过命令提示符进行操作时,我得到:


C:\pathToProject\ src\test\java\packagesPath\MyTestCase.java:找到不兼容类型的[xx,xxx]:

java.lang.Object

required:com.somePackagePath。 MyClass


但是提供时:

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

我没有收到任何错误或警告。

我明白java编译器擦除类型信息,所以:

不应该eclipse提供编译器错误而不是警告,还是maven-plugin-compiler是否会产生问题? br>
maven插件是:

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

请问,

Despot

解决方案

这是Eclipse中的一个已知错误: 333011:[编译器] [1.5] Eclipse编译javac拒绝的代码:不兼容的类型。可以自由地投票支持该错误,并添加您的用例。






有些情况下 javac 和Eclipse不同意,您应该将其报告为错误。优选的是具有尽可能少的Java文件的独立测试用例,它们可以在Eclipse中复制/粘贴,并使用命令行中的 javac 进行编译,而无需依赖。 / p>

I have a code like this:

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

which I use in a TestCase class:

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

This code gives a warning in eclipse:

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

and

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

When I run this code (test) in eclipse - everything works perfectly. When I do it through "mvn clean install" through command prompt, I get:

C:\pathToProject\src\test\java\packagesPath\MyTestCase.java:[xx,xxx] incompatible types found :
java.lang.Object
required: com.somePackagePath.MyClass

But when providing:

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

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

解决方案

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.


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