author | rfield |
Mon, 09 Jan 2017 18:04:16 -0800 | |
changeset 43134 | 006808ae5f6e |
parent 43038 | 7b8b8750a78e |
child 43586 | cc7a4eb79b29 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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.List; |
|
37 |
import javax.tools.Diagnostic; |
|
38 |
import javax.tools.DiagnosticCollector; |
|
39 |
import javax.tools.JavaCompiler; |
|
40 |
import javax.tools.JavaFileManager; |
|
41 |
import javax.tools.JavaFileObject; |
|
42 |
import javax.tools.ToolProvider; |
|
43 |
import static jdk.jshell.Util.*; |
|
44 |
import com.sun.source.tree.ImportTree; |
|
45 |
import com.sun.tools.javac.code.Types; |
|
46 |
import com.sun.tools.javac.util.JavacMessages; |
|
47 |
import jdk.jshell.MemoryFileManager.OutputMemoryJavaFileObject; |
|
48 |
import java.util.Collections; |
|
49 |
import java.util.Locale; |
|
50 |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
|
51 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; |
|
52 |
import java.io.File; |
|
53 |
import java.util.Collection; |
|
54 |
import java.util.HashMap; |
|
55 |
import java.util.LinkedHashMap; |
|
56 |
import java.util.Map; |
|
57 |
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
|
58 |
import static java.util.stream.Collectors.toList; |
33362 | 59 |
import java.util.stream.Stream; |
60 |
import javax.lang.model.util.Elements; |
|
61 |
import javax.tools.FileObject; |
|
62 |
import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject; |
|
38526
6f5838874afc
8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents:
37939
diff
changeset
|
63 |
import java.lang.Runtime.Version; |
33362 | 64 |
|
65 |
/** |
|
66 |
* The primary interface to the compiler API. Parsing, analysis, and |
|
67 |
* compilation to class files (in memory). |
|
68 |
* @author Robert Field |
|
69 |
*/ |
|
70 |
class TaskFactory { |
|
71 |
||
72 |
private final JavaCompiler compiler; |
|
73 |
private final MemoryFileManager fileManager; |
|
74 |
private final JShell state; |
|
75 |
private String classpath = System.getProperty("java.class.path"); |
|
36497
9c4840131512
8150632: jdk.jshell.TaskFactory should use jdk.Version to check for java.specification.version
simonis
parents:
35000
diff
changeset
|
76 |
private final static Version INITIAL_SUPPORTED_VER = Version.parse("9"); |
33362 | 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 |
} |
|
36497
9c4840131512
8150632: jdk.jshell.TaskFactory should use jdk.Version to check for java.specification.version
simonis
parents:
35000
diff
changeset
|
84 |
Version current = Version.parse(System.getProperty("java.specification.version")); |
39809
74b7aea686d8
8161236: Runtime.Version.{compareTo, equals}IgnoreOpt should be renamed
iris
parents:
39807
diff
changeset
|
85 |
if (INITIAL_SUPPORTED_VER.compareToIgnoreOptional(current) > 0) { |
33362 | 86 |
throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9."); |
87 |
} |
|
88 |
this.fileManager = new MemoryFileManager( |
|
89 |
compiler.getStandardFileManager(null, null, null), state); |
|
90 |
} |
|
91 |
||
92 |
void addToClasspath(String path) { |
|
93 |
classpath = classpath + File.pathSeparator + path; |
|
94 |
List<String> args = new ArrayList<>(); |
|
95 |
args.add(classpath); |
|
96 |
fileManager().handleOption("-classpath", args.iterator()); |
|
97 |
} |
|
98 |
||
99 |
MemoryFileManager fileManager() { |
|
100 |
return fileManager; |
|
101 |
} |
|
102 |
||
103 |
private interface SourceHandler<T> { |
|
104 |
||
105 |
JavaFileObject sourceToFileObject(MemoryFileManager fm, T t); |
|
106 |
||
107 |
Diag diag(Diagnostic<? extends JavaFileObject> d); |
|
108 |
} |
|
109 |
||
110 |
private class StringSourceHandler implements SourceHandler<String> { |
|
111 |
||
112 |
@Override |
|
113 |
public JavaFileObject sourceToFileObject(MemoryFileManager fm, String src) { |
|
114 |
return fm.createSourceFileObject(src, "$NeverUsedName$", src); |
|
115 |
} |
|
116 |
||
117 |
@Override |
|
118 |
public Diag diag(final Diagnostic<? extends JavaFileObject> d) { |
|
119 |
return new Diag() { |
|
120 |
||
121 |
@Override |
|
122 |
public boolean isError() { |
|
123 |
return d.getKind() == Diagnostic.Kind.ERROR; |
|
124 |
} |
|
125 |
||
126 |
@Override |
|
127 |
public long getPosition() { |
|
128 |
return d.getPosition(); |
|
129 |
} |
|
130 |
||
131 |
@Override |
|
132 |
public long getStartPosition() { |
|
133 |
return d.getStartPosition(); |
|
134 |
} |
|
135 |
||
136 |
@Override |
|
137 |
public long getEndPosition() { |
|
138 |
return d.getEndPosition(); |
|
139 |
} |
|
140 |
||
141 |
@Override |
|
142 |
public String getCode() { |
|
143 |
return d.getCode(); |
|
144 |
} |
|
145 |
||
146 |
@Override |
|
147 |
public String getMessage(Locale locale) { |
|
148 |
return expunge(d.getMessage(locale)); |
|
149 |
} |
|
150 |
}; |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
private class WrapSourceHandler implements SourceHandler<OuterWrap> { |
|
155 |
||
156 |
@Override |
|
157 |
public JavaFileObject sourceToFileObject(MemoryFileManager fm, OuterWrap w) { |
|
158 |
return fm.createSourceFileObject(w, w.classFullName(), w.wrapped()); |
|
159 |
} |
|
160 |
||
161 |
@Override |
|
162 |
public Diag diag(Diagnostic<? extends JavaFileObject> d) { |
|
163 |
SourceMemoryJavaFileObject smjfo = (SourceMemoryJavaFileObject) d.getSource(); |
|
43038 | 164 |
if (smjfo == null) { |
165 |
// Handle failure that doesn't preserve mapping |
|
166 |
return new StringSourceHandler().diag(d); |
|
167 |
} |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
168 |
OuterWrap w = (OuterWrap) smjfo.getOrigin(); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
169 |
return w.wrapDiag(d); |
33362 | 170 |
} |
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Parse a snippet of code (as a String) using the parser subclass. Return |
|
175 |
* the parse tree (and errors). |
|
176 |
*/ |
|
177 |
class ParseTask extends BaseTask { |
|
178 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
179 |
private final Iterable<? extends CompilationUnitTree> cuts; |
33362 | 180 |
private final List<? extends Tree> units; |
181 |
||
182 |
ParseTask(final String source) { |
|
183 |
super(Stream.of(source), |
|
184 |
new StringSourceHandler(), |
|
185 |
"-XDallowStringFolding=false", "-proc:none"); |
|
186 |
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
|
187 |
cuts = parse(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
188 |
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
|
189 |
.flatMap(cut -> { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
190 |
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
|
191 |
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
|
192 |
}) |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
193 |
.collect(toList()); |
33362 | 194 |
} |
195 |
||
196 |
private Iterable<? extends CompilationUnitTree> parse() { |
|
197 |
try { |
|
198 |
return task.parse(); |
|
199 |
} catch (Exception ex) { |
|
200 |
throw new InternalError("Exception during parse - " + ex.getMessage(), ex); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
List<? extends Tree> units() { |
|
205 |
return units; |
|
206 |
} |
|
207 |
||
208 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
209 |
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
|
210 |
return cuts; |
33362 | 211 |
} |
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Run the normal "analyze()" pass of the compiler over the wrapped snippet. |
|
216 |
*/ |
|
217 |
class AnalyzeTask extends BaseTask { |
|
218 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
219 |
private final Iterable<? extends CompilationUnitTree> cuts; |
33362 | 220 |
|
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
221 |
AnalyzeTask(final OuterWrap wrap, String... extraArgs) { |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
222 |
this(Collections.singletonList(wrap), extraArgs); |
33362 | 223 |
} |
224 |
||
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
225 |
AnalyzeTask(final Collection<OuterWrap> wraps, String... extraArgs) { |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
226 |
this(wraps.stream(), |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
227 |
new WrapSourceHandler(), |
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
228 |
Util.join(new String[] { |
40835
6ab9ed1abc46
8162546: change hidden options -Xdebug to --debug, -XshouldStop to --should-stop, and -diags to --diags
vromero
parents:
39809
diff
changeset
|
229 |
"--should-stop:at=FLOW", "-Xlint:unchecked", |
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
230 |
"-proc:none" |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
231 |
}, extraArgs)); |
33362 | 232 |
} |
233 |
||
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
234 |
private <T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler, |
33362 | 235 |
String... extraOptions) { |
236 |
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
|
237 |
cuts = analyze(); |
33362 | 238 |
} |
239 |
||
240 |
private Iterable<? extends CompilationUnitTree> analyze() { |
|
241 |
try { |
|
242 |
Iterable<? extends CompilationUnitTree> cuts = task.parse(); |
|
243 |
task.analyze(); |
|
244 |
return cuts; |
|
245 |
} catch (Exception ex) { |
|
246 |
throw new InternalError("Exception during analyze - " + ex.getMessage(), ex); |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
251 |
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
|
252 |
return cuts; |
33362 | 253 |
} |
254 |
||
255 |
Elements getElements() { |
|
256 |
return task.getElements(); |
|
257 |
} |
|
258 |
||
259 |
javax.lang.model.util.Types getTypes() { |
|
260 |
return task.getTypes(); |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Unit the wrapped snippet to class files. |
|
266 |
*/ |
|
267 |
class CompileTask extends BaseTask { |
|
268 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
269 |
private final Map<OuterWrap, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>(); |
33362 | 270 |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
271 |
CompileTask(final Collection<OuterWrap> wraps) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
272 |
super(wraps.stream(), new WrapSourceHandler(), |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39601
diff
changeset
|
273 |
"-Xlint:unchecked", "-proc:none", "-parameters"); |
33362 | 274 |
} |
275 |
||
276 |
boolean compile() { |
|
277 |
fileManager.registerClassFileCreationListener(this::listenForNewClassFile); |
|
278 |
boolean result = task.call(); |
|
279 |
fileManager.registerClassFileCreationListener(null); |
|
280 |
return result; |
|
281 |
} |
|
282 |
||
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
283 |
// Returns the list of classes generated during this compile. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
284 |
// Stores the mapping between class name and current compiled bytes. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
285 |
List<String> classList(OuterWrap w) { |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
286 |
List<OutputMemoryJavaFileObject> l = classObjs.get(w); |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
287 |
if (l == null) { |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
288 |
return Collections.emptyList(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
289 |
} |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
290 |
List<String> list = new ArrayList<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
291 |
for (OutputMemoryJavaFileObject fo : l) { |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39601
diff
changeset
|
292 |
state.classTracker.setCurrentBytes(fo.getName(), fo.getBytes()); |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
293 |
list.add(fo.getName()); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
294 |
} |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
295 |
return list; |
33362 | 296 |
} |
297 |
||
298 |
private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location, |
|
299 |
String className, JavaFileObject.Kind kind, FileObject sibling) { |
|
300 |
//debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind); |
|
301 |
if (location == CLASS_OUTPUT) { |
|
302 |
state.debug(DBG_GEN, "Compiler generating class %s\n", className); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
303 |
OuterWrap w = ((sibling instanceof SourceMemoryJavaFileObject) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
304 |
&& (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof OuterWrap)) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
305 |
? (OuterWrap) ((SourceMemoryJavaFileObject) sibling).getOrigin() |
33362 | 306 |
: null; |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
307 |
classObjs.compute(w, (k, v) -> (v == null)? new ArrayList<>() : v) |
33362 | 308 |
.add(jfo); |
309 |
} |
|
310 |
} |
|
311 |
||
312 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
313 |
Iterable<? extends CompilationUnitTree> cuTrees() { |
33362 | 314 |
throw new UnsupportedOperationException("Not supported."); |
315 |
} |
|
316 |
} |
|
317 |
||
318 |
abstract class BaseTask { |
|
319 |
||
320 |
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); |
|
321 |
final JavacTaskImpl task; |
|
322 |
private DiagList diags = null; |
|
323 |
private final SourceHandler<?> sourceHandler; |
|
43134
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
43038
diff
changeset
|
324 |
final Context context = new Context(); |
33362 | 325 |
private Types types; |
326 |
private JavacMessages messages; |
|
327 |
private Trees trees; |
|
328 |
||
329 |
private <T>BaseTask(Stream<T> inputs, |
|
330 |
//BiFunction<MemoryFileManager, T, JavaFileObject> sfoCreator, |
|
331 |
SourceHandler<T> sh, |
|
332 |
String... extraOptions) { |
|
333 |
this.sourceHandler = sh; |
|
39369 | 334 |
List<String> options = new ArrayList<>(extraOptions.length + state.extraCompilerOptions.size()); |
335 |
options.addAll(Arrays.asList(extraOptions)); |
|
336 |
options.addAll(state.extraCompilerOptions); |
|
33362 | 337 |
Iterable<? extends JavaFileObject> compilationUnits = inputs |
338 |
.map(in -> sh.sourceToFileObject(fileManager, in)) |
|
339 |
.collect(Collectors.toList()); |
|
340 |
this.task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, |
|
341 |
fileManager, diagnostics, options, null, |
|
342 |
compilationUnits, context); |
|
343 |
} |
|
344 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
345 |
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
|
346 |
|
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
347 |
CompilationUnitTree firstCuTree() { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
348 |
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
|
349 |
} |
33362 | 350 |
|
351 |
Diag diag(Diagnostic<? extends JavaFileObject> diag) { |
|
352 |
return sourceHandler.diag(diag); |
|
353 |
} |
|
354 |
||
355 |
Context getContext() { |
|
356 |
return context; |
|
357 |
} |
|
358 |
||
359 |
Types types() { |
|
360 |
if (types == null) { |
|
361 |
types = Types.instance(context); |
|
362 |
} |
|
363 |
return types; |
|
364 |
} |
|
365 |
||
366 |
JavacMessages messages() { |
|
367 |
if (messages == null) { |
|
368 |
messages = JavacMessages.instance(context); |
|
369 |
} |
|
370 |
return messages; |
|
371 |
} |
|
372 |
||
373 |
Trees trees() { |
|
374 |
if (trees == null) { |
|
375 |
trees = Trees.instance(task); |
|
376 |
} |
|
377 |
return trees; |
|
378 |
} |
|
379 |
||
380 |
// ------------------ diags functionality |
|
381 |
||
382 |
DiagList getDiagnostics() { |
|
383 |
if (diags == null) { |
|
384 |
LinkedHashMap<String, Diag> diagMap = new LinkedHashMap<>(); |
|
385 |
for (Diagnostic<? extends JavaFileObject> in : diagnostics.getDiagnostics()) { |
|
386 |
Diag d = diag(in); |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
387 |
String uniqueKey = d.getCode() + ":" + d.getPosition() + ":" + d.getMessage(PARSED_LOCALE); |
33362 | 388 |
diagMap.put(uniqueKey, d); |
389 |
} |
|
390 |
diags = new DiagList(diagMap.values()); |
|
391 |
} |
|
392 |
return diags; |
|
393 |
} |
|
394 |
||
395 |
boolean hasErrors() { |
|
396 |
return getDiagnostics().hasErrors(); |
|
397 |
} |
|
398 |
||
399 |
String shortErrorMessage() { |
|
400 |
StringBuilder sb = new StringBuilder(); |
|
401 |
for (Diag diag : getDiagnostics()) { |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
402 |
for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) { |
33362 | 403 |
if (!line.trim().startsWith("location:")) { |
404 |
sb.append(line); |
|
405 |
} |
|
406 |
} |
|
407 |
} |
|
408 |
return sb.toString(); |
|
409 |
} |
|
410 |
||
411 |
void debugPrintDiagnostics(String src) { |
|
412 |
for (Diag diag : getDiagnostics()) { |
|
413 |
state.debug(DBG_GEN, "ERROR --\n"); |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
414 |
for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) { |
33362 | 415 |
if (!line.trim().startsWith("location:")) { |
416 |
state.debug(DBG_GEN, "%s\n", line); |
|
417 |
} |
|
418 |
} |
|
419 |
int start = (int) diag.getStartPosition(); |
|
420 |
int end = (int) diag.getEndPosition(); |
|
421 |
if (src != null) { |
|
422 |
String[] srcLines = src.split("\\r?\\n"); |
|
423 |
for (String line : srcLines) { |
|
424 |
state.debug(DBG_GEN, "%s\n", line); |
|
425 |
} |
|
426 |
||
427 |
StringBuilder sb = new StringBuilder(); |
|
428 |
for (int i = 0; i < start; ++i) { |
|
429 |
sb.append(' '); |
|
430 |
} |
|
431 |
sb.append('^'); |
|
432 |
if (end > start) { |
|
433 |
for (int i = start + 1; i < end; ++i) { |
|
434 |
sb.append('-'); |
|
435 |
} |
|
436 |
sb.append('^'); |
|
437 |
} |
|
438 |
state.debug(DBG_GEN, "%s\n", sb.toString()); |
|
439 |
} |
|
440 |
state.debug(DBG_GEN, "printDiagnostics start-pos = %d ==> %d -- wrap = %s\n", |
|
441 |
diag.getStartPosition(), start, this); |
|
442 |
state.debug(DBG_GEN, "Code: %s\n", diag.getCode()); |
|
443 |
state.debug(DBG_GEN, "Pos: %d (%d - %d) -- %s\n", diag.getPosition(), |
|
444 |
diag.getStartPosition(), diag.getEndPosition(), diag.getMessage(null)); |
|
445 |
} |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
} |