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