author | jlahoda |
Fri, 08 Apr 2016 11:52:46 +0200 | |
changeset 37004 | ff77b7986967 |
parent 36990 | ec0b843a7af5 |
child 37644 | 33cf53901cac |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
2 |
* Copyright (c) 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 java.io.ByteArrayInputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.InputStream; |
|
31 |
import java.io.PrintStream; |
|
36990 | 32 |
import java.text.MessageFormat; |
33362 | 33 |
import java.util.Collections; |
34 |
import java.util.HashMap; |
|
35 |
import java.util.List; |
|
36 |
import java.util.Map; |
|
36990 | 37 |
import java.util.MissingResourceException; |
33362 | 38 |
import java.util.Objects; |
36990 | 39 |
import java.util.ResourceBundle; |
33362 | 40 |
import java.util.function.BiFunction; |
41 |
import java.util.function.Consumer; |
|
42 |
||
43 |
import java.util.function.Supplier; |
|
44 |
import jdk.internal.jshell.debug.InternalDebugControl; |
|
45 |
import static java.util.stream.Collectors.collectingAndThen; |
|
46 |
import static java.util.stream.Collectors.toList; |
|
47 |
import static jdk.jshell.Util.expunge; |
|
48 |
import jdk.jshell.Snippet.Status; |
|
49 |
||
50 |
/** |
|
51 |
* The JShell evaluation state engine. This is the central class in the JShell |
|
52 |
* API. A <code>JShell</code> instance holds the evolving compilation and |
|
53 |
* execution state. The state is changed with the instance methods |
|
54 |
* {@link jdk.jshell.JShell#eval(java.lang.String) eval(String)}, |
|
55 |
* {@link jdk.jshell.JShell#drop(jdk.jshell.PersistentSnippet) drop(PersistentSnippet)} and |
|
56 |
* {@link jdk.jshell.JShell#addToClasspath(java.lang.String) addToClasspath(String)}. |
|
57 |
* The majority of methods query the state. |
|
58 |
* A <code>JShell</code> instance also allows registering for events with |
|
59 |
* {@link jdk.jshell.JShell#onSnippetEvent(java.util.function.Consumer) onSnippetEvent(Consumer)} |
|
60 |
* and {@link jdk.jshell.JShell#onShutdown(java.util.function.Consumer) onShutdown(Consumer)}, which |
|
61 |
* are unregistered with |
|
62 |
* {@link jdk.jshell.JShell#unsubscribe(jdk.jshell.JShell.Subscription) unsubscribe(Subscription)}. |
|
63 |
* Access to the source analysis utilities is via |
|
64 |
* {@link jdk.jshell.JShell#sourceCodeAnalysis()}. |
|
65 |
* When complete the instance should be closed to free resources -- |
|
66 |
* {@link jdk.jshell.JShell#close()}. |
|
67 |
* <p> |
|
68 |
* An instance of <code>JShell</code> is created with |
|
69 |
* <code>JShell.create()</code>. |
|
70 |
* <p> |
|
71 |
* This class is not thread safe, except as noted, all access should be through |
|
72 |
* a single thread. |
|
73 |
* @see jdk.jshell |
|
74 |
* @author Robert Field |
|
75 |
*/ |
|
76 |
public class JShell implements AutoCloseable { |
|
77 |
||
78 |
final SnippetMaps maps; |
|
79 |
final KeyMap keyMap; |
|
80 |
final TaskFactory taskFactory; |
|
81 |
final InputStream in; |
|
82 |
final PrintStream out; |
|
83 |
final PrintStream err; |
|
84 |
final Supplier<String> tempVariableNameGenerator; |
|
85 |
final BiFunction<Snippet, Integer, String> idGenerator; |
|
86 |
||
87 |
private int nextKeyIndex = 1; |
|
88 |
||
89 |
final Eval eval; |
|
90 |
final ClassTracker classTracker; |
|
91 |
private final Map<Subscription, Consumer<JShell>> shutdownListeners = new HashMap<>(); |
|
92 |
private final Map<Subscription, Consumer<SnippetEvent>> keyStatusListeners = new HashMap<>(); |
|
93 |
private boolean closed = false; |
|
94 |
||
95 |
private ExecutionControl executionControl = null; |
|
33715
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33714
diff
changeset
|
96 |
private SourceCodeAnalysisImpl sourceCodeAnalysis = null; |
33362 | 97 |
|
36990 | 98 |
private static final String L10N_RB_NAME = "jdk.jshell.resources.l10n"; |
99 |
private static ResourceBundle outputRB = null; |
|
33362 | 100 |
|
101 |
JShell(Builder b) { |
|
102 |
this.in = b.in; |
|
103 |
this.out = b.out; |
|
104 |
this.err = b.err; |
|
105 |
this.tempVariableNameGenerator = b.tempVariableNameGenerator; |
|
106 |
this.idGenerator = b.idGenerator; |
|
107 |
||
108 |
this.maps = new SnippetMaps(this); |
|
109 |
maps.setPackageName("REPL"); |
|
110 |
this.keyMap = new KeyMap(this); |
|
111 |
this.taskFactory = new TaskFactory(this); |
|
112 |
this.eval = new Eval(this); |
|
113 |
this.classTracker = new ClassTracker(this); |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Builder for <code>JShell</code> instances. |
|
118 |
* Create custom instances of <code>JShell</code> by using the setter |
|
119 |
* methods on this class. After zero or more of these, use the |
|
120 |
* {@link #build()} method to create a <code>JShell</code> instance. |
|
121 |
* These can all be chained. For example, setting the remote output and |
|
122 |
* error streams: |
|
123 |
* <pre> |
|
124 |
* <code> |
|
125 |
* JShell myShell = |
|
126 |
* JShell.builder() |
|
127 |
* .out(myOutStream) |
|
128 |
* .err(myErrStream) |
|
129 |
* .build(); </code> </pre> |
|
130 |
* If no special set-up is needed, just use |
|
131 |
* <code>JShell.builder().build()</code> or the short-cut equivalent |
|
132 |
* <code>JShell.create()</code>. |
|
133 |
*/ |
|
134 |
public static class Builder { |
|
135 |
||
136 |
InputStream in = new ByteArrayInputStream(new byte[0]); |
|
137 |
PrintStream out = System.out; |
|
138 |
PrintStream err = System.err; |
|
139 |
Supplier<String> tempVariableNameGenerator = null; |
|
140 |
BiFunction<Snippet, Integer, String> idGenerator = null; |
|
141 |
||
142 |
Builder() { } |
|
143 |
||
144 |
/** |
|
145 |
* Input for the running evaluation (it's <code>System.in</code>). Note: |
|
146 |
* applications that use <code>System.in</code> for snippet or other |
|
147 |
* user input cannot use <code>System.in</code> as the input stream for |
|
148 |
* the remote process. |
|
149 |
* <p> |
|
150 |
* The default, if this is not set, is to provide an empty input stream |
|
151 |
* -- <code>new ByteArrayInputStream(new byte[0])</code>. |
|
152 |
* |
|
153 |
* @param in the <code>InputStream</code> to be channelled to |
|
154 |
* <code>System.in</code> in the remote execution process. |
|
155 |
* @return the <code>Builder</code> instance (for use in chained |
|
156 |
* initialization). |
|
157 |
*/ |
|
158 |
public Builder in(InputStream in) { |
|
159 |
this.in = in; |
|
160 |
return this; |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Output for the running evaluation (it's <code>System.out</code>). |
|
165 |
* The controlling process and |
|
166 |
* the remote process can share <code>System.out</code>. |
|
167 |
* <p> |
|
168 |
* The default, if this is not set, is <code>System.out</code>. |
|
169 |
* |
|
170 |
* @param out the <code>PrintStream</code> to be channelled to |
|
171 |
* <code>System.out</code> in the remote execution process. |
|
172 |
* @return the <code>Builder</code> instance (for use in chained |
|
173 |
* initialization). |
|
174 |
*/ |
|
175 |
public Builder out(PrintStream out) { |
|
176 |
this.out = out; |
|
177 |
return this; |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Error output for the running evaluation (it's |
|
182 |
* <code>System.err</code>). The controlling process and the remote |
|
183 |
* process can share <code>System.err</code>. |
|
184 |
* <p> |
|
185 |
* The default, if this is not set, is <code>System.err</code>. |
|
186 |
* |
|
187 |
* @param err the <code>PrintStream</code> to be channelled to |
|
188 |
* <code>System.err</code> in the remote execution process. |
|
189 |
* @return the <code>Builder</code> instance (for use in chained |
|
190 |
* initialization). |
|
191 |
*/ |
|
192 |
public Builder err(PrintStream err) { |
|
193 |
this.err = err; |
|
194 |
return this; |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Set a generator of temp variable names for |
|
199 |
* {@link jdk.jshell.VarSnippet} of |
|
200 |
* {@link jdk.jshell.Snippet.SubKind#TEMP_VAR_EXPRESSION_SUBKIND}. |
|
201 |
* <p> |
|
202 |
* Do not use this method unless you have explicit need for it. |
|
203 |
* <p> |
|
204 |
* The generator will be used for newly created VarSnippet |
|
205 |
* instances. The name of a variable is queried with |
|
206 |
* {@link jdk.jshell.VarSnippet#name()}. |
|
207 |
* <p> |
|
208 |
* The callback is sent during the processing of the snippet, the |
|
209 |
* JShell state is not stable. No calls whatsoever on the |
|
210 |
* <code>JShell</code> instance may be made from the callback. |
|
211 |
* <p> |
|
212 |
* The generated name must be unique within active snippets. |
|
213 |
* <p> |
|
214 |
* The default behavior (if this is not set or <code>generator</code> |
|
215 |
* is null) is to generate the name as a sequential number with a |
|
216 |
* prefixing dollar sign ("$"). |
|
217 |
* |
|
218 |
* @param generator the <code>Supplier</code> to generate the temporary |
|
219 |
* variable name string or <code>null</code>. |
|
220 |
* @return the <code>Builder</code> instance (for use in chained |
|
221 |
* initialization). |
|
222 |
*/ |
|
223 |
public Builder tempVariableNameGenerator(Supplier<String> generator) { |
|
224 |
this.tempVariableNameGenerator = generator; |
|
225 |
return this; |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Set the generator of identifying names for Snippets. |
|
230 |
* <p> |
|
231 |
* Do not use this method unless you have explicit need for it. |
|
232 |
* <p> |
|
233 |
* The generator will be used for newly created Snippet instances. The |
|
234 |
* identifying name (id) is accessed with |
|
235 |
* {@link jdk.jshell.Snippet#id()} and can be seen in the |
|
236 |
* <code>StackTraceElement.getFileName()</code> for a |
|
237 |
* {@link jdk.jshell.EvalException} and |
|
238 |
* {@link jdk.jshell.UnresolvedReferenceException}. |
|
239 |
* <p> |
|
240 |
* The inputs to the generator are the {@link jdk.jshell.Snippet} and an |
|
241 |
* integer. The integer will be the same for two Snippets which would |
|
242 |
* overwrite one-another, but otherwise is unique. |
|
243 |
* <p> |
|
244 |
* The callback is sent during the processing of the snippet and the |
|
245 |
* Snippet and the state as a whole are not stable. No calls to change |
|
246 |
* system state (including Snippet state) should be made. Queries of |
|
247 |
* Snippet may be made except to {@link jdk.jshell.Snippet#id()}. No |
|
248 |
* calls on the <code>JShell</code> instance may be made from the |
|
249 |
* callback, except to |
|
250 |
* {@link #status(jdk.jshell.Snippet) status(Snippet)}. |
|
251 |
* <p> |
|
252 |
* The default behavior (if this is not set or <code>generator</code> |
|
253 |
* is null) is to generate the id as the integer converted to a string. |
|
254 |
* |
|
255 |
* @param generator the <code>BiFunction</code> to generate the id |
|
256 |
* string or <code>null</code>. |
|
257 |
* @return the <code>Builder</code> instance (for use in chained |
|
258 |
* initialization). |
|
259 |
*/ |
|
260 |
public Builder idGenerator(BiFunction<Snippet, Integer, String> generator) { |
|
261 |
this.idGenerator = generator; |
|
262 |
return this; |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Build a JShell state engine. This is the entry-point to all JShell |
|
267 |
* functionality. This creates a remote process for execution. It is |
|
268 |
* thus important to close the returned instance. |
|
269 |
* |
|
270 |
* @return the state engine. |
|
271 |
*/ |
|
272 |
public JShell build() { |
|
273 |
return new JShell(this); |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
// --- public API --- |
|
278 |
||
279 |
/** |
|
280 |
* Create a new JShell state engine. |
|
281 |
* That is, create an instance of <code>JShell</code>. |
|
282 |
* <p> |
|
283 |
* Equivalent to {@link JShell#builder() JShell.builder()}{@link JShell.Builder#build() .build()}. |
|
284 |
* @return an instance of <code>JShell</code>. |
|
285 |
*/ |
|
286 |
public static JShell create() { |
|
287 |
return builder().build(); |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Factory method for <code>JShell.Builder</code> which, in-turn, is used |
|
292 |
* for creating instances of <code>JShell</code>. |
|
293 |
* Create a default instance of <code>JShell</code> with |
|
294 |
* <code>JShell.builder().build()</code>. For more construction options |
|
295 |
* see {@link jdk.jshell.JShell.Builder}. |
|
296 |
* @return an instance of <code>Builder</code>. |
|
297 |
* @see jdk.jshell.JShell.Builder |
|
298 |
*/ |
|
299 |
public static Builder builder() { |
|
300 |
return new Builder(); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Access to source code analysis functionality. |
|
305 |
* An instance of <code>JShell</code> will always return the same |
|
306 |
* <code>SourceCodeAnalysis</code> instance from |
|
307 |
* <code>sourceCodeAnalysis()</code>. |
|
308 |
* @return an instance of {@link SourceCodeAnalysis SourceCodeAnalysis} |
|
309 |
* which can be used for source analysis such as completion detection and |
|
310 |
* completion suggestions. |
|
311 |
*/ |
|
312 |
public SourceCodeAnalysis sourceCodeAnalysis() { |
|
313 |
if (sourceCodeAnalysis == null) { |
|
314 |
sourceCodeAnalysis = new SourceCodeAnalysisImpl(this); |
|
315 |
} |
|
316 |
return sourceCodeAnalysis; |
|
317 |
} |
|
318 |
||
319 |
/** |
|
320 |
* Evaluate the input String, including definition and/or execution, if |
|
321 |
* applicable. The input is checked for errors, unless the errors can be |
|
322 |
* deferred (as is the case with some unresolvedDependencies references), |
|
323 |
* errors will abort evaluation. The input should be |
|
324 |
* exactly one complete snippet of source code, that is, one expression, |
|
325 |
* statement, variable declaration, method declaration, class declaration, |
|
326 |
* or import. |
|
327 |
* To break arbitrary input into individual complete snippets, use |
|
328 |
* {@link SourceCodeAnalysis#analyzeCompletion(String)}. |
|
329 |
* <p> |
|
330 |
* For imports, the import is added. Classes, interfaces. methods, |
|
331 |
* and variables are defined. The initializer of variables, statements, |
|
332 |
* and expressions are executed. |
|
333 |
* The modifiers public, protected, private, static, and final are not |
|
334 |
* allowed on op-level declarations and are ignored with a warning. |
|
335 |
* Synchronized, native, abstract, and default top-level methods are not |
|
336 |
* allowed and are errors. |
|
337 |
* If a previous definition of a declaration is overwritten then there will |
|
338 |
* be an event showing its status changed to OVERWRITTEN, this will not |
|
339 |
* occur for dropped, rejected, or already overwritten declarations. |
|
340 |
* <p> |
|
341 |
* The execution environment is out of process. If the evaluated code |
|
342 |
* causes the execution environment to terminate, this <code>JShell</code> |
|
343 |
* instance will be closed but the calling process and VM remain valid. |
|
344 |
* @param input The input String to evaluate |
|
345 |
* @return the list of events directly or indirectly caused by this evaluation. |
|
346 |
* @throws IllegalStateException if this <code>JShell</code> instance is closed. |
|
347 |
* @see SourceCodeAnalysis#analyzeCompletion(String) |
|
348 |
* @see JShell#onShutdown(java.util.function.Consumer) |
|
349 |
*/ |
|
350 |
public List<SnippetEvent> eval(String input) throws IllegalStateException { |
|
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
351 |
SourceCodeAnalysisImpl a = sourceCodeAnalysis; |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
352 |
if (a != null) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
353 |
a.suspendIndexing(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
354 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
355 |
try { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
356 |
checkIfAlive(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
357 |
List<SnippetEvent> events = eval.eval(input); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
358 |
events.forEach(this::notifyKeyStatusEvent); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
359 |
return Collections.unmodifiableList(events); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
360 |
} finally { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
361 |
if (a != null) { |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
362 |
a.resumeIndexing(); |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
363 |
} |
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
33715
diff
changeset
|
364 |
} |
33362 | 365 |
} |
366 |
||
367 |
/** |
|
368 |
* Remove a declaration from the state. |
|
369 |
* @param snippet The snippet to remove |
|
370 |
* @return The list of events from updating declarations dependent on the |
|
371 |
* dropped snippet. |
|
372 |
* @throws IllegalStateException if this <code>JShell</code> instance is closed. |
|
373 |
* @throws IllegalArgumentException if the snippet is not associated with |
|
374 |
* this <code>JShell</code> instance. |
|
375 |
*/ |
|
376 |
public List<SnippetEvent> drop(PersistentSnippet snippet) throws IllegalStateException { |
|
377 |
checkIfAlive(); |
|
378 |
checkValidSnippet(snippet); |
|
379 |
List<SnippetEvent> events = eval.drop(snippet); |
|
380 |
events.forEach(this::notifyKeyStatusEvent); |
|
381 |
return Collections.unmodifiableList(events); |
|
382 |
} |
|
383 |
||
384 |
/** |
|
385 |
* The specified path is added to the end of the classpath used in eval(). |
|
386 |
* Note that the unnamed package is not accessible from the package in which |
|
37004
ff77b7986967
8153243: make docs should generate JShell API docs
jlahoda
parents:
36990
diff
changeset
|
387 |
* {@link JShell#eval(String)} code is placed. |
33362 | 388 |
* @param path the path to add to the classpath. |
389 |
*/ |
|
390 |
public void addToClasspath(String path) { |
|
391 |
taskFactory.addToClasspath(path); // Compiler |
|
392 |
executionControl().commandAddToClasspath(path); // Runtime |
|
33715
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33714
diff
changeset
|
393 |
if (sourceCodeAnalysis != null) { |
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33714
diff
changeset
|
394 |
sourceCodeAnalysis.classpathChanged(); |
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33714
diff
changeset
|
395 |
} |
33362 | 396 |
} |
397 |
||
398 |
/** |
|
399 |
* Attempt to stop currently running evaluation. When called while |
|
400 |
* the {@link #eval(java.lang.String) } method is running and the |
|
401 |
* user's code being executed, an attempt will be made to stop user's code. |
|
402 |
* Note that typically this method needs to be called from a different thread |
|
403 |
* than the one running the {@code eval} method. |
|
404 |
* <p> |
|
405 |
* If the {@link #eval(java.lang.String) } method is not running, does nothing. |
|
406 |
* <p> |
|
407 |
* The attempt to stop the user's code may fail in some case, which may include |
|
408 |
* when the execution is blocked on an I/O operation, or when the user's code is |
|
409 |
* catching the {@link ThreadDeath} exception. |
|
410 |
*/ |
|
411 |
public void stop() { |
|
412 |
if (executionControl != null) |
|
413 |
executionControl.commandStop(); |
|
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* Close this state engine. Frees resources. Should be called when this |
|
418 |
* state engine is no longer needed. |
|
419 |
*/ |
|
420 |
@Override |
|
421 |
public void close() { |
|
422 |
if (!closed) { |
|
423 |
closeDown(); |
|
424 |
executionControl().commandExit(); |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
/** |
|
429 |
* Return all snippets. |
|
430 |
* @return the snippets for all current snippets in id order. |
|
431 |
* @throws IllegalStateException if this JShell instance is closed. |
|
432 |
*/ |
|
433 |
public List<Snippet> snippets() throws IllegalStateException { |
|
434 |
checkIfAlive(); |
|
435 |
return Collections.unmodifiableList(maps.snippetList()); |
|
436 |
} |
|
437 |
||
438 |
/** |
|
439 |
* Returns the active variable snippets. |
|
440 |
* This convenience method is equivalent to <code>snippets()</code> filtered for |
|
441 |
* {@link jdk.jshell.Snippet.Status#isActive status(snippet).isActive} |
|
442 |
* <code>&& snippet.kind() == Kind.VARIABLE</code> |
|
443 |
* and cast to <code>VarSnippet</code>. |
|
444 |
* @return the active declared variables. |
|
445 |
* @throws IllegalStateException if this JShell instance is closed. |
|
446 |
*/ |
|
447 |
public List<VarSnippet> variables() throws IllegalStateException { |
|
448 |
return snippets().stream() |
|
449 |
.filter(sn -> status(sn).isActive && sn.kind() == Snippet.Kind.VAR) |
|
450 |
.map(sn -> (VarSnippet) sn) |
|
451 |
.collect(collectingAndThen(toList(), Collections::unmodifiableList)); |
|
452 |
} |
|
453 |
||
454 |
/** |
|
455 |
* Returns the active method snippets. |
|
456 |
* This convenience method is equivalent to <code>snippets()</code> filtered for |
|
457 |
* {@link jdk.jshell.Snippet.Status#isActive status(snippet).isActive} |
|
458 |
* <code>&& snippet.kind() == Kind.METHOD</code> |
|
459 |
* and cast to MethodSnippet. |
|
460 |
* @return the active declared methods. |
|
461 |
* @throws IllegalStateException if this JShell instance is closed. |
|
462 |
*/ |
|
463 |
public List<MethodSnippet> methods() throws IllegalStateException { |
|
464 |
return snippets().stream() |
|
465 |
.filter(sn -> status(sn).isActive && sn.kind() == Snippet.Kind.METHOD) |
|
466 |
.map(sn -> (MethodSnippet)sn) |
|
467 |
.collect(collectingAndThen(toList(), Collections::unmodifiableList)); |
|
468 |
} |
|
469 |
||
470 |
/** |
|
471 |
* Returns the active type declaration (class, interface, annotation type, and enum) snippets. |
|
472 |
* This convenience method is equivalent to <code>snippets()</code> filtered for |
|
473 |
* {@link jdk.jshell.Snippet.Status#isActive status(snippet).isActive} |
|
474 |
* <code>&& snippet.kind() == Kind.TYPE_DECL</code> |
|
475 |
* and cast to TypeDeclSnippet. |
|
476 |
* @return the active declared type declarations. |
|
477 |
* @throws IllegalStateException if this JShell instance is closed. |
|
478 |
*/ |
|
479 |
public List<TypeDeclSnippet> types() throws IllegalStateException { |
|
480 |
return snippets().stream() |
|
481 |
.filter(sn -> status(sn).isActive && sn.kind() == Snippet.Kind.TYPE_DECL) |
|
482 |
.map(sn -> (TypeDeclSnippet) sn) |
|
483 |
.collect(collectingAndThen(toList(), Collections::unmodifiableList)); |
|
484 |
} |
|
485 |
||
486 |
/** |
|
33714
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
487 |
* Returns the active import snippets. |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
488 |
* This convenience method is equivalent to <code>snippets()</code> filtered for |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
489 |
* {@link jdk.jshell.Snippet.Status#isActive status(snippet).isActive} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
490 |
* <code>&& snippet.kind() == Kind.IMPORT</code> |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
491 |
* and cast to ImportSnippet. |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
492 |
* @return the active declared import declarations. |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
493 |
* @throws IllegalStateException if this JShell instance is closed. |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
494 |
*/ |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
495 |
public List<ImportSnippet> imports() throws IllegalStateException { |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
496 |
return snippets().stream() |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
497 |
.filter(sn -> status(sn).isActive && sn.kind() == Snippet.Kind.IMPORT) |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
498 |
.map(sn -> (ImportSnippet) sn) |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
499 |
.collect(collectingAndThen(toList(), Collections::unmodifiableList)); |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
500 |
} |
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
501 |
|
8064f484590e
8142384: JShell tool: New command: /imports, /i which show the list of imported packages or classes, etc...
shinyafox
parents:
33362
diff
changeset
|
502 |
/** |
33362 | 503 |
* Return the status of the snippet. |
504 |
* This is updated either because of an explicit <code>eval()</code> call or |
|
505 |
* an automatic update triggered by a dependency. |
|
506 |
* @param snippet the <code>Snippet</code> to look up |
|
507 |
* @return the status corresponding to this snippet |
|
508 |
* @throws IllegalStateException if this <code>JShell</code> instance is closed. |
|
509 |
* @throws IllegalArgumentException if the snippet is not associated with |
|
510 |
* this <code>JShell</code> instance. |
|
511 |
*/ |
|
512 |
public Status status(Snippet snippet) { |
|
513 |
return checkValidSnippet(snippet).status(); |
|
514 |
} |
|
515 |
||
516 |
/** |
|
517 |
* Return the diagnostics of the most recent evaluation of the snippet. |
|
518 |
* The evaluation can either because of an explicit <code>eval()</code> call or |
|
519 |
* an automatic update triggered by a dependency. |
|
520 |
* @param snippet the <code>Snippet</code> to look up |
|
521 |
* @return the diagnostics corresponding to this snippet. This does not |
|
522 |
* include unresolvedDependencies references reported in <code>unresolvedDependencies()</code>. |
|
523 |
* @throws IllegalStateException if this <code>JShell</code> instance is closed. |
|
524 |
* @throws IllegalArgumentException if the snippet is not associated with |
|
525 |
* this <code>JShell</code> instance. |
|
526 |
*/ |
|
527 |
public List<Diag> diagnostics(Snippet snippet) { |
|
528 |
return Collections.unmodifiableList(checkValidSnippet(snippet).diagnostics()); |
|
529 |
} |
|
530 |
||
531 |
/** |
|
532 |
* For {@link jdk.jshell.Snippet.Status#RECOVERABLE_DEFINED RECOVERABLE_DEFINED} or |
|
533 |
* {@link jdk.jshell.Snippet.Status#RECOVERABLE_NOT_DEFINED RECOVERABLE_NOT_DEFINED} |
|
534 |
* declarations, the names of current unresolved dependencies for |
|
535 |
* the snippet. |
|
536 |
* The returned value of this method, for a given method may change when an |
|
537 |
* <code>eval()</code> or <code>drop()</code> of another snippet causes |
|
538 |
* an update of a dependency. |
|
539 |
* @param snippet the declaration <code>Snippet</code> to look up |
|
540 |
* @return the list of symbol names that are currently unresolvedDependencies. |
|
541 |
* @throws IllegalStateException if this <code>JShell</code> instance is closed. |
|
542 |
* @throws IllegalArgumentException if the snippet is not associated with |
|
543 |
* this <code>JShell</code> instance. |
|
544 |
*/ |
|
545 |
public List<String> unresolvedDependencies(DeclarationSnippet snippet) { |
|
546 |
return Collections.unmodifiableList(checkValidSnippet(snippet).unresolved()); |
|
547 |
} |
|
548 |
||
549 |
/** |
|
550 |
* Get the current value of a variable. |
|
551 |
* @param snippet the variable Snippet whose value is queried. |
|
552 |
* @return the current value of the variable referenced by snippet. |
|
553 |
* @throws IllegalStateException if this <code>JShell</code> instance is closed. |
|
554 |
* @throws IllegalArgumentException if the snippet is not associated with |
|
555 |
* this <code>JShell</code> instance. |
|
556 |
* @throws IllegalArgumentException if the variable's status is anything but |
|
557 |
* {@link jdk.jshell.Snippet.Status#VALID}. |
|
558 |
*/ |
|
559 |
public String varValue(VarSnippet snippet) throws IllegalStateException { |
|
560 |
checkIfAlive(); |
|
561 |
checkValidSnippet(snippet); |
|
562 |
if (snippet.status() != Status.VALID) { |
|
36990 | 563 |
throw new IllegalArgumentException( |
564 |
messageFormat("jshell.exc.var.not.valid", snippet, snippet.status())); |
|
33362 | 565 |
} |
566 |
String value = executionControl().commandVarValue(maps.classFullName(snippet), snippet.name()); |
|
567 |
return expunge(value); |
|
568 |
} |
|
569 |
||
570 |
/** |
|
571 |
* Register a callback to be called when the Status of a snippet changes. |
|
572 |
* Each call adds a new subscription. |
|
573 |
* @param listener Action to perform when the Status changes. |
|
574 |
* @return A token which can be used to {@linkplain JShell#unsubscribe unsubscribe} this subscription. |
|
575 |
* @throws IllegalStateException if this <code>JShell</code> instance is closed. |
|
576 |
*/ |
|
577 |
public Subscription onSnippetEvent(Consumer<SnippetEvent> listener) |
|
578 |
throws IllegalStateException { |
|
579 |
return onX(keyStatusListeners, listener); |
|
580 |
} |
|
581 |
||
582 |
/** |
|
583 |
* Register a callback to be called when this JShell instance terminates. |
|
584 |
* This occurs either because the client process has ended (e.g. called System.exit(0)) |
|
585 |
* or the connection has been shutdown, as by close(). |
|
586 |
* Each call adds a new subscription. |
|
587 |
* @param listener Action to perform when the state terminates. |
|
588 |
* @return A token which can be used to {@linkplain JShell#unsubscribe unsubscribe} this subscription. |
|
589 |
* @throws IllegalStateException if this JShell instance is closed |
|
590 |
*/ |
|
591 |
public Subscription onShutdown(Consumer<JShell> listener) |
|
592 |
throws IllegalStateException { |
|
593 |
return onX(shutdownListeners, listener); |
|
594 |
} |
|
595 |
||
596 |
/** |
|
597 |
* Cancel a callback subscription. |
|
598 |
* @param token The token corresponding to the subscription to be unsubscribed. |
|
599 |
*/ |
|
600 |
public void unsubscribe(Subscription token) { |
|
601 |
synchronized (this) { |
|
602 |
token.remover.accept(token); |
|
603 |
} |
|
604 |
} |
|
605 |
||
606 |
/** |
|
607 |
* Subscription is a token for referring to subscriptions so they can |
|
608 |
* be {@linkplain JShell#unsubscribe unsubscribed}. |
|
609 |
*/ |
|
610 |
public class Subscription { |
|
611 |
||
612 |
Consumer<Subscription> remover; |
|
613 |
||
614 |
Subscription(Consumer<Subscription> remover) { |
|
615 |
this.remover = remover; |
|
616 |
} |
|
617 |
} |
|
618 |
||
619 |
// --- private / package-private implementation support --- |
|
620 |
||
621 |
ExecutionControl executionControl() { |
|
622 |
if (executionControl == null) { |
|
623 |
this.executionControl = new ExecutionControl(new JDIEnv(this), maps, this); |
|
624 |
try { |
|
625 |
executionControl.launch(); |
|
626 |
} catch (IOException ex) { |
|
627 |
throw new InternalError("Launching JDI execution engine threw: " + ex.getMessage(), ex); |
|
628 |
} |
|
629 |
} |
|
630 |
return executionControl; |
|
631 |
} |
|
632 |
||
633 |
void debug(int flags, String format, Object... args) { |
|
634 |
if (InternalDebugControl.debugEnabled(this, flags)) { |
|
635 |
err.printf(format, args); |
|
636 |
} |
|
637 |
} |
|
638 |
||
639 |
void debug(Exception ex, String where) { |
|
640 |
if (InternalDebugControl.debugEnabled(this, 0xFFFFFFFF)) { |
|
641 |
err.printf("Fatal error: %s: %s\n", where, ex.getMessage()); |
|
642 |
ex.printStackTrace(err); |
|
643 |
} |
|
644 |
} |
|
645 |
||
646 |
/** |
|
647 |
* Generate the next key index, indicating a unique snippet signature. |
|
648 |
* @return the next key index |
|
649 |
*/ |
|
650 |
int nextKeyIndex() { |
|
651 |
return nextKeyIndex++; |
|
652 |
} |
|
653 |
||
654 |
private synchronized <T> Subscription onX(Map<Subscription, Consumer<T>> map, Consumer<T> listener) |
|
655 |
throws IllegalStateException { |
|
656 |
Objects.requireNonNull(listener); |
|
657 |
checkIfAlive(); |
|
658 |
Subscription token = new Subscription(map::remove); |
|
659 |
map.put(token, listener); |
|
660 |
return token; |
|
661 |
} |
|
662 |
||
663 |
private synchronized void notifyKeyStatusEvent(SnippetEvent event) { |
|
664 |
keyStatusListeners.values().forEach(l -> l.accept(event)); |
|
665 |
} |
|
666 |
||
667 |
private synchronized void notifyShutdownEvent(JShell state) { |
|
668 |
shutdownListeners.values().forEach(l -> l.accept(state)); |
|
669 |
} |
|
670 |
||
671 |
void closeDown() { |
|
672 |
if (!closed) { |
|
673 |
// Send only once |
|
674 |
closed = true; |
|
675 |
notifyShutdownEvent(this); |
|
676 |
} |
|
677 |
} |
|
678 |
||
679 |
/** |
|
680 |
* Check if this JShell has been closed |
|
681 |
* @throws IllegalStateException if it is closed |
|
682 |
*/ |
|
683 |
private void checkIfAlive() throws IllegalStateException { |
|
684 |
if (closed) { |
|
36990 | 685 |
throw new IllegalStateException(messageFormat("jshell.exc.closed", this)); |
33362 | 686 |
} |
687 |
} |
|
688 |
||
689 |
/** |
|
690 |
* Check a Snippet parameter coming from the API user |
|
691 |
* @param sn the Snippet to check |
|
692 |
* @throws NullPointerException if Snippet parameter is null |
|
693 |
* @throws IllegalArgumentException if Snippet is not from this JShell |
|
694 |
* @return the input Snippet (for chained calls) |
|
695 |
*/ |
|
696 |
private Snippet checkValidSnippet(Snippet sn) { |
|
697 |
if (sn == null) { |
|
36990 | 698 |
throw new NullPointerException(messageFormat("jshell.exc.null")); |
33362 | 699 |
} else { |
700 |
if (sn.key().state() != this) { |
|
36990 | 701 |
throw new IllegalArgumentException(messageFormat("jshell.exc.alien")); |
33362 | 702 |
} |
703 |
return sn; |
|
704 |
} |
|
705 |
} |
|
706 |
||
36990 | 707 |
/** |
708 |
* Format using resource bundle look-up using MessageFormat |
|
709 |
* |
|
710 |
* @param key the resource key |
|
711 |
* @param args |
|
712 |
*/ |
|
713 |
String messageFormat(String key, Object... args) { |
|
714 |
if (outputRB == null) { |
|
715 |
try { |
|
716 |
outputRB = ResourceBundle.getBundle(L10N_RB_NAME); |
|
717 |
} catch (MissingResourceException mre) { |
|
718 |
throw new InternalError("Cannot find ResourceBundle: " + L10N_RB_NAME); |
|
719 |
} |
|
720 |
} |
|
721 |
String s; |
|
722 |
try { |
|
723 |
s = outputRB.getString(key); |
|
724 |
} catch (MissingResourceException mre) { |
|
725 |
throw new InternalError("Missing resource: " + key + " in " + L10N_RB_NAME); |
|
726 |
} |
|
727 |
return MessageFormat.format(s, args); |
|
728 |
} |
|
729 |
||
33362 | 730 |
} |