Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6402516/CheckIsAccessible.java
38813 views
/*1* Copyright (c) 2006, 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* @build Checker CheckIsAccessible28* @run main CheckIsAccessible29*/3031import java.util.*;32import com.sun.source.tree.*;33import com.sun.source.util.*;34import javax.lang.model.element.*;35import javax.lang.model.type.*;36import javax.lang.model.util.*;3738/*39* Check the accessibility of items of a scope against the contents of string literals.40*/41public class CheckIsAccessible extends Checker {42public static void main(String... args) throws Exception {43Checker chk = new CheckIsAccessible();44chk.check("TestIsAccessible.java", "A.java");45}4647@Override48protected boolean check(Scope s, String ref) {49System.err.println("checkIsAccessible: " + s + " " + s.getEnclosingClass() + " " + ref);50if (ref.length() == 0)51return true;5253Trees trees = getTrees();54String[] args = ref.split(" +", 3);55boolean expect = args[args.length - 1].equals("yes");56boolean actual;57switch (args.length) {58case 2:59TypeElement te = getTypeElement(args[0]);60actual = trees.isAccessible(s, te);61if (actual != expect)62error(s, ref, "accessible issue found: " + te + " " + actual);63break;6465case 3:66DeclaredType site = getType(args[0]);67Element member = getMember(args[1]);68actual = trees.isAccessible(s, member, site);69if (actual != expect)70error(s, ref, "accessible issue found: " + member + "@" + site + " " + actual);71break;7273default:74throw new IllegalArgumentException(ref);75}7677return (actual == expect);78}7980private TypeElement getTypeElement(String name) {81TypeElement te = getElements().getTypeElement(name);82if (te == null)83throw new IllegalArgumentException("can't find element " + name);84return te;85}8687private DeclaredType getType(String name) {88return (DeclaredType)(getTypeElement(name).asType());89}9091private Element getMember(String name) {92int sep = name.indexOf("#");93String tname = name.substring(0, sep);94String mname = name.substring(sep+1);95TypeElement te = getTypeElement(tname);96for (Element e: te.getEnclosedElements()) {97if (mname.contentEquals(e.getSimpleName()))98return e;99}100throw new IllegalArgumentException("can't find member " + mname + " in " + tname);101}102103}104105106