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