8009129: Illegal access error when calling method reference
Summary: Javac generates method handle referencing non public type
Reviewed-by: jjg, rfield
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Mar 05 14:16:07 2013 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Mar 05 14:19:49 2013 +0000
@@ -2623,6 +2623,7 @@
that.sym = refSym.baseSymbol();
that.kind = lookupHelper.referenceKind(that.sym);
+ that.ownerAccessible = rs.isAccessible(localEnv, that.sym.enclClass());
if (desc.getReturnType() == Type.recoveryType) {
// stop here
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Tue Mar 05 14:16:07 2013 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Tue Mar 05 14:19:49 2013 +0000
@@ -1775,16 +1775,26 @@
}
boolean isPrivateConstructor() {
+ //hack needed to workaround 292 bug (8005122)
+ //when 292 issue is fixed we should simply remove this
return tree.sym.name == names.init &&
(tree.sym.flags() & PRIVATE) != 0;
}
+ boolean receiverAccessible() {
+ //hack needed to workaround 292 bug (7087658)
+ //when 292 issue is fixed we should remove this and change the backend
+ //code to always generate a method handle to an accessible method
+ return tree.ownerAccessible;
+ }
+
/**
* Does this reference needs a bridge (i.e. var args need to be
* expanded or "super" is used)
*/
final boolean needsBridge() {
- return isSuper || needsVarArgsConversion() || isArrayOp() || isPrivateConstructor();
+ return isSuper || needsVarArgsConversion() || isArrayOp() ||
+ isPrivateConstructor() || !receiverAccessible();
}
Type generatedRefSig() {
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Mar 05 14:16:07 2013 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Mar 05 14:19:49 2013 +0000
@@ -766,6 +766,10 @@
compiler.err.not.def.public.cant.access=\
{0} is not public in {1}; cannot be accessed from outside package
+# 0: symbol, 1: symbol
+compiler.misc.not.def.public.cant.access=\
+ {0} is not public in {1}; cannot be accessed from outside package
+
# 0: name
compiler.err.not.loop.label=\
not a loop label: {0}
--- a/langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java Tue Mar 05 14:16:07 2013 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/tree/JCTree.java Tue Mar 05 14:19:49 2013 +0000
@@ -1887,6 +1887,7 @@
public Symbol sym;
public Type varargsElement;
public PolyKind refPolyKind;
+ public boolean ownerAccessible;
/**
* Javac-dependent classification for member references, based
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/NotDefPublicCantAccessFragment/NotDefPublicCantAccessFragment.java Tue Mar 05 14:19:49 2013 +0000
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.prob.found.req
+// key: compiler.misc.invalid.mref
+// key: compiler.misc.not.def.public.cant.access
+
+class NotDefPublicCantAccessFragment {
+ interface SAM {
+ void m();
+ }
+
+ void test (p.C c) {
+ SAM s = c::m;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/NotDefPublicCantAccessFragment/p/C.java Tue Mar 05 14:19:49 2013 +0000
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package p;
+
+public class C {
+ void m() { }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/inaccessibleMref01/InaccessibleMref01.java Tue Mar 05 14:19:49 2013 +0000
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8003280
+ * @summary Add lambda tests
+ * check that classfiles with member ref CP entries are read correctly
+ * @compile/fail/ref=InaccessibleMref01.out -XDrawDiagnostics InaccessibleMref01.java
+ */
+class InaccessibleMref01 {
+ interface SAM {
+ void m();
+ }
+
+ void test(p1.C c) {
+ SAM s = c::m;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/inaccessibleMref01/InaccessibleMref01.out Tue Mar 05 14:19:49 2013 +0000
@@ -0,0 +1,2 @@
+InaccessibleMref01.java:37:17: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.method, (compiler.misc.not.def.public.cant.access: m(), p1.C))
+1 error
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/inaccessibleMref01/p1/C.java Tue Mar 05 14:19:49 2013 +0000
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package p1;
+
+public class C {
+ void m() { }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/inaccessibleMref02/InaccessibleMref02.java Tue Mar 05 14:19:49 2013 +0000
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8003280
+ * @summary Add lambda tests
+ * check that classfiles with member ref CP entries are read correctly
+ */
+public class InaccessibleMref02 {
+ interface SAM {
+ void m();
+ }
+
+ public static void main(String[] args) {
+ p1.C c = new p1.C();
+ SAM s = c::m;
+ s.m();
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/inaccessibleMref02/p1/C.java Tue Mar 05 14:19:49 2013 +0000
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package p1;
+
+class Sup {
+ public void m() { }
+}
+
+public class C extends Sup { }