author | rfield |
Tue, 29 Dec 2015 21:27:25 -0800 | |
changeset 34857 | 14d1224cfed3 |
parent 34092 | fd97b0f6abcc |
child 35000 | 952a7b4652f0 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 2014, 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jshell; |
|
27 |
||
28 |
import com.sun.source.tree.CompilationUnitTree; |
|
29 |
import com.sun.source.tree.Tree; |
|
30 |
import com.sun.source.util.Trees; |
|
31 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
32 |
import com.sun.tools.javac.api.JavacTool; |
|
33 |
import com.sun.tools.javac.util.Context; |
|
34 |
import java.util.ArrayList; |
|
35 |
import java.util.Arrays; |
|
36 |
import java.util.Iterator; |
|
37 |
import java.util.List; |
|
38 |
import javax.tools.Diagnostic; |
|
39 |
import javax.tools.DiagnosticCollector; |
|
40 |
import javax.tools.JavaCompiler; |
|
41 |
import javax.tools.JavaFileManager; |
|
42 |
import javax.tools.JavaFileObject; |
|
43 |
import javax.tools.ToolProvider; |
|
44 |
import static jdk.jshell.Util.*; |
|
45 |
import com.sun.source.tree.ImportTree; |
|
46 |
import com.sun.tools.javac.code.Types; |
|
47 |
import com.sun.tools.javac.util.JavacMessages; |
|
48 |
import jdk.jshell.MemoryFileManager.OutputMemoryJavaFileObject; |
|
49 |
import java.util.Collections; |
|
50 |
import java.util.Locale; |
|
51 |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
|
52 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; |
|
53 |
import java.io.File; |
|
54 |
import java.util.Collection; |
|
55 |
import java.util.HashMap; |
|
56 |
import java.util.LinkedHashMap; |
|
57 |
import java.util.Map; |
|
58 |
import java.util.stream.Collectors; |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
59 |
import static java.util.stream.Collectors.toList; |
33362 | 60 |
import java.util.stream.Stream; |
61 |
import javax.lang.model.util.Elements; |
|
62 |
import javax.tools.FileObject; |
|
63 |
import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject; |
|
64 |
import jdk.jshell.ClassTracker.ClassInfo; |
|
65 |
||
66 |
/** |
|
67 |
* The primary interface to the compiler API. Parsing, analysis, and |
|
68 |
* compilation to class files (in memory). |
|
69 |
* @author Robert Field |
|
70 |
*/ |
|
71 |
class TaskFactory { |
|
72 |
||
73 |
private final JavaCompiler compiler; |
|
74 |
private final MemoryFileManager fileManager; |
|
75 |
private final JShell state; |
|
76 |
private String classpath = System.getProperty("java.class.path"); |
|
77 |
||
78 |
TaskFactory(JShell state) { |
|
79 |
this.state = state; |
|
80 |
this.compiler = ToolProvider.getSystemJavaCompiler(); |
|
81 |
if (compiler == null) { |
|
82 |
throw new UnsupportedOperationException("Compiler not available, must be run with full JDK 9."); |
|
83 |
} |
|
34092
fd97b0f6abcc
8141104: jdk.jshell.TaskFactory code for java.specification.version = 1.9 should be adjusted for Verona
iris
parents:
33362
diff
changeset
|
84 |
if (!System.getProperty("java.specification.version").equals("9")) { |
33362 | 85 |
throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9."); |
86 |
} |
|
87 |
this.fileManager = new MemoryFileManager( |
|
88 |
compiler.getStandardFileManager(null, null, null), state); |
|
89 |
} |
|
90 |
||
91 |
void addToClasspath(String path) { |
|
92 |
classpath = classpath + File.pathSeparator + path; |
|
93 |
List<String> args = new ArrayList<>(); |
|
94 |
args.add(classpath); |
|
95 |
fileManager().handleOption("-classpath", args.iterator()); |
|
96 |
} |
|
97 |
||
98 |
MemoryFileManager fileManager() { |
|
99 |
return fileManager; |
|
100 |
} |
|
101 |
||
102 |
private interface SourceHandler<T> { |
|
103 |
||
104 |
JavaFileObject sourceToFileObject(MemoryFileManager fm, T t); |
|
105 |
||
106 |
Diag diag(Diagnostic<? extends JavaFileObject> d); |
|
107 |
} |
|
108 |
||
109 |
private class StringSourceHandler implements SourceHandler<String> { |
|
110 |
||
111 |
@Override |
|
112 |
public JavaFileObject sourceToFileObject(MemoryFileManager fm, String src) { |
|
113 |
return fm.createSourceFileObject(src, "$NeverUsedName$", src); |
|
114 |
} |
|
115 |
||
116 |
@Override |
|
117 |
public Diag diag(final Diagnostic<? extends JavaFileObject> d) { |
|
118 |
return new Diag() { |
|
119 |
||
120 |
@Override |
|
121 |
public boolean isError() { |
|
122 |
return d.getKind() == Diagnostic.Kind.ERROR; |
|
123 |
} |
|
124 |
||
125 |
@Override |
|
126 |
public long getPosition() { |
|
127 |
return d.getPosition(); |
|
128 |
} |
|
129 |
||
130 |
@Override |
|
131 |
public long getStartPosition() { |
|
132 |
return d.getStartPosition(); |
|
133 |
} |
|
134 |
||
135 |
@Override |
|
136 |
public long getEndPosition() { |
|
137 |
return d.getEndPosition(); |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
141 |
public String getCode() { |
|
142 |
return d.getCode(); |
|
143 |
} |
|
144 |
||
145 |
@Override |
|
146 |
public String getMessage(Locale locale) { |
|
147 |
return expunge(d.getMessage(locale)); |
|
148 |
} |
|
149 |
||
150 |
@Override |
|
151 |
Unit unitOrNull() { |
|
152 |
return null; |
|
153 |
} |
|
154 |
}; |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
private class WrapSourceHandler implements SourceHandler<OuterWrap> { |
|
159 |
||
160 |
final OuterWrap wrap; |
|
161 |
||
162 |
WrapSourceHandler(OuterWrap wrap) { |
|
163 |
this.wrap = wrap; |
|
164 |
} |
|
165 |
||
166 |
@Override |
|
167 |
public JavaFileObject sourceToFileObject(MemoryFileManager fm, OuterWrap w) { |
|
168 |
return fm.createSourceFileObject(w, w.classFullName(), w.wrapped()); |
|
169 |
} |
|
170 |
||
171 |
@Override |
|
172 |
public Diag diag(Diagnostic<? extends JavaFileObject> d) { |
|
173 |
return wrap.wrapDiag(d); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
private class UnitSourceHandler implements SourceHandler<Unit> { |
|
178 |
||
179 |
@Override |
|
180 |
public JavaFileObject sourceToFileObject(MemoryFileManager fm, Unit u) { |
|
181 |
return fm.createSourceFileObject(u, |
|
182 |
state.maps.classFullName(u.snippet()), |
|
183 |
u.snippet().outerWrap().wrapped()); |
|
184 |
} |
|
185 |
||
186 |
@Override |
|
187 |
public Diag diag(Diagnostic<? extends JavaFileObject> d) { |
|
188 |
SourceMemoryJavaFileObject smjfo = (SourceMemoryJavaFileObject) d.getSource(); |
|
189 |
Unit u = (Unit) smjfo.getOrigin(); |
|
190 |
return u.snippet().outerWrap().wrapDiag(d); |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Parse a snippet of code (as a String) using the parser subclass. Return |
|
196 |
* the parse tree (and errors). |
|
197 |
*/ |
|
198 |
class ParseTask extends BaseTask { |
|
199 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
200 |
private final Iterable<? extends CompilationUnitTree> cuts; |
33362 | 201 |
private final List<? extends Tree> units; |
202 |
||
203 |
ParseTask(final String source) { |
|
204 |
super(Stream.of(source), |
|
205 |
new StringSourceHandler(), |
|
206 |
"-XDallowStringFolding=false", "-proc:none"); |
|
207 |
ReplParserFactory.instance(getContext()); |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
208 |
cuts = parse(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
209 |
units = Util.stream(cuts) |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
210 |
.flatMap(cut -> { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
211 |
List<? extends ImportTree> imps = cut.getImports(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
212 |
return (!imps.isEmpty() ? imps : cut.getTypeDecls()).stream(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
213 |
}) |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
214 |
.collect(toList()); |
33362 | 215 |
} |
216 |
||
217 |
private Iterable<? extends CompilationUnitTree> parse() { |
|
218 |
try { |
|
219 |
return task.parse(); |
|
220 |
} catch (Exception ex) { |
|
221 |
throw new InternalError("Exception during parse - " + ex.getMessage(), ex); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
List<? extends Tree> units() { |
|
226 |
return units; |
|
227 |
} |
|
228 |
||
229 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
230 |
Iterable<? extends CompilationUnitTree> cuTrees() { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
231 |
return cuts; |
33362 | 232 |
} |
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* Run the normal "analyze()" pass of the compiler over the wrapped snippet. |
|
237 |
*/ |
|
238 |
class AnalyzeTask extends BaseTask { |
|
239 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
240 |
private final Iterable<? extends CompilationUnitTree> cuts; |
33362 | 241 |
|
242 |
AnalyzeTask(final OuterWrap wrap) { |
|
243 |
this(Stream.of(wrap), |
|
244 |
new WrapSourceHandler(wrap), |
|
245 |
"-XDshouldStopPolicy=FLOW", "-proc:none"); |
|
246 |
} |
|
247 |
||
248 |
AnalyzeTask(final Collection<Unit> units) { |
|
249 |
this(units.stream(), new UnitSourceHandler(), |
|
250 |
"-XDshouldStopPolicy=FLOW", "-Xlint:unchecked", "-proc:none"); |
|
251 |
} |
|
252 |
||
253 |
<T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler, |
|
254 |
String... extraOptions) { |
|
255 |
super(stream, sourceHandler, extraOptions); |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
256 |
cuts = analyze(); |
33362 | 257 |
} |
258 |
||
259 |
private Iterable<? extends CompilationUnitTree> analyze() { |
|
260 |
try { |
|
261 |
Iterable<? extends CompilationUnitTree> cuts = task.parse(); |
|
262 |
task.analyze(); |
|
263 |
return cuts; |
|
264 |
} catch (Exception ex) { |
|
265 |
throw new InternalError("Exception during analyze - " + ex.getMessage(), ex); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
270 |
Iterable<? extends CompilationUnitTree> cuTrees() { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
271 |
return cuts; |
33362 | 272 |
} |
273 |
||
274 |
Elements getElements() { |
|
275 |
return task.getElements(); |
|
276 |
} |
|
277 |
||
278 |
javax.lang.model.util.Types getTypes() { |
|
279 |
return task.getTypes(); |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Unit the wrapped snippet to class files. |
|
285 |
*/ |
|
286 |
class CompileTask extends BaseTask { |
|
287 |
||
288 |
private final Map<Unit, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>(); |
|
289 |
||
290 |
CompileTask(Collection<Unit> units) { |
|
291 |
super(units.stream(), new UnitSourceHandler(), |
|
292 |
"-Xlint:unchecked", "-proc:none"); |
|
293 |
} |
|
294 |
||
295 |
boolean compile() { |
|
296 |
fileManager.registerClassFileCreationListener(this::listenForNewClassFile); |
|
297 |
boolean result = task.call(); |
|
298 |
fileManager.registerClassFileCreationListener(null); |
|
299 |
return result; |
|
300 |
} |
|
301 |
||
302 |
||
303 |
List<ClassInfo> classInfoList(Unit u) { |
|
304 |
List<OutputMemoryJavaFileObject> l = classObjs.get(u); |
|
305 |
if (l == null) return Collections.emptyList(); |
|
306 |
return l.stream() |
|
307 |
.map(fo -> state.classTracker.classInfo(fo.getName(), fo.getBytes())) |
|
308 |
.collect(Collectors.toList()); |
|
309 |
} |
|
310 |
||
311 |
private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location, |
|
312 |
String className, JavaFileObject.Kind kind, FileObject sibling) { |
|
313 |
//debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind); |
|
314 |
if (location == CLASS_OUTPUT) { |
|
315 |
state.debug(DBG_GEN, "Compiler generating class %s\n", className); |
|
316 |
Unit u = ((sibling instanceof SourceMemoryJavaFileObject) |
|
317 |
&& (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof Unit)) |
|
318 |
? (Unit) ((SourceMemoryJavaFileObject) sibling).getOrigin() |
|
319 |
: null; |
|
320 |
classObjs.compute(u, (k, v) -> (v == null)? new ArrayList<>() : v) |
|
321 |
.add(jfo); |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
326 |
Iterable<? extends CompilationUnitTree> cuTrees() { |
33362 | 327 |
throw new UnsupportedOperationException("Not supported."); |
328 |
} |
|
329 |
} |
|
330 |
||
331 |
abstract class BaseTask { |
|
332 |
||
333 |
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); |
|
334 |
final JavacTaskImpl task; |
|
335 |
private DiagList diags = null; |
|
336 |
private final SourceHandler<?> sourceHandler; |
|
337 |
private final Context context = new Context(); |
|
338 |
private Types types; |
|
339 |
private JavacMessages messages; |
|
340 |
private Trees trees; |
|
341 |
||
342 |
private <T>BaseTask(Stream<T> inputs, |
|
343 |
//BiFunction<MemoryFileManager, T, JavaFileObject> sfoCreator, |
|
344 |
SourceHandler<T> sh, |
|
345 |
String... extraOptions) { |
|
346 |
this.sourceHandler = sh; |
|
347 |
List<String> options = Arrays.asList(extraOptions); |
|
348 |
Iterable<? extends JavaFileObject> compilationUnits = inputs |
|
349 |
.map(in -> sh.sourceToFileObject(fileManager, in)) |
|
350 |
.collect(Collectors.toList()); |
|
351 |
this.task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, |
|
352 |
fileManager, diagnostics, options, null, |
|
353 |
compilationUnits, context); |
|
354 |
} |
|
355 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
356 |
abstract Iterable<? extends CompilationUnitTree> cuTrees(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
357 |
|
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
358 |
CompilationUnitTree firstCuTree() { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
359 |
return cuTrees().iterator().next(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
360 |
} |
33362 | 361 |
|
362 |
Diag diag(Diagnostic<? extends JavaFileObject> diag) { |
|
363 |
return sourceHandler.diag(diag); |
|
364 |
} |
|
365 |
||
366 |
Context getContext() { |
|
367 |
return context; |
|
368 |
} |
|
369 |
||
370 |
Types types() { |
|
371 |
if (types == null) { |
|
372 |
types = Types.instance(context); |
|
373 |
} |
|
374 |
return types; |
|
375 |
} |
|
376 |
||
377 |
JavacMessages messages() { |
|
378 |
if (messages == null) { |
|
379 |
messages = JavacMessages.instance(context); |
|
380 |
} |
|
381 |
return messages; |
|
382 |
} |
|
383 |
||
384 |
Trees trees() { |
|
385 |
if (trees == null) { |
|
386 |
trees = Trees.instance(task); |
|
387 |
} |
|
388 |
return trees; |
|
389 |
} |
|
390 |
||
391 |
// ------------------ diags functionality |
|
392 |
||
393 |
DiagList getDiagnostics() { |
|
394 |
if (diags == null) { |
|
395 |
LinkedHashMap<String, Diag> diagMap = new LinkedHashMap<>(); |
|
396 |
for (Diagnostic<? extends JavaFileObject> in : diagnostics.getDiagnostics()) { |
|
397 |
Diag d = diag(in); |
|
398 |
String uniqueKey = d.getCode() + ":" + d.getPosition() + ":" + d.getMessage(null); |
|
399 |
diagMap.put(uniqueKey, d); |
|
400 |
} |
|
401 |
diags = new DiagList(diagMap.values()); |
|
402 |
} |
|
403 |
return diags; |
|
404 |
} |
|
405 |
||
406 |
boolean hasErrors() { |
|
407 |
return getDiagnostics().hasErrors(); |
|
408 |
} |
|
409 |
||
410 |
String shortErrorMessage() { |
|
411 |
StringBuilder sb = new StringBuilder(); |
|
412 |
for (Diag diag : getDiagnostics()) { |
|
413 |
for (String line : diag.getMessage(null).split("\\r?\\n")) { |
|
414 |
if (!line.trim().startsWith("location:")) { |
|
415 |
sb.append(line); |
|
416 |
} |
|
417 |
} |
|
418 |
} |
|
419 |
return sb.toString(); |
|
420 |
} |
|
421 |
||
422 |
void debugPrintDiagnostics(String src) { |
|
423 |
for (Diag diag : getDiagnostics()) { |
|
424 |
state.debug(DBG_GEN, "ERROR --\n"); |
|
425 |
for (String line : diag.getMessage(null).split("\\r?\\n")) { |
|
426 |
if (!line.trim().startsWith("location:")) { |
|
427 |
state.debug(DBG_GEN, "%s\n", line); |
|
428 |
} |
|
429 |
} |
|
430 |
int start = (int) diag.getStartPosition(); |
|
431 |
int end = (int) diag.getEndPosition(); |
|
432 |
if (src != null) { |
|
433 |
String[] srcLines = src.split("\\r?\\n"); |
|
434 |
for (String line : srcLines) { |
|
435 |
state.debug(DBG_GEN, "%s\n", line); |
|
436 |
} |
|
437 |
||
438 |
StringBuilder sb = new StringBuilder(); |
|
439 |
for (int i = 0; i < start; ++i) { |
|
440 |
sb.append(' '); |
|
441 |
} |
|
442 |
sb.append('^'); |
|
443 |
if (end > start) { |
|
444 |
for (int i = start + 1; i < end; ++i) { |
|
445 |
sb.append('-'); |
|
446 |
} |
|
447 |
sb.append('^'); |
|
448 |
} |
|
449 |
state.debug(DBG_GEN, "%s\n", sb.toString()); |
|
450 |
} |
|
451 |
state.debug(DBG_GEN, "printDiagnostics start-pos = %d ==> %d -- wrap = %s\n", |
|
452 |
diag.getStartPosition(), start, this); |
|
453 |
state.debug(DBG_GEN, "Code: %s\n", diag.getCode()); |
|
454 |
state.debug(DBG_GEN, "Pos: %d (%d - %d) -- %s\n", diag.getPosition(), |
|
455 |
diag.getStartPosition(), diag.getEndPosition(), diag.getMessage(null)); |
|
456 |
} |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
} |