Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/tools/javac/6402516/CheckIsAccessible.java
41149 views
1
/*
2
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 6402516
27
* @summary need Trees.getScope(TreePath)
28
* @modules jdk.compiler/com.sun.tools.javac.api
29
* jdk.compiler/com.sun.tools.javac.comp
30
* jdk.compiler/com.sun.tools.javac.file
31
* jdk.compiler/com.sun.tools.javac.tree
32
* jdk.compiler/com.sun.tools.javac.util
33
* @build Checker CheckIsAccessible
34
* @run main CheckIsAccessible
35
*/
36
37
import java.util.*;
38
import com.sun.source.tree.*;
39
import com.sun.source.util.*;
40
import javax.lang.model.element.*;
41
import javax.lang.model.type.*;
42
import javax.lang.model.util.*;
43
44
/*
45
* Check the accessibility of items of a scope against the contents of string literals.
46
*/
47
public class CheckIsAccessible extends Checker {
48
public static void main(String... args) throws Exception {
49
Checker chk = new CheckIsAccessible();
50
chk.check("TestIsAccessible.java", "A.java");
51
}
52
53
@Override
54
protected boolean check(Scope s, String ref) {
55
System.err.println("checkIsAccessible: " + s + " " + s.getEnclosingClass() + " " + ref);
56
if (ref.length() == 0)
57
return true;
58
59
Trees trees = getTrees();
60
String[] args = ref.split(" +", 3);
61
boolean expect = args[args.length - 1].equals("yes");
62
boolean actual;
63
switch (args.length) {
64
case 2:
65
TypeElement te = getTypeElement(args[0]);
66
actual = trees.isAccessible(s, te);
67
if (actual != expect)
68
error(s, ref, "accessible issue found: " + te + " " + actual);
69
break;
70
71
case 3:
72
DeclaredType site = getType(args[0]);
73
Element member = getMember(args[1]);
74
actual = trees.isAccessible(s, member, site);
75
if (actual != expect)
76
error(s, ref, "accessible issue found: " + member + "@" + site + " " + actual);
77
break;
78
79
default:
80
throw new IllegalArgumentException(ref);
81
}
82
83
return (actual == expect);
84
}
85
86
private TypeElement getTypeElement(String name) {
87
TypeElement te = getElements().getTypeElement(name);
88
if (te == null)
89
throw new IllegalArgumentException("can't find element " + name);
90
return te;
91
}
92
93
private DeclaredType getType(String name) {
94
return (DeclaredType)(getTypeElement(name).asType());
95
}
96
97
private Element getMember(String name) {
98
int sep = name.indexOf("#");
99
String tname = name.substring(0, sep);
100
String mname = name.substring(sep+1);
101
TypeElement te = getTypeElement(tname);
102
for (Element e: te.getEnclosedElements()) {
103
if (mname.contentEquals(e.getSimpleName()))
104
return e;
105
}
106
throw new IllegalArgumentException("can't find member " + mname + " in " + tname);
107
}
108
109
}
110
111