0N/A/*
553N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6402516
0N/A * @summary need Trees.getScope(TreePath)
0N/A * @build Checker CheckIsAccessible
0N/A * @run main CheckIsAccessible
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport com.sun.source.tree.*;
0N/Aimport com.sun.source.util.*;
0N/Aimport javax.lang.model.element.*;
0N/Aimport javax.lang.model.type.*;
0N/Aimport javax.lang.model.util.*;
0N/A
0N/A/*
0N/A * Check the accessibility of items of a scope against the contents of string literals.
0N/A */
0N/Apublic class CheckIsAccessible extends Checker {
0N/A public static void main(String... args) throws Exception {
0N/A Checker chk = new CheckIsAccessible();
0N/A chk.check("TestIsAccessible.java", "A.java");
0N/A }
0N/A
0N/A @Override
0N/A protected boolean check(Scope s, String ref) {
0N/A System.err.println("checkIsAccessible: " + s + " " + s.getEnclosingClass() + " " + ref);
0N/A if (ref.length() == 0)
0N/A return true;
0N/A
0N/A Trees trees = getTrees();
0N/A String[] args = ref.split(" +", 3);
0N/A boolean expect = args[args.length - 1].equals("yes");
0N/A boolean actual;
0N/A switch (args.length) {
0N/A case 2:
0N/A TypeElement te = getTypeElement(args[0]);
0N/A actual = trees.isAccessible(s, te);
0N/A if (actual != expect)
0N/A error(s, ref, "accessible issue found: " + te + " " + actual);
0N/A break;
0N/A
0N/A case 3:
0N/A DeclaredType site = getType(args[0]);
0N/A Element member = getMember(args[1]);
0N/A actual = trees.isAccessible(s, member, site);
0N/A if (actual != expect)
0N/A error(s, ref, "accessible issue found: " + member + "@" + site + " " + actual);
0N/A break;
0N/A
0N/A default:
0N/A throw new IllegalArgumentException(ref);
0N/A }
0N/A
0N/A return (actual == expect);
0N/A }
0N/A
0N/A private TypeElement getTypeElement(String name) {
0N/A TypeElement te = getElements().getTypeElement(name);
0N/A if (te == null)
0N/A throw new IllegalArgumentException("can't find element " + name);
0N/A return te;
0N/A }
0N/A
0N/A private DeclaredType getType(String name) {
0N/A return (DeclaredType)(getTypeElement(name).asType());
0N/A }
0N/A
0N/A private Element getMember(String name) {
0N/A int sep = name.indexOf("#");
0N/A String tname = name.substring(0, sep);
0N/A String mname = name.substring(sep+1);
0N/A TypeElement te = getTypeElement(tname);
0N/A for (Element e: te.getEnclosedElements()) {
0N/A if (mname.contentEquals(e.getSimpleName()))
0N/A return e;
0N/A }
0N/A throw new IllegalArgumentException("can't find member " + mname + " in " + tname);
0N/A }
0N/A
0N/A}