无法确定 <E>E 的类型参数;对于上限为 int,java.lang.Object 的类型变量 E,不存在唯一最大实例

1 篇文章 0 订阅
本文介绍了一个关于Eclipse与Ant编译器在处理泛型时出现的不同行为问题,特别是当使用JDK 1.6时会出现类型参数确定错误。文章提供了多种解决方案,并指出该问题是JDK 1.6的已知bug,在升级到JDK 1.7后得以修复。
摘要由CSDN通过智能技术生成

在Eclipse中能够编译运行,同样的代码,使用ant编译会失败报错:无法确定 <E>E 的类型参数;对于上限为 int,java.lang.Object 的类型变量 E,不存在唯一最大实例

通过在网上查找资料,有提到eclipse中是用到JDT编译而ant javac使用JDK,找到了几种解决方案:

1.配置使用JDK编译[0]

2.通过加强制类型转换,可以临时绕过该问题,参考[1][2]

3.[2][3]同时指出这是JDK1.6版本的问题,[2]中指出在升级到jdk1.6.0_38版本后,测试打包就没有问题,而我这边没有成功,根据[3]中参考的stackoverflow链接中有人指出这是JDK的一个bug,在JDK1.7中被修复了[4]。我使用JDK1.7后问题没有出现。

bug_id=6302954

JDK-6302954 : Inference fails for type variable return constraint

Details
Type:
Bug
Submit Date:
2005-07-28
Status:
Closed
Updated Date:
2011-03-08
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
tools
OS:
generic,windows_xp,windows_2000
Sub-Component:
javac
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0,6,6u20
Fixed Versions:
(b03)

Related Reports
Backport:
Backport:
Backport:
Duplicate:
Duplicate:
Relates:

Sub Tasks

Description
https://bugs.eclipse.org/bugs/show_bug.cgi?id=98379

This program doesn't compile:

public class X {
    <T extends X> T f1() throws Exception{
    	return null;
    }
    <U extends X> U f2() throws Exception {
        return f1();
    }
}

X.java:6: type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds U,X
        return f1();
                 ^
1 error

                                    

Comments
SUGGESTED FIX

Index: j2se/src/share/classes/com/sun/tools/javac/comp/Infer.java
--- /tmp/geta16053	2006-10-18 23:36:54.000000000 -0700
+++ /tmp/getb16053	2006-10-18 23:36:54.000000000 -0700
@@ -140,7 +140,7 @@
 		     bs.nonEmpty() && that.inst == null;
 		     bs = bs.tail) {
 		    // System.out.println("hibounds = " + that.hibounds);//DEBUG
-		    if (isSubClass(bs.head, that.hibounds, types))
+		    if (isSubClass(bs.head, that.hibounds))
 			that.inst = types.fromUnknownFun.apply(bs.head);
 		}
 		if (that.inst == null || !types.isSubtypeUnchecked(that.inst, that.hibounds, warn))
@@ -151,19 +151,25 @@
 	}
     }
     //where
-	private boolean isSubClass(Type that, List<Type> those, Types types) {
-	    Type that1 = that.baseType();
-	    boolean isSub = true;
-	    if (that1.tag == TYPEVAR) {
-		for (List<Type> l = types.getBounds(((TypeVar)that1)); isSub && l.nonEmpty(); l = l.tail) {
-		    isSub = isSubClass(l.head, those, types);
+        private boolean isSubClass(Type t, final List<Type> ts) {
+            t = t.baseType();
+            if (t.tag == TYPEVAR) {
+                List<Type> bounds = types.getBounds((TypeVar)t);
+                for (Type s : ts) {
+                    if (!types.isSameType(t, s.baseType())) {
+                        for (Type bound : bounds) {
+                            if (!isSubClass(bound, List.of(s.baseType())))
+                                return false;
+                        }
+                    }
 		}
 	    } else {
-		for (List<Type> l = those; isSub && l.nonEmpty(); l = l.tail) {
-		    isSub = that1.tsym.isSubClass(l.head.baseType().tsym, types);
+                for (Type s : ts) {
+                    if (!t.tsym.isSubClass(s.baseType().tsym, types))
+                        return false;
 		}
 	    }
-	    return isSub;
+            return true;
 	}
 
     /** Instaniate undetermined type variable to the lub of all its lower bounds.
@@ -192,7 +198,7 @@
 	    else for (List<Type> bs = that.hibounds;
 		      bs.nonEmpty() && hb == null;
 		      bs = bs.tail) {
-		if (isSubClass(bs.head, that.hibounds, types))
+		if (isSubClass(bs.head, that.hibounds))
 		    hb = types.fromUnknownFun.apply(bs.head);
 	    }
 	    if (hb == null ||
                                     
2006-10-19
SUGGESTED FIX

Webrev of changes: http://sa.sfbay/projects/langtools/bugid_summary.pl?bugid=6302954
See also attachment 6302954.tar.gz.
                                     
2006-10-19
EVALUATION

A bug.  The compiler should infer U.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值