Wed, 02 Apr 2008 11:44:23 +0100
6531090: Cannot access methods/fields of a captured type belonging to an intersection type
Summary: fixed lookup of field/methods on intersection types
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Wed Apr 02 11:38:16 2008 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Wed Apr 02 11:44:23 2008 +0100 1.3 @@ -1298,7 +1298,7 @@ 1.4 return t; 1.5 1.6 Type st = supertype(t); 1.7 - if (st.tag == CLASS || st.tag == ERROR) { 1.8 + if (st.tag == CLASS || st.tag == TYPEVAR || st.tag == ERROR) { 1.9 Type x = asSuper(st, sym); 1.10 if (x != null) 1.11 return x; 1.12 @@ -1320,7 +1320,10 @@ 1.13 1.14 @Override 1.15 public Type visitTypeVar(TypeVar t, Symbol sym) { 1.16 - return asSuper(t.bound, sym); 1.17 + if (t.tsym == sym) 1.18 + return t; 1.19 + else 1.20 + return asSuper(t.bound, sym); 1.21 } 1.22 1.23 @Override
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Wed Apr 02 11:38:16 2008 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Wed Apr 02 11:44:23 2008 +0100 2.3 @@ -407,6 +407,8 @@ 2.4 Type site, 2.5 Name name, 2.6 TypeSymbol c) { 2.7 + while (c.type.tag == TYPEVAR) 2.8 + c = c.type.getUpperBound().tsym; 2.9 Symbol bestSoFar = varNotFound; 2.10 Symbol sym; 2.11 Scope.Entry e = c.members().lookup(name); 2.12 @@ -418,7 +420,7 @@ 2.13 e = e.next(); 2.14 } 2.15 Type st = types.supertype(c.type); 2.16 - if (st != null && st.tag == CLASS) { 2.17 + if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) { 2.18 sym = findField(env, site, name, st.tsym); 2.19 if (sym.kind < bestSoFar.kind) bestSoFar = sym; 2.20 } 2.21 @@ -733,7 +735,9 @@ 2.22 boolean allowBoxing, 2.23 boolean useVarargs, 2.24 boolean operator) { 2.25 - for (Type ct = intype; ct.tag == CLASS; ct = types.supertype(ct)) { 2.26 + for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) { 2.27 + while (ct.tag == TYPEVAR) 2.28 + ct = ct.getUpperBound(); 2.29 ClassSymbol c = (ClassSymbol)ct.tsym; 2.30 if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) 2.31 abstractok = false;
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/generics/6531090/T6531090a.java Wed Apr 02 11:44:23 2008 +0100 3.3 @@ -0,0 +1,59 @@ 3.4 +/* 3.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 3.24 + * have any questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 6531090 3.30 + * 3.31 + * @summary Cannot access methods/fields of a captured type belonging to an intersection type 3.32 + * @author Maurizio Cimadamore 3.33 + * 3.34 + */ 3.35 +public class T6531090a { 3.36 + 3.37 + static class E {} 3.38 + 3.39 + static class F extends E implements I1 {} 3.40 + 3.41 + static interface I {} 3.42 + 3.43 + static interface I1 {} 3.44 + 3.45 + static class G extends F implements I {} 3.46 + 3.47 + static class C<T extends E & I> { 3.48 + T field; 3.49 + } 3.50 + 3.51 + public static void main(String... args) { 3.52 + test(new C<G>()); 3.53 + } 3.54 + 3.55 + static <W extends F> void test(C<? extends W> arg) { 3.56 + F vf = arg.field; 3.57 + I vi = arg.field; 3.58 + I1 vi1 = arg.field; 3.59 + E ve = arg.field; 3.60 + W vt = arg.field; 3.61 + } 3.62 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/generics/6531090/T6531090b.java Wed Apr 02 11:44:23 2008 +0100 4.3 @@ -0,0 +1,82 @@ 4.4 +/* 4.5 + * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 4.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 4.24 + * have any questions. 4.25 + */ 4.26 + 4.27 +/* 4.28 + * @test 4.29 + * @bug 6531090 4.30 + * 4.31 + * @summary Cannot access methods/fields of a captured type belonging to an intersection type 4.32 + * @author Maurizio Cimadamore 4.33 + * 4.34 + */ 4.35 +public class T6531090b { 4.36 + 4.37 + static class A { 4.38 + public void a() {} 4.39 + public A a; 4.40 + } 4.41 + static class B extends A { 4.42 + public void b(){} 4.43 + public B b; 4.44 + } 4.45 + static interface I{ 4.46 + void i(); 4.47 + } 4.48 + static interface I1{ 4.49 + void i1(); 4.50 + } 4.51 + static class E extends B implements I, I1{ 4.52 + public void i() {} 4.53 + public void i1() {} 4.54 + } 4.55 + static class C<W extends B & I1, T extends W>{ 4.56 + T t; 4.57 + W w; 4.58 + C(W w, T t) { 4.59 + this.w = w; 4.60 + this.t = t; 4.61 + } 4.62 + } 4.63 + 4.64 + public static void main(String... args) { 4.65 + C<E,E> c = new C<E,E>(new E(), new E()); 4.66 + testMemberMethods(c); 4.67 + testMemberFields(c); 4.68 + } 4.69 + 4.70 + static void testMemberMethods(C<? extends A, ? extends I> arg) { 4.71 + arg.t.a(); 4.72 + arg.t.b(); 4.73 + arg.t.i1(); 4.74 + arg.w.a(); 4.75 + arg.w.b(); 4.76 + arg.w.i1(); 4.77 + } 4.78 + 4.79 + static void testMemberFields(C<? extends A, ? extends I> arg) { 4.80 + A ta = arg.t.a; 4.81 + B tb = arg.t.b; 4.82 + A wa = arg.w.a; 4.83 + B wb = arg.w.b; 4.84 + } 4.85 +}