8075799: Extraneous access checks implemented by javac
authorsadayapalam
Wed, 07 Oct 2015 19:35:43 +0530
changeset 32951 a098b538296c
parent 32950 22315d47485b
child 32952 9a64fdcb72be
8075799: Extraneous access checks implemented by javac Summary: Allow anonymous class constructors to refer to inaccessible types. Reviewed-by: mcimadamore
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java
langtools/test/tools/javac/AnonymousClass/CtorAccessBypassTest.java
langtools/test/tools/javac/generics/diamond/neg/Neg18.out
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Oct 07 16:02:57 2015 +0530
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Oct 07 19:35:43 2015 +0530
@@ -290,6 +290,13 @@
     }
 
     public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
+
+        /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
+           to refer to an inaccessible type
+        */
+        if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
+            return true;
+
         boolean isAccessible = false;
         switch ((short)(c.flags() & AccessFlags)) {
             case PRIVATE:
@@ -301,13 +308,7 @@
                 isAccessible =
                     env.toplevel.packge == c.owner // fast special case
                     ||
-                    env.toplevel.packge == c.packge()
-                    ||
-                    // Hack: this case is added since synthesized default constructors
-                    // of anonymous classes should be allowed to access
-                    // classes which would be inaccessible otherwise.
-                    env.enclMethod != null &&
-                    (env.enclMethod.mods.flags & ANONCONSTR) != 0;
+                    env.toplevel.packge == c.packge();
                 break;
             default: // error recovery
             case PUBLIC:
@@ -361,6 +362,13 @@
     }
     public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
         if (sym.name == names.init && sym.owner != site.tsym) return false;
+
+        /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
+           to refer to an inaccessible type
+        */
+        if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
+            return true;
+
         switch ((short)(sym.flags() & AccessFlags)) {
         case PRIVATE:
             return
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/AnonymousClass/CtorAccessBypassTest.java	Wed Oct 07 19:35:43 2015 +0530
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2015, 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 8075799
+ *
+ * @summary Extraneous access checks implemented by javac
+ * @compile CtorAccessBypassTest.java
+ * @run main CtorAccessBypassTest
+ *
+ */
+
+public class CtorAccessBypassTest {
+    public static void main(String[] args) {
+        new CtorAccessBypassTest_01<Object>(null) {};
+        new CtorAccessBypassTest_01<>(null) {};
+    }
+}
+
+class CtorAccessBypassTest_01<T> {
+    private class Private {}
+      CtorAccessBypassTest_01(Private p) {
+    }
+}
--- a/langtools/test/tools/javac/generics/diamond/neg/Neg18.out	Wed Oct 07 16:02:57 2015 +0530
+++ b/langtools/test/tools/javac/generics/diamond/neg/Neg18.out	Wed Oct 07 19:35:43 2015 +0530
@@ -1,3 +1,3 @@
 Neg18.java:14:21: compiler.err.prob.found.req: (compiler.misc.cant.apply.diamond.1: (compiler.misc.diamond: pkg.Neg18_01), (compiler.misc.inaccessible.varargs.type: pkg.Neg18_01.PkgPrivate, kindname.class, Neg18))
-Neg18.java:14:26: compiler.err.not.def.public.cant.access: pkg.Neg18_01.PkgPrivate, pkg.Neg18_01
+Neg18.java:14:9: compiler.err.cant.apply.symbol: kindname.constructor, , pkg.Neg18_01.PkgPrivate[], compiler.misc.no.args, kindname.class, compiler.misc.anonymous.class: <any>, (compiler.misc.inaccessible.varargs.type: pkg.Neg18_01.PkgPrivate, kindname.class, Neg18)
 2 errors