Maven编译器与Eclipse编译器泛型差异?

编程入门 行业动态 更新时间:2024-10-26 23:38:20
本文介绍了Maven编译器与Eclipse编译器泛型差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 当我从Eclipse构建到maven构建时,我的项目中发现了很多问题。我使用2.5.1编译器插件。

JDK是开放的JDK-7

我孤立的问题在一个新项目中,解决了这个问题:

public class Test { public static void main(String [] args){列表< String> list = newList(); for(String name:sort(newList(list))){ System.out.println(name); } } public static< T>列表与LT; T> newList(){ return new ArrayList< T>(); } public static< T,E扩展T>列表与LT; T> newList(Collection< E> list){ return new ArrayList< T>(); } public static< T>列表与LT; T> sort(List< T> list){ return list; } }

这不能用javaC编译(但在Eclipse中工作)说明以下错误:

[ERROR]无法执行目标org.apache.maven.plugins:maven-compiler-plugin :2.5.1:在项目测试中编译(default-compile):编译失败

[ERROR] / home / username / workspaces / projectx43 / test / src / main / java / test / Test.java:[11,24]错误:不兼容类型

public class Test { public static void main(String [] args){列表< String> list = newList(); for(String name:sort(newList(list))){ System.out.println(name); } } public static< T>列表与LT; T> newList(){ return new ArrayList< T>(); } public static< T>列表与LT; T> newList(Collection<?extends T> list){ return new ArrayList< T>(); } public static< T>列表与LT; T> sort(List< T> list){ return list; } }

每个人都可以看到E版本与好的一个只使用T。现在的问题是,我可以调整javac编译器来接受这个。任何关于此的信息将不胜感激。

另外一种可能性:OpenJDK 7是否以不同的方式处理SunJDK 7?如果您可以使用最新的Windows Sun JDK 7版本甚至JDK 8的测试版验证此代码段。非常感谢。

PS:我读过这些Eclipse vs JavaC +泛型问题和链接的Bug被说明要解决。

解决方案

我记得在2008年采用Java 5.0时,同样的问题是一个问题。它只是与eclipse编译器处理泛型的事实有些不同。

如果您检查上述示例,则JavaC编译器不允许传递泛型解析。例如,我们来看看这个:

列表< String> strings = sort(newList(A,B,C);

JavaC抱怨这里说排序不适用于列表。

< T>列表< T> sort(List< T> ...}; < T> List< T> newList(T ... elements){};

解决方案是引入一个破坏上述表达式的变量的变量:

列表< String> list = newList(A,B,C); 列表< String> strings = sort(list);

它取决于是否希望这是一个错误,我喜欢Eclipse编译器版本更好,因为它生成更容易阅读表达式。

I spotted many problems in my project when I went from Eclipse build to maven build. I use the 2.5.1 compiler plugin.

JDK is open-JDK-7

I isolated the problem in a new project and stipping it down the problem is this:

public class Test { public static void main(String[] args) { List<String> list = newList(); for(String name : sort(newList(list))) { System.out.println(name); } } public static <T> List<T> newList() { return new ArrayList<T>(); } public static <T, E extends T> List<T> newList(Collection<E> list) { return new ArrayList<T>(); } public static <T> List<T> sort(List<T> list) { return list; } }

This fails to compile with javaC (but works in Eclipse) stating the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project test: Compilation failure

[ERROR] /home/username/workspaces/projectx43/test/src/main/java/test/Test.java:[11,24] error: incompatible types

And this will work:

public class Test { public static void main(String[] args) { List<String> list = newList(); for(String name : sort(newList(list))) { System.out.println(name); } } public static <T> List<T> newList() { return new ArrayList<T>(); } public static <T> List<T> newList(Collection<? extends T> list) { return new ArrayList<T>(); } public static <T> List<T> sort(List<T> list) { return list; } }

Everyone can see that the version with E is just as good as the one using only T. The question now is, can I tweak the javac compiler to accept this. Any information about this would be appreciated.

Also another possibility: Is openJDK 7 is handling this differently to SunJDK 7? If you can please verify this snippet with the latest windows Sun JDK 7 versions or even with the beta of JDK 8. Thanks a lot.

PS: I read those Eclipse vs. JavaC + Generics questions and the Bugs linked are stating to be resolved.

解决方案

I remembered the very same issue being a problem in 2008 when we adopted Java 5.0. It is just related to the fact that the eclipse compiler handles generics slightly differently.

If you check the above example the JavaC compiler does not allow transitive generics resolution. For an example lets look at this:

List<String> strings = sort(newList("A", "B", "C");

JavaC complains here saying that sort is not applicable to List.

<T> List<T> sort(List<T>) {...}; <T> List<T> newList(T ... elements) {};

The solution is to introduce a variable destroying the beauty of the expression above:

List<String> list = newList("A", "B", "C"); List<String> strings = sort(list);

It depends whether one expect this to be an error or not. I like the Eclipse compiler version better since it produces easier to read expressions.

更多推荐

Maven编译器与Eclipse编译器泛型差异?

本文发布于:2023-07-27 14:17:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编译器   差异   Maven   Eclipse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!