test/tools/javac/6402516/CheckIsAccessible.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

duke@1 1 /*
ohair@554 2 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
duke@1 22 */
duke@1 23
duke@1 24 /*
duke@1 25 * @test
duke@1 26 * @bug 6402516
duke@1 27 * @summary need Trees.getScope(TreePath)
duke@1 28 * @build Checker CheckIsAccessible
duke@1 29 * @run main CheckIsAccessible
duke@1 30 */
duke@1 31
duke@1 32 import java.util.*;
duke@1 33 import com.sun.source.tree.*;
duke@1 34 import com.sun.source.util.*;
duke@1 35 import javax.lang.model.element.*;
duke@1 36 import javax.lang.model.type.*;
duke@1 37 import javax.lang.model.util.*;
duke@1 38
duke@1 39 /*
duke@1 40 * Check the accessibility of items of a scope against the contents of string literals.
duke@1 41 */
duke@1 42 public class CheckIsAccessible extends Checker {
duke@1 43 public static void main(String... args) throws Exception {
duke@1 44 Checker chk = new CheckIsAccessible();
duke@1 45 chk.check("TestIsAccessible.java", "A.java");
duke@1 46 }
duke@1 47
duke@1 48 @Override
duke@1 49 protected boolean check(Scope s, String ref) {
duke@1 50 System.err.println("checkIsAccessible: " + s + " " + s.getEnclosingClass() + " " + ref);
duke@1 51 if (ref.length() == 0)
duke@1 52 return true;
duke@1 53
duke@1 54 Trees trees = getTrees();
duke@1 55 String[] args = ref.split(" +", 3);
duke@1 56 boolean expect = args[args.length - 1].equals("yes");
duke@1 57 boolean actual;
duke@1 58 switch (args.length) {
duke@1 59 case 2:
duke@1 60 TypeElement te = getTypeElement(args[0]);
duke@1 61 actual = trees.isAccessible(s, te);
duke@1 62 if (actual != expect)
duke@1 63 error(s, ref, "accessible issue found: " + te + " " + actual);
duke@1 64 break;
duke@1 65
duke@1 66 case 3:
duke@1 67 DeclaredType site = getType(args[0]);
duke@1 68 Element member = getMember(args[1]);
duke@1 69 actual = trees.isAccessible(s, member, site);
duke@1 70 if (actual != expect)
duke@1 71 error(s, ref, "accessible issue found: " + member + "@" + site + " " + actual);
duke@1 72 break;
duke@1 73
duke@1 74 default:
duke@1 75 throw new IllegalArgumentException(ref);
duke@1 76 }
duke@1 77
duke@1 78 return (actual == expect);
duke@1 79 }
duke@1 80
duke@1 81 private TypeElement getTypeElement(String name) {
duke@1 82 TypeElement te = getElements().getTypeElement(name);
duke@1 83 if (te == null)
duke@1 84 throw new IllegalArgumentException("can't find element " + name);
duke@1 85 return te;
duke@1 86 }
duke@1 87
duke@1 88 private DeclaredType getType(String name) {
duke@1 89 return (DeclaredType)(getTypeElement(name).asType());
duke@1 90 }
duke@1 91
duke@1 92 private Element getMember(String name) {
duke@1 93 int sep = name.indexOf("#");
duke@1 94 String tname = name.substring(0, sep);
duke@1 95 String mname = name.substring(sep+1);
duke@1 96 TypeElement te = getTypeElement(tname);
duke@1 97 for (Element e: te.getEnclosedElements()) {
duke@1 98 if (mname.contentEquals(e.getSimpleName()))
duke@1 99 return e;
duke@1 100 }
duke@1 101 throw new IllegalArgumentException("can't find member " + mname + " in " + tname);
duke@1 102 }
duke@1 103
duke@1 104 }

mercurial