author | jlahoda |
Wed, 09 Jul 2014 16:32:05 +0200 | |
changeset 25443 | 9187d77f2c64 |
parent 5520 | 86e4b9a9da40 |
child 27319 | 030080f03e4f |
permissions | -rw-r--r-- |
10 | 1 |
/* |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
5520
diff
changeset
|
2 |
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 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 |
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()); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
5520
diff
changeset
|
70 |
additionalChecks(getTrees(), unit); |
10 | 71 |
} |
72 |
task = null; |
|
73 |
||
74 |
if (errors) |
|
75 |
throw new AssertionError("errors occurred checking scopes"); |
|
76 |
} |
|
77 |
||
78 |
// default impl: split ref at ";" and call checkLocal(scope, ref_segment) on scope and its enclosing scopes |
|
79 |
protected boolean check(Scope s, String ref) { |
|
80 |
// System.err.println("check scope: " + s); |
|
81 |
// System.err.println("check ref: " + ref); |
|
82 |
if (s == null && (ref == null || ref.trim().length() == 0)) |
|
83 |
return true; |
|
84 |
||
85 |
if (s == null) { |
|
86 |
error(s, ref, "scope missing"); |
|
87 |
return false; |
|
88 |
} |
|
89 |
||
90 |
if (ref == null) { |
|
91 |
error(s, ref, "scope unexpected"); |
|
92 |
return false; |
|
93 |
} |
|
94 |
||
95 |
String local; |
|
96 |
String encl; |
|
97 |
int semi = ref.indexOf(';'); |
|
98 |
if (semi == -1) { |
|
99 |
local = ref; |
|
100 |
encl = null; |
|
101 |
} else { |
|
102 |
local = ref.substring(0, semi); |
|
103 |
encl = ref.substring(semi + 1); |
|
104 |
} |
|
105 |
||
106 |
return checkLocal(s, local.trim()) |
|
107 |
& check(s.getEnclosingScope(), encl); |
|
108 |
} |
|
109 |
||
110 |
// override if using default check(Scope,String) |
|
111 |
boolean checkLocal(Scope s, String ref) { |
|
112 |
throw new IllegalStateException(); |
|
113 |
} |
|
114 |
||
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
5520
diff
changeset
|
115 |
void additionalChecks(Trees trees, CompilationUnitTree topLevel) throws IOException { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
5520
diff
changeset
|
116 |
} |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
5520
diff
changeset
|
117 |
|
10 | 118 |
void error(Scope s, String ref, String msg) { |
119 |
System.err.println("Error: " + msg); |
|
120 |
System.err.println("Scope: " + (s == null ? null : asList(s.getLocalElements()))); |
|
121 |
System.err.println("Expect: " + ref); |
|
122 |
System.err.println("javac: " + (s == null ? null : ((JavacScope) s).getEnv())); |
|
123 |
errors = true; |
|
124 |
} |
|
125 |
||
126 |
protected Elements getElements() { |
|
127 |
return task.getElements(); |
|
128 |
} |
|
129 |
||
130 |
protected Trees getTrees() { |
|
131 |
return Trees.instance(task); |
|
132 |
} |
|
133 |
||
134 |
boolean errors = false; |
|
135 |
protected JavacTask task; |
|
136 |
||
137 |
// scan a parse tree, and for every string literal found, call check(scope, string) with |
|
138 |
// the string value at the scope at that point |
|
139 |
class ScopeScanner extends TreePathScanner<Boolean,Trees> { |
|
140 |
public Boolean visitLiteral(LiteralTree tree, Trees trees) { |
|
141 |
TreePath path = getCurrentPath(); |
|
142 |
CompilationUnitTree unit = path.getCompilationUnit(); |
|
143 |
Position.LineMap lineMap = ((JCCompilationUnit)unit).lineMap; |
|
144 |
// long line = lineMap.getLineNumber(((JCTree)tree).pos/*trees.getSourcePositions().getStartPosition(tree)*/); |
|
145 |
// System.err.println(line + ": " + abbrev(tree)); |
|
146 |
Scope s = trees.getScope(path); |
|
147 |
if (tree.getKind() == Tree.Kind.STRING_LITERAL) |
|
148 |
check(s, tree.getValue().toString().trim()); |
|
149 |
return null; |
|
150 |
} |
|
151 |
||
152 |
private String abbrev(Tree tree) { |
|
153 |
int max = 48; |
|
154 |
String s = tree.toString().replaceAll("[ \n]+", " "); |
|
155 |
return (s.length() < max ? s : s.substring(0, max-3) + "..."); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
// prefix filenames with a directory |
|
160 |
static Iterable<File> getFiles(File dir, String... names) { |
|
161 |
List<File> files = new ArrayList<File>(names.length); |
|
162 |
for (String name: names) |
|
163 |
files.add(new File(dir, name)); |
|
164 |
return files; |
|
165 |
} |
|
166 |
||
167 |
static private <T> List<T> asList(Iterable<T> iter) { |
|
168 |
List<T> l = new ArrayList<T>(); |
|
169 |
for (T t: iter) |
|
170 |
l.add(t); |
|
171 |
return l; |
|
172 |
} |
|
173 |
} |