author | rfield |
Wed, 08 Jun 2016 00:32:31 -0700 | |
changeset 38908 | f0c186d76c8a |
parent 38535 | 4a25025e0b0d |
child 39369 | 0469f052203d |
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")); |
9c4840131512
8150632: jdk.jshell.TaskFactory should use jdk.Version to check for java.specification.version
simonis
parents:
35000
diff
changeset
|
85 |
if (INITIAL_SUPPORTED_VER.compareToIgnoreOpt(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(); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
164 |
OuterWrap w = (OuterWrap) smjfo.getOrigin(); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
165 |
return w.wrapDiag(d); |
33362 | 166 |
} |
167 |
} |
|
168 |
||
169 |
/** |
|
170 |
* Parse a snippet of code (as a String) using the parser subclass. Return |
|
171 |
* the parse tree (and errors). |
|
172 |
*/ |
|
173 |
class ParseTask extends BaseTask { |
|
174 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
175 |
private final Iterable<? extends CompilationUnitTree> cuts; |
33362 | 176 |
private final List<? extends Tree> units; |
177 |
||
178 |
ParseTask(final String source) { |
|
179 |
super(Stream.of(source), |
|
180 |
new StringSourceHandler(), |
|
181 |
"-XDallowStringFolding=false", "-proc:none"); |
|
182 |
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
|
183 |
cuts = parse(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
184 |
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
|
185 |
.flatMap(cut -> { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
186 |
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
|
187 |
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
|
188 |
}) |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
189 |
.collect(toList()); |
33362 | 190 |
} |
191 |
||
192 |
private Iterable<? extends CompilationUnitTree> parse() { |
|
193 |
try { |
|
194 |
return task.parse(); |
|
195 |
} catch (Exception ex) { |
|
196 |
throw new InternalError("Exception during parse - " + ex.getMessage(), ex); |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
List<? extends Tree> units() { |
|
201 |
return units; |
|
202 |
} |
|
203 |
||
204 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
205 |
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
|
206 |
return cuts; |
33362 | 207 |
} |
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Run the normal "analyze()" pass of the compiler over the wrapped snippet. |
|
212 |
*/ |
|
213 |
class AnalyzeTask extends BaseTask { |
|
214 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
215 |
private final Iterable<? extends CompilationUnitTree> cuts; |
33362 | 216 |
|
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
217 |
AnalyzeTask(final OuterWrap wrap, String... extraArgs) { |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
218 |
this(Collections.singletonList(wrap), extraArgs); |
33362 | 219 |
} |
220 |
||
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
221 |
AnalyzeTask(final Collection<OuterWrap> wraps, String... extraArgs) { |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
222 |
this(wraps.stream(), |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
223 |
new WrapSourceHandler(), |
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
224 |
Util.join(new String[] { |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
225 |
"-XDshouldStopPolicy=FLOW", "-Xlint:unchecked", |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
226 |
"-XaddExports:jdk.jshell/jdk.internal.jshell.remote=ALL-UNNAMED", |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
227 |
"-proc:none" |
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
228 |
}, extraArgs)); |
33362 | 229 |
} |
230 |
||
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
231 |
private <T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler, |
33362 | 232 |
String... extraOptions) { |
233 |
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
|
234 |
cuts = analyze(); |
33362 | 235 |
} |
236 |
||
237 |
private Iterable<? extends CompilationUnitTree> analyze() { |
|
238 |
try { |
|
239 |
Iterable<? extends CompilationUnitTree> cuts = task.parse(); |
|
240 |
task.analyze(); |
|
241 |
return cuts; |
|
242 |
} catch (Exception ex) { |
|
243 |
throw new InternalError("Exception during analyze - " + ex.getMessage(), ex); |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
248 |
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
|
249 |
return cuts; |
33362 | 250 |
} |
251 |
||
252 |
Elements getElements() { |
|
253 |
return task.getElements(); |
|
254 |
} |
|
255 |
||
256 |
javax.lang.model.util.Types getTypes() { |
|
257 |
return task.getTypes(); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Unit the wrapped snippet to class files. |
|
263 |
*/ |
|
264 |
class CompileTask extends BaseTask { |
|
265 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
266 |
private final Map<OuterWrap, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>(); |
33362 | 267 |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
268 |
CompileTask(final Collection<OuterWrap> wraps) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
269 |
super(wraps.stream(), new WrapSourceHandler(), |
37939
3eb8c2a89b77
8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents:
37644
diff
changeset
|
270 |
"-Xlint:unchecked", "-XaddExports:jdk.jshell/jdk.internal.jshell.remote=ALL-UNNAMED", "-proc:none", "-parameters"); |
33362 | 271 |
} |
272 |
||
273 |
boolean compile() { |
|
274 |
fileManager.registerClassFileCreationListener(this::listenForNewClassFile); |
|
275 |
boolean result = task.call(); |
|
276 |
fileManager.registerClassFileCreationListener(null); |
|
277 |
return result; |
|
278 |
} |
|
279 |
||
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
280 |
// Returns the list of classes generated during this compile. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
281 |
// 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
|
282 |
List<String> classList(OuterWrap w) { |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
283 |
List<OutputMemoryJavaFileObject> l = classObjs.get(w); |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
284 |
if (l == null) { |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
285 |
return Collections.emptyList(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
286 |
} |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
287 |
List<String> list = new ArrayList<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
288 |
for (OutputMemoryJavaFileObject fo : l) { |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
289 |
state.setClassnameToBytes(fo.getName(), fo.getBytes()); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
290 |
list.add(fo.getName()); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
291 |
} |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
292 |
return list; |
33362 | 293 |
} |
294 |
||
295 |
private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location, |
|
296 |
String className, JavaFileObject.Kind kind, FileObject sibling) { |
|
297 |
//debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind); |
|
298 |
if (location == CLASS_OUTPUT) { |
|
299 |
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
|
300 |
OuterWrap w = ((sibling instanceof SourceMemoryJavaFileObject) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
301 |
&& (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof OuterWrap)) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
302 |
? (OuterWrap) ((SourceMemoryJavaFileObject) sibling).getOrigin() |
33362 | 303 |
: null; |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
304 |
classObjs.compute(w, (k, v) -> (v == null)? new ArrayList<>() : v) |
33362 | 305 |
.add(jfo); |
306 |
} |
|
307 |
} |
|
308 |
||
309 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
310 |
Iterable<? extends CompilationUnitTree> cuTrees() { |
33362 | 311 |
throw new UnsupportedOperationException("Not supported."); |
312 |
} |
|
313 |
} |
|
314 |
||
315 |
abstract class BaseTask { |
|
316 |
||
317 |
final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); |
|
318 |
final JavacTaskImpl task; |
|
319 |
private DiagList diags = null; |
|
320 |
private final SourceHandler<?> sourceHandler; |
|
321 |
private final Context context = new Context(); |
|
322 |
private Types types; |
|
323 |
private JavacMessages messages; |
|
324 |
private Trees trees; |
|
325 |
||
326 |
private <T>BaseTask(Stream<T> inputs, |
|
327 |
//BiFunction<MemoryFileManager, T, JavaFileObject> sfoCreator, |
|
328 |
SourceHandler<T> sh, |
|
329 |
String... extraOptions) { |
|
330 |
this.sourceHandler = sh; |
|
331 |
List<String> options = Arrays.asList(extraOptions); |
|
332 |
Iterable<? extends JavaFileObject> compilationUnits = inputs |
|
333 |
.map(in -> sh.sourceToFileObject(fileManager, in)) |
|
334 |
.collect(Collectors.toList()); |
|
335 |
this.task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null, |
|
336 |
fileManager, diagnostics, options, null, |
|
337 |
compilationUnits, context); |
|
338 |
} |
|
339 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
340 |
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
|
341 |
|
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
342 |
CompilationUnitTree firstCuTree() { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
343 |
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
|
344 |
} |
33362 | 345 |
|
346 |
Diag diag(Diagnostic<? extends JavaFileObject> diag) { |
|
347 |
return sourceHandler.diag(diag); |
|
348 |
} |
|
349 |
||
350 |
Context getContext() { |
|
351 |
return context; |
|
352 |
} |
|
353 |
||
354 |
Types types() { |
|
355 |
if (types == null) { |
|
356 |
types = Types.instance(context); |
|
357 |
} |
|
358 |
return types; |
|
359 |
} |
|
360 |
||
361 |
JavacMessages messages() { |
|
362 |
if (messages == null) { |
|
363 |
messages = JavacMessages.instance(context); |
|
364 |
} |
|
365 |
return messages; |
|
366 |
} |
|
367 |
||
368 |
Trees trees() { |
|
369 |
if (trees == null) { |
|
370 |
trees = Trees.instance(task); |
|
371 |
} |
|
372 |
return trees; |
|
373 |
} |
|
374 |
||
375 |
// ------------------ diags functionality |
|
376 |
||
377 |
DiagList getDiagnostics() { |
|
378 |
if (diags == null) { |
|
379 |
LinkedHashMap<String, Diag> diagMap = new LinkedHashMap<>(); |
|
380 |
for (Diagnostic<? extends JavaFileObject> in : diagnostics.getDiagnostics()) { |
|
381 |
Diag d = diag(in); |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
382 |
String uniqueKey = d.getCode() + ":" + d.getPosition() + ":" + d.getMessage(PARSED_LOCALE); |
33362 | 383 |
diagMap.put(uniqueKey, d); |
384 |
} |
|
385 |
diags = new DiagList(diagMap.values()); |
|
386 |
} |
|
387 |
return diags; |
|
388 |
} |
|
389 |
||
390 |
boolean hasErrors() { |
|
391 |
return getDiagnostics().hasErrors(); |
|
392 |
} |
|
393 |
||
394 |
String shortErrorMessage() { |
|
395 |
StringBuilder sb = new StringBuilder(); |
|
396 |
for (Diag diag : getDiagnostics()) { |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
397 |
for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) { |
33362 | 398 |
if (!line.trim().startsWith("location:")) { |
399 |
sb.append(line); |
|
400 |
} |
|
401 |
} |
|
402 |
} |
|
403 |
return sb.toString(); |
|
404 |
} |
|
405 |
||
406 |
void debugPrintDiagnostics(String src) { |
|
407 |
for (Diag diag : getDiagnostics()) { |
|
408 |
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
|
409 |
for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) { |
33362 | 410 |
if (!line.trim().startsWith("location:")) { |
411 |
state.debug(DBG_GEN, "%s\n", line); |
|
412 |
} |
|
413 |
} |
|
414 |
int start = (int) diag.getStartPosition(); |
|
415 |
int end = (int) diag.getEndPosition(); |
|
416 |
if (src != null) { |
|
417 |
String[] srcLines = src.split("\\r?\\n"); |
|
418 |
for (String line : srcLines) { |
|
419 |
state.debug(DBG_GEN, "%s\n", line); |
|
420 |
} |
|
421 |
||
422 |
StringBuilder sb = new StringBuilder(); |
|
423 |
for (int i = 0; i < start; ++i) { |
|
424 |
sb.append(' '); |
|
425 |
} |
|
426 |
sb.append('^'); |
|
427 |
if (end > start) { |
|
428 |
for (int i = start + 1; i < end; ++i) { |
|
429 |
sb.append('-'); |
|
430 |
} |
|
431 |
sb.append('^'); |
|
432 |
} |
|
433 |
state.debug(DBG_GEN, "%s\n", sb.toString()); |
|
434 |
} |
|
435 |
state.debug(DBG_GEN, "printDiagnostics start-pos = %d ==> %d -- wrap = %s\n", |
|
436 |
diag.getStartPosition(), start, this); |
|
437 |
state.debug(DBG_GEN, "Code: %s\n", diag.getCode()); |
|
438 |
state.debug(DBG_GEN, "Pos: %d (%d - %d) -- %s\n", diag.getPosition(), |
|
439 |
diag.getStartPosition(), diag.getEndPosition(), diag.getMessage(null)); |
|
440 |
} |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
} |