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