6598108: com.sun.source.util.Trees.isAccessible incorrect

Fri, 12 Nov 2010 12:32:43 +0000

author
mcimadamore
date
Fri, 12 Nov 2010 12:32:43 +0000
changeset 741
58ceeff50af8
parent 740
bce19889597e
child 742
fdc67f5170e9

6598108: com.sun.source.util.Trees.isAccessible incorrect
Summary: JavacTrees' version of isAccessible should take into account enclosing class accessibility
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/api/JavacTrees.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/comp/Resolve.java file | annotate | diff | comparison | revisions
test/tools/javac/api/6598108/T6598108.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Wed Nov 10 12:37:25 2010 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Fri Nov 12 12:32:43 2010 +0000
     1.3 @@ -49,6 +49,7 @@
     1.4  import com.sun.tools.javac.code.Symbol.ClassSymbol;
     1.5  import com.sun.tools.javac.code.Symbol.TypeSymbol;
     1.6  import com.sun.tools.javac.code.Symbol;
     1.7 +import com.sun.tools.javac.code.Type;
     1.8  import com.sun.tools.javac.comp.Attr;
     1.9  import com.sun.tools.javac.comp.AttrContext;
    1.10  import com.sun.tools.javac.comp.Enter;
    1.11 @@ -230,7 +231,7 @@
    1.12      public boolean isAccessible(Scope scope, TypeElement type) {
    1.13          if (scope instanceof JavacScope && type instanceof ClassSymbol) {
    1.14              Env<AttrContext> env = ((JavacScope) scope).env;
    1.15 -            return resolve.isAccessible(env, (ClassSymbol)type);
    1.16 +            return resolve.isAccessible(env, (ClassSymbol)type, true);
    1.17          } else
    1.18              return false;
    1.19      }
    1.20 @@ -240,7 +241,7 @@
    1.21                  && member instanceof Symbol
    1.22                  && type instanceof com.sun.tools.javac.code.Type) {
    1.23              Env<AttrContext> env = ((JavacScope) scope).env;
    1.24 -            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member);
    1.25 +            return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
    1.26          } else
    1.27              return false;
    1.28      }
     2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Nov 10 12:37:25 2010 +0000
     2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Fri Nov 12 12:32:43 2010 +0000
     2.3 @@ -159,33 +159,45 @@
     2.4       *  @param c      The class whose accessibility is checked.
     2.5       */
     2.6      public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
     2.7 +        return isAccessible(env, c, false);
     2.8 +    }
     2.9 +
    2.10 +    public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
    2.11 +        boolean isAccessible = false;
    2.12          switch ((short)(c.flags() & AccessFlags)) {
    2.13 -        case PRIVATE:
    2.14 -            return
    2.15 -                env.enclClass.sym.outermostClass() ==
    2.16 -                c.owner.outermostClass();
    2.17 -        case 0:
    2.18 -            return
    2.19 -                env.toplevel.packge == c.owner // fast special case
    2.20 -                ||
    2.21 -                env.toplevel.packge == c.packge()
    2.22 -                ||
    2.23 -                // Hack: this case is added since synthesized default constructors
    2.24 -                // of anonymous classes should be allowed to access
    2.25 -                // classes which would be inaccessible otherwise.
    2.26 -                env.enclMethod != null &&
    2.27 -                (env.enclMethod.mods.flags & ANONCONSTR) != 0;
    2.28 -        default: // error recovery
    2.29 -        case PUBLIC:
    2.30 -            return true;
    2.31 -        case PROTECTED:
    2.32 -            return
    2.33 -                env.toplevel.packge == c.owner // fast special case
    2.34 -                ||
    2.35 -                env.toplevel.packge == c.packge()
    2.36 -                ||
    2.37 -                isInnerSubClass(env.enclClass.sym, c.owner);
    2.38 +            case PRIVATE:
    2.39 +                isAccessible =
    2.40 +                    env.enclClass.sym.outermostClass() ==
    2.41 +                    c.owner.outermostClass();
    2.42 +                break;
    2.43 +            case 0:
    2.44 +                isAccessible =
    2.45 +                    env.toplevel.packge == c.owner // fast special case
    2.46 +                    ||
    2.47 +                    env.toplevel.packge == c.packge()
    2.48 +                    ||
    2.49 +                    // Hack: this case is added since synthesized default constructors
    2.50 +                    // of anonymous classes should be allowed to access
    2.51 +                    // classes which would be inaccessible otherwise.
    2.52 +                    env.enclMethod != null &&
    2.53 +                    (env.enclMethod.mods.flags & ANONCONSTR) != 0;
    2.54 +                break;
    2.55 +            default: // error recovery
    2.56 +            case PUBLIC:
    2.57 +                isAccessible = true;
    2.58 +                break;
    2.59 +            case PROTECTED:
    2.60 +                isAccessible =
    2.61 +                    env.toplevel.packge == c.owner // fast special case
    2.62 +                    ||
    2.63 +                    env.toplevel.packge == c.packge()
    2.64 +                    ||
    2.65 +                    isInnerSubClass(env.enclClass.sym, c.owner);
    2.66 +                break;
    2.67          }
    2.68 +        return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
    2.69 +            isAccessible :
    2.70 +            isAccessible & isAccessible(env, c.type.getEnclosingType(), checkInner);
    2.71      }
    2.72      //where
    2.73          /** Is given class a subclass of given base class, or an inner class
    2.74 @@ -202,9 +214,13 @@
    2.75          }
    2.76  
    2.77      boolean isAccessible(Env<AttrContext> env, Type t) {
    2.78 +        return isAccessible(env, t, false);
    2.79 +    }
    2.80 +
    2.81 +    boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
    2.82          return (t.tag == ARRAY)
    2.83              ? isAccessible(env, types.elemtype(t))
    2.84 -            : isAccessible(env, t.tsym);
    2.85 +            : isAccessible(env, t.tsym, checkInner);
    2.86      }
    2.87  
    2.88      /** Is symbol accessible as a member of given type in given evironment?
    2.89 @@ -214,6 +230,9 @@
    2.90       *  @param sym    The symbol.
    2.91       */
    2.92      public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
    2.93 +        return isAccessible(env, site, sym, false);
    2.94 +    }
    2.95 +    public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
    2.96          if (sym.name == names.init && sym.owner != site.tsym) return false;
    2.97          ClassSymbol sub;
    2.98          switch ((short)(sym.flags() & AccessFlags)) {
    2.99 @@ -231,7 +250,7 @@
   2.100                   ||
   2.101                   env.toplevel.packge == sym.packge())
   2.102                  &&
   2.103 -                isAccessible(env, site)
   2.104 +                isAccessible(env, site, checkInner)
   2.105                  &&
   2.106                  sym.isInheritedIn(site.tsym, types)
   2.107                  &&
   2.108 @@ -248,11 +267,11 @@
   2.109                   // (but type names should be disallowed elsewhere!)
   2.110                   env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
   2.111                  &&
   2.112 -                isAccessible(env, site)
   2.113 +                isAccessible(env, site, checkInner)
   2.114                  &&
   2.115                  notOverriddenIn(site, sym);
   2.116          default: // this case includes erroneous combinations as well
   2.117 -            return isAccessible(env, site) && notOverriddenIn(site, sym);
   2.118 +            return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
   2.119          }
   2.120      }
   2.121      //where
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/api/6598108/T6598108.java	Fri Nov 12 12:32:43 2010 +0000
     3.3 @@ -0,0 +1,71 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug     6598108
    3.30 + * @summary com.sun.source.util.Trees.isAccessible incorrect
    3.31 + * @author  Jan Lahoda
    3.32 + */
    3.33 +
    3.34 +import com.sun.source.tree.CompilationUnitTree;
    3.35 +import com.sun.source.tree.Scope;
    3.36 +import com.sun.source.util.JavacTask;
    3.37 +import com.sun.source.util.TreePath;
    3.38 +import com.sun.source.util.Trees;
    3.39 +import java.net.URI;
    3.40 +import java.util.Arrays;
    3.41 +import javax.lang.model.element.TypeElement;
    3.42 +import javax.tools.JavaCompiler;
    3.43 +import javax.tools.JavaFileObject;
    3.44 +import javax.tools.SimpleJavaFileObject;
    3.45 +import javax.tools.ToolProvider;
    3.46 +
    3.47 +public class T6598108 {
    3.48 +    public static void main(String[] args) throws Exception {
    3.49 +        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
    3.50 +        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    3.51 +        assert tool != null;
    3.52 +        final JavacTask ct = (JavacTask)tool.getTask(null, null, null, Arrays.asList("-bootclasspath",  bootPath), null, Arrays.asList(new MyFileObject()));
    3.53 +
    3.54 +        CompilationUnitTree cut = ct.parse().iterator().next();
    3.55 +        TreePath tp = new TreePath(new TreePath(cut), cut.getTypeDecls().get(0));
    3.56 +        Scope s = Trees.instance(ct).getScope(tp);
    3.57 +        TypeElement type = ct.getElements().getTypeElement("com.sun.java.util.jar.pack.Package.File");
    3.58 +
    3.59 +        if (Trees.instance(ct).isAccessible(s, type)) {
    3.60 +            //com.sun.java.util.jar.pack.Package.File is a public innerclass inside a non-accessible class, so
    3.61 +            //"false" would be expected here.
    3.62 +            throw new IllegalStateException("");
    3.63 +        }
    3.64 +    }
    3.65 +
    3.66 +    static class MyFileObject extends SimpleJavaFileObject {
    3.67 +        public MyFileObject() {
    3.68 +            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
    3.69 +        }
    3.70 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
    3.71 +            return "public class Test<TTT> { public void test() {TTT ttt;}}";
    3.72 +        }
    3.73 +    }
    3.74 +}

mercurial