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