6598108: com.sun.source.util.Trees.isAccessible incorrect
authormcimadamore
Fri, 12 Nov 2010 12:32:43 +0000
changeset 7209 889e80aa1d2e
parent 7208 5db8147393ec
child 7210 8dd5f907461e
6598108: com.sun.source.util.Trees.isAccessible incorrect Summary: JavacTrees' version of isAccessible should take into account enclosing class accessibility Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java
langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
langtools/test/tools/javac/api/6598108/T6598108.java
--- a/langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Wed Nov 10 12:37:25 2010 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Fri Nov 12 12:32:43 2010 +0000
@@ -49,6 +49,7 @@
 import com.sun.tools.javac.code.Symbol.ClassSymbol;
 import com.sun.tools.javac.code.Symbol.TypeSymbol;
 import com.sun.tools.javac.code.Symbol;
+import com.sun.tools.javac.code.Type;
 import com.sun.tools.javac.comp.Attr;
 import com.sun.tools.javac.comp.AttrContext;
 import com.sun.tools.javac.comp.Enter;
@@ -230,7 +231,7 @@
     public boolean isAccessible(Scope scope, TypeElement type) {
         if (scope instanceof JavacScope && type instanceof ClassSymbol) {
             Env<AttrContext> env = ((JavacScope) scope).env;
-            return resolve.isAccessible(env, (ClassSymbol)type);
+            return resolve.isAccessible(env, (ClassSymbol)type, true);
         } else
             return false;
     }
@@ -240,7 +241,7 @@
                 && member instanceof Symbol
                 && type instanceof com.sun.tools.javac.code.Type) {
             Env<AttrContext> env = ((JavacScope) scope).env;
-            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member);
+            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
         } else
             return false;
     }
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Nov 10 12:37:25 2010 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri Nov 12 12:32:43 2010 +0000
@@ -159,33 +159,45 @@
      *  @param c      The class whose accessibility is checked.
      */
     public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
+        return isAccessible(env, c, false);
+    }
+
+    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
+        boolean isAccessible = false;
         switch ((short)(c.flags() & AccessFlags)) {
-        case PRIVATE:
-            return
-                env.enclClass.sym.outermostClass() ==
-                c.owner.outermostClass();
-        case 0:
-            return
-                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;
-        default: // error recovery
-        case PUBLIC:
-            return true;
-        case PROTECTED:
-            return
-                env.toplevel.packge == c.owner // fast special case
-                ||
-                env.toplevel.packge == c.packge()
-                ||
-                isInnerSubClass(env.enclClass.sym, c.owner);
+            case PRIVATE:
+                isAccessible =
+                    env.enclClass.sym.outermostClass() ==
+                    c.owner.outermostClass();
+                break;
+            case 0:
+                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;
+                break;
+            default: // error recovery
+            case PUBLIC:
+                isAccessible = true;
+                break;
+            case PROTECTED:
+                isAccessible =
+                    env.toplevel.packge == c.owner // fast special case
+                    ||
+                    env.toplevel.packge == c.packge()
+                    ||
+                    isInnerSubClass(env.enclClass.sym, c.owner);
+                break;
         }
+        return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
+            isAccessible :
+            isAccessible & isAccessible(env, c.type.getEnclosingType(), checkInner);
     }
     //where
         /** Is given class a subclass of given base class, or an inner class
@@ -202,9 +214,13 @@
         }
 
     boolean isAccessible(Env<AttrContext> env, Type t) {
+        return isAccessible(env, t, false);
+    }
+
+    boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
         return (t.tag == ARRAY)
             ? isAccessible(env, types.elemtype(t))
-            : isAccessible(env, t.tsym);
+            : isAccessible(env, t.tsym, checkInner);
     }
 
     /** Is symbol accessible as a member of given type in given evironment?
@@ -214,6 +230,9 @@
      *  @param sym    The symbol.
      */
     public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
+        return isAccessible(env, site, sym, false);
+    }
+    public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
         if (sym.name == names.init && sym.owner != site.tsym) return false;
         ClassSymbol sub;
         switch ((short)(sym.flags() & AccessFlags)) {
@@ -231,7 +250,7 @@
                  ||
                  env.toplevel.packge == sym.packge())
                 &&
-                isAccessible(env, site)
+                isAccessible(env, site, checkInner)
                 &&
                 sym.isInheritedIn(site.tsym, types)
                 &&
@@ -248,11 +267,11 @@
                  // (but type names should be disallowed elsewhere!)
                  env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
                 &&
-                isAccessible(env, site)
+                isAccessible(env, site, checkInner)
                 &&
                 notOverriddenIn(site, sym);
         default: // this case includes erroneous combinations as well
-            return isAccessible(env, site) && notOverriddenIn(site, sym);
+            return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
         }
     }
     //where
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/api/6598108/T6598108.java	Fri Nov 12 12:32:43 2010 +0000
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2010, 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     6598108
+ * @summary com.sun.source.util.Trees.isAccessible incorrect
+ * @author  Jan Lahoda
+ */
+
+import com.sun.source.tree.CompilationUnitTree;
+import com.sun.source.tree.Scope;
+import com.sun.source.util.JavacTask;
+import com.sun.source.util.TreePath;
+import com.sun.source.util.Trees;
+import java.net.URI;
+import java.util.Arrays;
+import javax.lang.model.element.TypeElement;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.ToolProvider;
+
+public class T6598108 {
+    public static void main(String[] args) throws Exception {
+        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
+        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
+        assert tool != null;
+        final JavacTask ct = (JavacTask)tool.getTask(null, null, null, Arrays.asList("-bootclasspath",  bootPath), null, Arrays.asList(new MyFileObject()));
+
+        CompilationUnitTree cut = ct.parse().iterator().next();
+        TreePath tp = new TreePath(new TreePath(cut), cut.getTypeDecls().get(0));
+        Scope s = Trees.instance(ct).getScope(tp);
+        TypeElement type = ct.getElements().getTypeElement("com.sun.java.util.jar.pack.Package.File");
+
+        if (Trees.instance(ct).isAccessible(s, type)) {
+            //com.sun.java.util.jar.pack.Package.File is a public innerclass inside a non-accessible class, so
+            //"false" would be expected here.
+            throw new IllegalStateException("");
+        }
+    }
+
+    static class MyFileObject extends SimpleJavaFileObject {
+        public MyFileObject() {
+            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
+        }
+        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+            return "public class Test<TTT> { public void test() {TTT ttt;}}";
+        }
+    }
+}