6711619: javac doesn't allow access to protected members in intersection types
Summary: Accordingly to new accessibility rules all members of intersection types (but private ones) should be accessible
Reviewed-by: jjg
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java Thu Oct 23 17:59:16 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java Thu Oct 23 17:59:43 2008 +0100
@@ -361,6 +361,8 @@
for (Symbol sup = clazz;
sup != null && sup != this.owner;
sup = types.supertype(sup.type).tsym) {
+ while (sup.type.tag == TYPEVAR)
+ sup = sup.type.getUpperBound().tsym;
if (sup.type.isErroneous())
return true; // error recovery
if ((sup.flags() & COMPOUND) != 0)
@@ -1183,7 +1185,9 @@
* as possible implementations.
*/
public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
- for (Type t = origin.type; t.tag == CLASS; t = types.supertype(t)) {
+ for (Type t = origin.type; t.tag == CLASS || t.tag == TYPEVAR; t = types.supertype(t)) {
+ while (t.tag == TYPEVAR)
+ t = t.getUpperBound();
TypeSymbol c = t.tsym;
for (Scope.Entry e = c.members().lookup(name);
e.scope != null;
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Oct 23 17:59:16 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Thu Oct 23 17:59:43 2008 +0100
@@ -2007,6 +2007,10 @@
log.error(pos, "type.var.cant.be.deref");
return syms.errSymbol;
} else {
+ Symbol sym2 = (sym.flags() & Flags.PRIVATE) != 0 ?
+ rs.new AccessError(env, site, sym) :
+ sym;
+ rs.access(sym2, pos, site, name, true);
return sym;
}
case ERROR:
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java Thu Oct 23 17:59:16 2008 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java Thu Oct 23 17:59:43 2008 +0100
@@ -554,7 +554,6 @@
boolean useVarargs,
boolean operator) {
if (sym.kind == ERR) return bestSoFar;
- if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
assert sym.kind < AMBIGUOUS;
try {
if (rawInstantiate(env, site, sym, argtypes, typeargtypes,
--- a/langtools/test/tools/javac/generics/6531090/T6531090b.java Thu Oct 23 17:59:16 2008 +0100
+++ b/langtools/test/tools/javac/generics/6531090/T6531090b.java Thu Oct 23 17:59:43 2008 +0100
@@ -23,7 +23,7 @@
/*
* @test
- * @bug 6531090
+ * @bug 6531090 6711619
*
* @summary Cannot access methods/fields of a captured type belonging to an intersection type
* @author Maurizio Cimadamore
@@ -32,12 +32,20 @@
public class T6531090b {
static class A {
- public void a() {}
- public A a;
+ public void a1() {}
+ protected void a2() {}
+ void a3() {}
+ public A a1;
+ protected A a2;
+ A a3;
}
static class B extends A {
- public void b(){}
- public B b;
+ public void b1() {}
+ protected void b2() {}
+ void b3() {}
+ public B b1;
+ protected B b2;
+ B b3;
}
static interface I{
void i();
@@ -65,18 +73,35 @@
}
static void testMemberMethods(C<? extends A, ? extends I> arg) {
- arg.t.a();
- arg.t.b();
+ arg.t.a1();
+ arg.t.a2();
+ arg.t.a3();
+ arg.t.b1();
+ arg.t.b2();
+ arg.t.b3();
arg.t.i1();
- arg.w.a();
- arg.w.b();
+ arg.w.a1();
+ arg.w.a2();
+ arg.w.a3();
+ arg.w.b1();
+ arg.w.b2();
+ arg.w.b3();
arg.w.i1();
}
static void testMemberFields(C<? extends A, ? extends I> arg) {
- A ta = arg.t.a;
- B tb = arg.t.b;
- A wa = arg.w.a;
- B wb = arg.w.b;
+ A ta; B tb;
+ ta = arg.t.a1;
+ ta = arg.t.a2;
+ ta = arg.t.a3;
+ tb = arg.t.b1;
+ tb = arg.t.b2;
+ tb = arg.t.b3;
+ ta = arg.w.a1;
+ ta = arg.w.a2;
+ ta = arg.w.a3;
+ tb = arg.w.b1;
+ tb = arg.w.b2;
+ tb = arg.w.b3;
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6711619/T6711619a.java Thu Oct 23 17:59:43 2008 +0100
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6711619
+ *
+ * @summary javac doesn't allow access to protected members in intersection types
+ * @author Maurizio Cimadamore
+ *
+ * @compile/fail/ref=T6711619a.out -XDrawDiagnostics T6711619a.java
+ */
+class T6711619a {
+
+ static class A {
+ private void a() {}
+ private A a;
+ }
+ static class B extends A {
+ private B b() {}
+ private B b;
+ }
+ static interface I{
+ void i();
+ }
+ static interface I1{
+ void i1();
+ }
+ static class E extends B implements I, I1{
+ public void i() {}
+ public void i1() {}
+ }
+ static class C<W extends B & I1, T extends W>{
+ T t;
+ W w;
+ C(W w, T t) {
+ this.w = w;
+ this.t = t;
+ }
+ }
+
+ static void testMemberMethods(C<? extends A, ? extends I> arg) {
+ arg.t.a();
+ arg.t.b();
+ }
+
+ static void testMemberFields(C<? extends A, ? extends I> arg) {
+ A ta; B tb;
+ ta = arg.t.a;
+ tb = arg.t.b;
+ ta = arg.w.a;
+ tb = arg.w.b;
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6711619/T6711619a.out Thu Oct 23 17:59:43 2008 +0100
@@ -0,0 +1,7 @@
+T6711619a.java:63:14: compiler.err.report.access: a(), private, T6711619a.A
+T6711619a.java:64:14: compiler.err.report.access: b(), private, T6711619a.B
+T6711619a.java:69:19: compiler.err.report.access: a, private, T6711619a.A
+T6711619a.java:70:19: compiler.err.report.access: b, private, T6711619a.B
+T6711619a.java:71:19: compiler.err.report.access: a, private, T6711619a.A
+T6711619a.java:72:19: compiler.err.report.access: b, private, T6711619a.B
+6 errors
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6711619/T6711619b.java Thu Oct 23 17:59:43 2008 +0100
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6711619
+ *
+ * @summary javac doesn't allow access to protected members in intersection types
+ * @author Maurizio Cimadamore
+ *
+ * @compile/fail/ref=T6711619b.out -XDrawDiagnostics T6711619b.java
+ */
+
+class T6711619b {
+ static class X1<E extends X1<E>> {
+ private int i;
+ E e;
+ int f() {
+ return e.i;
+ }
+ }
+
+ static class X2<E extends X2<E>> {
+ static private int i;
+ int f() {
+ return E.i;
+ }
+ }
+
+ static class X3<E extends X3<E> & java.io.Serializable> {
+ private int i;
+ E e;
+ int f() {
+ return e.i;
+ }
+ }
+
+ static class X4<E extends X4<E> & java.io.Serializable> {
+ static private int i;
+ int f() {
+ return E.i;
+ }
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/generics/6711619/T6711619b.out Thu Oct 23 17:59:43 2008 +0100
@@ -0,0 +1,5 @@
+T6711619b.java:39:22: compiler.err.report.access: i, private, T6711619b.X1
+T6711619b.java:46:22: compiler.err.report.access: i, private, T6711619b.X2
+T6711619b.java:54:22: compiler.err.report.access: i, private, T6711619b.X3
+T6711619b.java:61:22: compiler.err.report.access: i, private, T6711619b.X4
+4 errors