Path: blob/master/test/langtools/tools/javac/6402516/CheckIsAccessible.java
41149 views
/*1* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 640251626* @summary need Trees.getScope(TreePath)27* @modules jdk.compiler/com.sun.tools.javac.api28* jdk.compiler/com.sun.tools.javac.comp29* jdk.compiler/com.sun.tools.javac.file30* jdk.compiler/com.sun.tools.javac.tree31* jdk.compiler/com.sun.tools.javac.util32* @build Checker CheckIsAccessible33* @run main CheckIsAccessible34*/3536import java.util.*;37import com.sun.source.tree.*;38import com.sun.source.util.*;39import javax.lang.model.element.*;40import javax.lang.model.type.*;41import javax.lang.model.util.*;4243/*44* Check the accessibility of items of a scope against the contents of string literals.45*/46public class CheckIsAccessible extends Checker {47public static void main(String... args) throws Exception {48Checker chk = new CheckIsAccessible();49chk.check("TestIsAccessible.java", "A.java");50}5152@Override53protected boolean check(Scope s, String ref) {54System.err.println("checkIsAccessible: " + s + " " + s.getEnclosingClass() + " " + ref);55if (ref.length() == 0)56return true;5758Trees trees = getTrees();59String[] args = ref.split(" +", 3);60boolean expect = args[args.length - 1].equals("yes");61boolean actual;62switch (args.length) {63case 2:64TypeElement te = getTypeElement(args[0]);65actual = trees.isAccessible(s, te);66if (actual != expect)67error(s, ref, "accessible issue found: " + te + " " + actual);68break;6970case 3:71DeclaredType site = getType(args[0]);72Element member = getMember(args[1]);73actual = trees.isAccessible(s, member, site);74if (actual != expect)75error(s, ref, "accessible issue found: " + member + "@" + site + " " + actual);76break;7778default:79throw new IllegalArgumentException(ref);80}8182return (actual == expect);83}8485private TypeElement getTypeElement(String name) {86TypeElement te = getElements().getTypeElement(name);87if (te == null)88throw new IllegalArgumentException("can't find element " + name);89return te;90}9192private DeclaredType getType(String name) {93return (DeclaredType)(getTypeElement(name).asType());94}9596private Element getMember(String name) {97int sep = name.indexOf("#");98String tname = name.substring(0, sep);99String mname = name.substring(sep+1);100TypeElement te = getTypeElement(tname);101for (Element e: te.getEnclosedElements()) {102if (mname.contentEquals(e.getSimpleName()))103return e;104}105throw new IllegalArgumentException("can't find member " + mname + " in " + tname);106}107108}109110111