1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/6402516/CheckIsAccessible.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,104 @@ 1.4 +/* 1.5 + * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6402516 1.30 + * @summary need Trees.getScope(TreePath) 1.31 + * @build Checker CheckIsAccessible 1.32 + * @run main CheckIsAccessible 1.33 + */ 1.34 + 1.35 +import java.util.*; 1.36 +import com.sun.source.tree.*; 1.37 +import com.sun.source.util.*; 1.38 +import javax.lang.model.element.*; 1.39 +import javax.lang.model.type.*; 1.40 +import javax.lang.model.util.*; 1.41 + 1.42 +/* 1.43 + * Check the accessibility of items of a scope against the contents of string literals. 1.44 + */ 1.45 +public class CheckIsAccessible extends Checker { 1.46 + public static void main(String... args) throws Exception { 1.47 + Checker chk = new CheckIsAccessible(); 1.48 + chk.check("TestIsAccessible.java", "A.java"); 1.49 + } 1.50 + 1.51 + @Override 1.52 + protected boolean check(Scope s, String ref) { 1.53 + System.err.println("checkIsAccessible: " + s + " " + s.getEnclosingClass() + " " + ref); 1.54 + if (ref.length() == 0) 1.55 + return true; 1.56 + 1.57 + Trees trees = getTrees(); 1.58 + String[] args = ref.split(" +", 3); 1.59 + boolean expect = args[args.length - 1].equals("yes"); 1.60 + boolean actual; 1.61 + switch (args.length) { 1.62 + case 2: 1.63 + TypeElement te = getTypeElement(args[0]); 1.64 + actual = trees.isAccessible(s, te); 1.65 + if (actual != expect) 1.66 + error(s, ref, "accessible issue found: " + te + " " + actual); 1.67 + break; 1.68 + 1.69 + case 3: 1.70 + DeclaredType site = getType(args[0]); 1.71 + Element member = getMember(args[1]); 1.72 + actual = trees.isAccessible(s, member, site); 1.73 + if (actual != expect) 1.74 + error(s, ref, "accessible issue found: " + member + "@" + site + " " + actual); 1.75 + break; 1.76 + 1.77 + default: 1.78 + throw new IllegalArgumentException(ref); 1.79 + } 1.80 + 1.81 + return (actual == expect); 1.82 + } 1.83 + 1.84 + private TypeElement getTypeElement(String name) { 1.85 + TypeElement te = getElements().getTypeElement(name); 1.86 + if (te == null) 1.87 + throw new IllegalArgumentException("can't find element " + name); 1.88 + return te; 1.89 + } 1.90 + 1.91 + private DeclaredType getType(String name) { 1.92 + return (DeclaredType)(getTypeElement(name).asType()); 1.93 + } 1.94 + 1.95 + private Element getMember(String name) { 1.96 + int sep = name.indexOf("#"); 1.97 + String tname = name.substring(0, sep); 1.98 + String mname = name.substring(sep+1); 1.99 + TypeElement te = getTypeElement(tname); 1.100 + for (Element e: te.getEnclosedElements()) { 1.101 + if (mname.contentEquals(e.getSimpleName())) 1.102 + return e; 1.103 + } 1.104 + throw new IllegalArgumentException("can't find member " + mname + " in " + tname); 1.105 + } 1.106 + 1.107 +}