Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/tools/javac/6402516/Checker.java
41152 views
1
/*
2
* Copyright (c) 2006, 2014, 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
import java.io.*;
25
import java.util.*;
26
import javax.lang.model.util.*;
27
import javax.tools.*;
28
import com.sun.tools.javac.api.*;
29
import com.sun.source.tree.*;
30
import com.sun.source.util.*;
31
import com.sun.tools.javac.tree.JCTree;
32
import com.sun.tools.javac.tree.JCTree.*;
33
import com.sun.tools.javac.util.Position;
34
35
/*
36
* Abstract class to help check the scopes in a parsed source file.
37
* -- parse source file
38
* -- scan trees looking for string literals
39
* -- check the scope at that point against the string, using
40
* boolean check(Scope s, String ref)
41
*/
42
abstract class Checker {
43
// parse the source file and call check(scope, string) for each string literal found
44
void check(String... fileNames) throws IOException {
45
File testSrc = new File(System.getProperty("test.src"));
46
47
DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {
48
public void report(Diagnostic d) {
49
System.err.println(d);
50
if (d.getKind() == Diagnostic.Kind.ERROR)
51
errors = true;
52
new Exception().printStackTrace();
53
}
54
};
55
56
JavacTool tool = JavacTool.create();
57
try (StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null)) {
58
Iterable<? extends JavaFileObject> files =
59
fm.getJavaFileObjectsFromFiles(getFiles(testSrc, fileNames));
60
task = tool.getTask(null, fm, dl, null, null, files);
61
Iterable<? extends CompilationUnitTree> units = task.parse();
62
63
if (errors)
64
throw new AssertionError("errors occurred creating trees");
65
66
ScopeScanner s = new ScopeScanner();
67
for (CompilationUnitTree unit: units) {
68
TreePath p = new TreePath(unit);
69
s.scan(p, getTrees());
70
additionalChecks(getTrees(), unit);
71
}
72
task = null;
73
74
if (errors)
75
throw new AssertionError("errors occurred checking scopes");
76
}
77
}
78
79
// default impl: split ref at ";" and call checkLocal(scope, ref_segment) on scope and its enclosing scopes
80
protected boolean check(Scope s, String ref) {
81
// System.err.println("check scope: " + s);
82
// System.err.println("check ref: " + ref);
83
if (s == null && (ref == null || ref.trim().length() == 0))
84
return true;
85
86
if (s == null) {
87
error(s, ref, "scope missing");
88
return false;
89
}
90
91
if (ref == null) {
92
error(s, ref, "scope unexpected");
93
return false;
94
}
95
96
String local;
97
String encl;
98
int semi = ref.indexOf(';');
99
if (semi == -1) {
100
local = ref;
101
encl = null;
102
} else {
103
local = ref.substring(0, semi);
104
encl = ref.substring(semi + 1);
105
}
106
107
return checkLocal(s, local.trim())
108
& check(s.getEnclosingScope(), encl);
109
}
110
111
// override if using default check(Scope,String)
112
boolean checkLocal(Scope s, String ref) {
113
throw new IllegalStateException();
114
}
115
116
void additionalChecks(Trees trees, CompilationUnitTree topLevel) throws IOException {
117
}
118
119
void error(Scope s, String ref, String msg) {
120
System.err.println("Error: " + msg);
121
System.err.println("Scope: " + (s == null ? null : asList(s.getLocalElements())));
122
System.err.println("Expect: " + ref);
123
System.err.println("javac: " + (s == null ? null : ((JavacScope) s).getEnv()));
124
errors = true;
125
}
126
127
protected Elements getElements() {
128
return task.getElements();
129
}
130
131
protected Trees getTrees() {
132
return Trees.instance(task);
133
}
134
135
boolean errors = false;
136
protected JavacTask task;
137
138
// scan a parse tree, and for every string literal found, call check(scope, string) with
139
// the string value at the scope at that point
140
class ScopeScanner extends TreePathScanner<Boolean,Trees> {
141
public Boolean visitLiteral(LiteralTree tree, Trees trees) {
142
TreePath path = getCurrentPath();
143
CompilationUnitTree unit = path.getCompilationUnit();
144
Position.LineMap lineMap = ((JCCompilationUnit)unit).lineMap;
145
// long line = lineMap.getLineNumber(((JCTree)tree).pos/*trees.getSourcePositions().getStartPosition(tree)*/);
146
// System.err.println(line + ": " + abbrev(tree));
147
Scope s = trees.getScope(path);
148
if (tree.getKind() == Tree.Kind.STRING_LITERAL)
149
check(s, tree.getValue().toString().trim());
150
return null;
151
}
152
153
private String abbrev(Tree tree) {
154
int max = 48;
155
String s = tree.toString().replaceAll("[ \n]+", " ");
156
return (s.length() < max ? s : s.substring(0, max-3) + "...");
157
}
158
}
159
160
// prefix filenames with a directory
161
static Iterable<File> getFiles(File dir, String... names) {
162
List<File> files = new ArrayList<File>(names.length);
163
for (String name: names)
164
files.add(new File(dir, name));
165
return files;
166
}
167
168
static private <T> List<T> asList(Iterable<T> iter) {
169
List<T> l = new ArrayList<T>();
170
for (T t: iter)
171
l.add(t);
172
return l;
173
}
174
}
175
176