author | prr |
Mon, 12 Feb 2018 08:56:44 -0800 | |
changeset 49083 | 673be6f60323 |
parent 47216 | 71c04702a3d5 |
child 49416 | f14852315495 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jshell; |
|
27 |
||
28 |
import java.util.ArrayList; |
|
29 |
import java.util.Collection; |
|
30 |
import java.util.Collections; |
|
31 |
import java.util.LinkedHashSet; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Set; |
|
34 |
import java.util.stream.Stream; |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
35 |
import jdk.jshell.ClassTracker.ClassInfo; |
33362 | 36 |
import jdk.jshell.Snippet.Kind; |
37 |
import jdk.jshell.Snippet.Status; |
|
38 |
import jdk.jshell.Snippet.SubKind; |
|
39 |
import jdk.jshell.TaskFactory.AnalyzeTask; |
|
40 |
import jdk.jshell.TaskFactory.CompileTask; |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
41 |
import jdk.jshell.spi.ExecutionControl.ClassBytecodes; |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
42 |
import jdk.jshell.spi.ExecutionControl.ClassInstallException; |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
43 |
import jdk.jshell.spi.ExecutionControl.EngineTerminationException; |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
44 |
import jdk.jshell.spi.ExecutionControl.NotImplementedException; |
33362 | 45 |
import static java.util.stream.Collectors.toList; |
46 |
import static java.util.stream.Collectors.toSet; |
|
47 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_EVNT; |
|
48 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; |
|
43134
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
42827
diff
changeset
|
49 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_WRAP; |
33362 | 50 |
import static jdk.jshell.Snippet.Status.OVERWRITTEN; |
51 |
import static jdk.jshell.Snippet.Status.RECOVERABLE_DEFINED; |
|
52 |
import static jdk.jshell.Snippet.Status.RECOVERABLE_NOT_DEFINED; |
|
53 |
import static jdk.jshell.Snippet.Status.REJECTED; |
|
54 |
import static jdk.jshell.Snippet.Status.VALID; |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
55 |
import static jdk.jshell.Util.PARSED_LOCALE; |
33362 | 56 |
import static jdk.jshell.Util.expunge; |
57 |
||
58 |
/** |
|
59 |
* Tracks the compilation and load of a new or updated snippet. |
|
60 |
* @author Robert Field |
|
61 |
*/ |
|
62 |
final class Unit { |
|
63 |
||
64 |
private final JShell state; |
|
65 |
private final Snippet si; |
|
66 |
private final Snippet siOld; |
|
67 |
private final boolean isDependency; |
|
68 |
private final boolean isNew; |
|
69 |
private final Snippet causalSnippet; |
|
70 |
private final DiagList generatedDiagnostics; |
|
71 |
||
72 |
private int seq; |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
73 |
private String classNameInitial; |
33362 | 74 |
private Wrap activeGuts; |
75 |
private Status status; |
|
76 |
private Status prevStatus; |
|
77 |
private boolean signatureChanged; |
|
78 |
private DiagList compilationDiagnostics; |
|
79 |
private DiagList recompilationDiagnostics = null; |
|
80 |
private List<String> unresolved; |
|
81 |
private SnippetEvent replaceOldEvent; |
|
82 |
private List<SnippetEvent> secondaryEvents; |
|
83 |
private boolean isAttemptingCorral; |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
84 |
private List<ClassInfo> toRedefine; |
33362 | 85 |
private boolean dependenciesNeeded; |
86 |
||
87 |
Unit(JShell state, Snippet si, Snippet causalSnippet, |
|
88 |
DiagList generatedDiagnostics) { |
|
89 |
this.state = state; |
|
90 |
this.si = si; |
|
91 |
this.isDependency = causalSnippet != null; |
|
92 |
this.siOld = isDependency |
|
93 |
? si |
|
94 |
: state.maps.getSnippet(si.key()); |
|
95 |
this.isNew = siOld == null; |
|
96 |
this.causalSnippet = causalSnippet; |
|
97 |
this.generatedDiagnostics = generatedDiagnostics; |
|
98 |
||
99 |
this.seq = isNew? 0 : siOld.sequenceNumber(); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
100 |
this.classNameInitial = isNew? "<none>" : siOld.className(); |
33362 | 101 |
this.prevStatus = (isNew || isDependency) |
102 |
? si.status() |
|
103 |
: siOld.status(); |
|
104 |
si.setSequenceNumber(seq); |
|
105 |
} |
|
106 |
||
107 |
// Drop entry |
|
108 |
Unit(JShell state, Snippet si) { |
|
109 |
this.state = state; |
|
110 |
this.si = si; |
|
111 |
this.siOld = null; |
|
112 |
this.isDependency = false; |
|
113 |
this.isNew = false; |
|
114 |
this.causalSnippet = null; |
|
115 |
this.generatedDiagnostics = new DiagList(); |
|
116 |
this.prevStatus = si.status(); |
|
117 |
si.setDropped(); |
|
118 |
this.status = si.status(); |
|
119 |
} |
|
120 |
||
121 |
@Override |
|
122 |
public int hashCode() { |
|
123 |
return si.hashCode(); |
|
124 |
} |
|
125 |
||
126 |
@Override |
|
127 |
public boolean equals(Object o) { |
|
128 |
return (o instanceof Unit) |
|
129 |
? si.equals(((Unit) o).si) |
|
130 |
: false; |
|
131 |
} |
|
132 |
||
133 |
Snippet snippet() { |
|
134 |
return si; |
|
135 |
} |
|
136 |
||
137 |
boolean isDependency() { |
|
138 |
return isDependency; |
|
139 |
} |
|
140 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
141 |
void initialize() { |
33362 | 142 |
isAttemptingCorral = false; |
143 |
dependenciesNeeded = false; |
|
144 |
toRedefine = null; // assure NPE if classToLoad not called |
|
145 |
activeGuts = si.guts(); |
|
146 |
markOldDeclarationOverwritten(); |
|
147 |
} |
|
148 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
149 |
// Set the outer wrap of our Snippet |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
150 |
void setWrap(Collection<Unit> exceptUnit, Collection<Unit> plusUnfiltered) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
151 |
if (isImport()) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
152 |
si.setOuterWrap(state.outerMap.wrapImport(activeGuts, si)); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
153 |
} else { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
154 |
// Collect Units for be wrapped together. Just this except for overloaded methods |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
155 |
List<Unit> units; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
156 |
if (snippet().kind() == Kind.METHOD) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
157 |
String name = ((MethodSnippet) snippet()).name(); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
158 |
units = plusUnfiltered.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
159 |
.filter(u -> u.snippet().kind() == Kind.METHOD && |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
160 |
((MethodSnippet) u.snippet()).name().equals(name)) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
161 |
.collect(toList()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
162 |
} else { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
163 |
units = Collections.singletonList(this); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
164 |
} |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
165 |
// Keys to exclude from imports |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
166 |
Set<Key> except = exceptUnit.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
167 |
.map(u -> u.snippet().key()) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
168 |
.collect(toSet()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
169 |
// Snippets to add to imports |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
170 |
Collection<Snippet> plus = plusUnfiltered.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
171 |
.filter(u -> !units.contains(u)) |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40304
diff
changeset
|
172 |
.map(Unit::snippet) |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
173 |
.collect(toList()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
174 |
// Snippets to wrap in an outer |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
175 |
List<Snippet> snippets = units.stream() |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40304
diff
changeset
|
176 |
.map(Unit::snippet) |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
177 |
.collect(toList()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
178 |
// Snippet wraps to wrap in an outer |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
179 |
List<Wrap> wraps = units.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
180 |
.map(u -> u.activeGuts) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
181 |
.collect(toList()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
182 |
// Set the outer wrap for this snippet |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
183 |
si.setOuterWrap(state.outerMap.wrapInClass(except, plus, snippets, wraps)); |
43134
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
42827
diff
changeset
|
184 |
state.debug(DBG_WRAP, "++setWrap() %s\n%s\n", |
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
42827
diff
changeset
|
185 |
si, si.outerWrap().wrapped()); |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
186 |
} |
33362 | 187 |
} |
188 |
||
189 |
void setDiagnostics(AnalyzeTask ct) { |
|
190 |
setDiagnostics(ct.getDiagnostics().ofUnit(this)); |
|
191 |
} |
|
192 |
||
193 |
void setDiagnostics(DiagList diags) { |
|
194 |
compilationDiagnostics = diags; |
|
195 |
UnresolvedExtractor ue = new UnresolvedExtractor(diags); |
|
196 |
unresolved = ue.unresolved(); |
|
197 |
state.debug(DBG_GEN, "++setCompilationInfo() %s\n%s\n-- diags: %s\n", |
|
198 |
si, si.outerWrap().wrapped(), diags); |
|
199 |
} |
|
200 |
||
201 |
private boolean isRecoverable() { |
|
202 |
// Unit failed, use corralling if it is defined on this Snippet, |
|
203 |
// and either all the errors are resolution errors or this is a |
|
204 |
// redeclare of an existing method |
|
205 |
return compilationDiagnostics.hasErrors() |
|
206 |
&& si instanceof DeclarationSnippet |
|
207 |
&& (isDependency() |
|
208 |
|| (si.subKind() != SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND |
|
209 |
&& compilationDiagnostics.hasResolutionErrorsAndNoOthers())); |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* If it meets the conditions for corralling, install the corralled wrap |
|
214 |
* @return true is the corralled wrap was installed |
|
215 |
*/ |
|
216 |
boolean corralIfNeeded(Collection<Unit> working) { |
|
217 |
if (isRecoverable() |
|
218 |
&& si.corralled() != null) { |
|
219 |
activeGuts = si.corralled(); |
|
220 |
setWrap(working, working); |
|
221 |
return isAttemptingCorral = true; |
|
222 |
} |
|
223 |
return isAttemptingCorral = false; |
|
224 |
} |
|
225 |
||
226 |
void setCorralledDiagnostics(AnalyzeTask cct) { |
|
227 |
// set corralled diagnostics, but don't reset unresolved |
|
228 |
recompilationDiagnostics = cct.getDiagnostics().ofUnit(this); |
|
229 |
state.debug(DBG_GEN, "++recomp %s\n%s\n-- diags: %s\n", |
|
230 |
si, si.outerWrap().wrapped(), recompilationDiagnostics); |
|
231 |
} |
|
232 |
||
233 |
boolean smashingErrorDiagnostics(CompileTask ct) { |
|
234 |
if (isDefined()) { |
|
235 |
// set corralled diagnostics, but don't reset unresolved |
|
236 |
DiagList dl = ct.getDiagnostics().ofUnit(this); |
|
237 |
if (dl.hasErrors()) { |
|
238 |
setDiagnostics(dl); |
|
239 |
status = RECOVERABLE_NOT_DEFINED; |
|
240 |
// overwrite orginal bytes |
|
241 |
state.debug(DBG_GEN, "++smashingErrorDiagnostics %s\n%s\n-- diags: %s\n", |
|
242 |
si, si.outerWrap().wrapped(), dl); |
|
243 |
return true; |
|
244 |
} |
|
245 |
} |
|
246 |
return false; |
|
247 |
} |
|
248 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
249 |
void setStatus(AnalyzeTask at) { |
33362 | 250 |
if (!compilationDiagnostics.hasErrors()) { |
251 |
status = VALID; |
|
252 |
} else if (isRecoverable()) { |
|
253 |
if (isAttemptingCorral && !recompilationDiagnostics.hasErrors()) { |
|
254 |
status = RECOVERABLE_DEFINED; |
|
255 |
} else { |
|
256 |
status = RECOVERABLE_NOT_DEFINED; |
|
257 |
} |
|
258 |
} else { |
|
259 |
status = REJECTED; |
|
260 |
} |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
261 |
checkForOverwrite(at); |
33362 | 262 |
|
263 |
state.debug(DBG_GEN, "setStatus() %s - status: %s\n", |
|
264 |
si, status); |
|
265 |
} |
|
266 |
||
267 |
boolean isDefined() { |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
268 |
return status.isDefined(); |
33362 | 269 |
} |
270 |
||
271 |
/** |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
272 |
* Process the class information from the last compile. Requires loading of |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
273 |
* returned list. |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
274 |
* |
33362 | 275 |
* @return the list of classes to load |
276 |
*/ |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
277 |
Stream<ClassBytecodes> classesToLoad(List<String> classnames) { |
33362 | 278 |
toRedefine = new ArrayList<>(); |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
279 |
List<ClassBytecodes> toLoad = new ArrayList<>(); |
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
280 |
if (status.isDefined() && !isImport()) { |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
281 |
// Classes should only be loaded/redefined if the compile left them |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
282 |
// in a defined state. Imports do not have code and are not loaded. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
283 |
for (String cn : classnames) { |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
284 |
ClassInfo ci = state.classTracker.get(cn); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
285 |
if (ci.isLoaded()) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
286 |
if (ci.isCurrent()) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
287 |
// nothing to do |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
288 |
} else { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
289 |
toRedefine.add(ci); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
290 |
} |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
291 |
} else { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
292 |
// If not loaded, add to the list of classes to load. |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
293 |
toLoad.add(ci.toClassBytecodes()); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
294 |
dependenciesNeeded = true; |
33362 | 295 |
} |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
296 |
} |
33362 | 297 |
} |
298 |
return toLoad.stream(); |
|
299 |
} |
|
300 |
||
301 |
/** |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
302 |
* Redefine classes needing redefine. classesToLoad() must be called first. |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
303 |
* |
33362 | 304 |
* @return true if all redefines succeeded (can be vacuously true) |
305 |
*/ |
|
306 |
boolean doRedefines() { |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
307 |
if (toRedefine.isEmpty()) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
308 |
return true; |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
309 |
} |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
310 |
ClassBytecodes[] cbcs = toRedefine.stream() |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40304
diff
changeset
|
311 |
.map(ClassInfo::toClassBytecodes) |
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
40304
diff
changeset
|
312 |
.toArray(ClassBytecodes[]::new); |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
313 |
try { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
314 |
state.executionControl().redefine(cbcs); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
315 |
state.classTracker.markLoaded(cbcs); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
316 |
return true; |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
317 |
} catch (ClassInstallException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
318 |
state.classTracker.markLoaded(cbcs, ex.installed()); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
319 |
return false; |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
320 |
} catch (EngineTerminationException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
321 |
state.closeDown(); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
322 |
return false; |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
323 |
} catch (NotImplementedException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
324 |
return false; |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
38908
diff
changeset
|
325 |
} |
33362 | 326 |
} |
327 |
||
328 |
void markForReplacement() { |
|
329 |
// increment for replace class wrapper |
|
330 |
si.setSequenceNumber(++seq); |
|
331 |
} |
|
332 |
||
333 |
private boolean isImport() { |
|
334 |
return si.kind() == Kind.IMPORT; |
|
335 |
} |
|
336 |
||
337 |
private boolean sigChanged() { |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
338 |
return (status.isDefined() != prevStatus.isDefined()) |
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
339 |
|| (status.isDefined() && !si.className().equals(classNameInitial)) |
33362 | 340 |
|| signatureChanged; |
341 |
} |
|
342 |
||
343 |
Stream<Unit> effectedDependents() { |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
344 |
//System.err.printf("effectedDependents sigChanged=%b dependenciesNeeded=%b status=%s\n", |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
345 |
// sigChanged(), dependenciesNeeded, status); |
33362 | 346 |
return sigChanged() || dependenciesNeeded || status == RECOVERABLE_NOT_DEFINED |
347 |
? dependents() |
|
348 |
: Stream.empty(); |
|
349 |
} |
|
350 |
||
351 |
Stream<Unit> dependents() { |
|
352 |
return state.maps.getDependents(si) |
|
353 |
.stream() |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
354 |
.filter(xsi -> xsi != si && xsi.status().isActive()) |
33362 | 355 |
.map(xsi -> new Unit(state, xsi, si, new DiagList())); |
356 |
} |
|
357 |
||
358 |
void finish() { |
|
359 |
recordCompilation(); |
|
360 |
state.maps.installSnippet(si); |
|
361 |
} |
|
362 |
||
363 |
private void markOldDeclarationOverwritten() { |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
364 |
if (si != siOld && siOld != null && siOld.status().isActive()) { |
33362 | 365 |
// Mark the old declaraion as replaced |
366 |
replaceOldEvent = new SnippetEvent(siOld, |
|
367 |
siOld.status(), OVERWRITTEN, |
|
368 |
false, si, null, null); |
|
369 |
siOld.setOverwritten(); |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
private DiagList computeDiagnostics() { |
|
374 |
DiagList diagnostics = new DiagList(); |
|
375 |
DiagList diags = compilationDiagnostics; |
|
376 |
if (status == RECOVERABLE_DEFINED || status == RECOVERABLE_NOT_DEFINED) { |
|
377 |
UnresolvedExtractor ue = new UnresolvedExtractor(diags); |
|
378 |
diagnostics.addAll(ue.otherAll()); |
|
379 |
} else { |
|
380 |
unresolved = Collections.emptyList(); |
|
381 |
diagnostics.addAll(diags); |
|
382 |
} |
|
383 |
diagnostics.addAll(generatedDiagnostics); |
|
384 |
return diagnostics; |
|
385 |
} |
|
386 |
||
387 |
private void recordCompilation() { |
|
388 |
state.maps.mapDependencies(si); |
|
389 |
DiagList diags = computeDiagnostics(); |
|
390 |
si.setCompilationStatus(status, unresolved, diags); |
|
391 |
state.debug(DBG_GEN, "recordCompilation: %s -- status %s, unresolved %s\n", |
|
392 |
si, status, unresolved); |
|
393 |
} |
|
394 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
395 |
private void checkForOverwrite(AnalyzeTask at) { |
33362 | 396 |
secondaryEvents = new ArrayList<>(); |
397 |
if (replaceOldEvent != null) secondaryEvents.add(replaceOldEvent); |
|
398 |
||
399 |
// Defined methods can overwrite methods of other (equivalent) snippets |
|
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
400 |
if (isNew && si.kind() == Kind.METHOD && status.isDefined()) { |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
401 |
MethodSnippet msi = (MethodSnippet)si; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
402 |
String oqpt = msi.qualifiedParameterTypes(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
403 |
String nqpt = computeQualifiedParameterTypes(at, msi); |
33362 | 404 |
if (!nqpt.equals(oqpt)) { |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
405 |
msi.setQualifiedParamaterTypes(nqpt); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
406 |
Status overwrittenStatus = overwriteMatchingMethod(msi); |
33362 | 407 |
if (overwrittenStatus != null) { |
408 |
prevStatus = overwrittenStatus; |
|
409 |
signatureChanged = true; |
|
410 |
} |
|
411 |
} |
|
412 |
} |
|
413 |
} |
|
414 |
||
415 |
// Check if there is a method whose user-declared parameter types are |
|
416 |
// different (and thus has a different snippet) but whose compiled parameter |
|
417 |
// types are the same. if so, consider it an overwrite replacement. |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
418 |
private Status overwriteMatchingMethod(MethodSnippet msi) { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
419 |
String qpt = msi.qualifiedParameterTypes(); |
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
420 |
List<MethodSnippet> matching = state.methods() |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
421 |
.filter(sn -> |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
422 |
sn != null |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
423 |
&& sn != msi |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
424 |
&& sn.status().isActive() |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
425 |
&& sn.name().equals(msi.name()) |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
426 |
&& qpt.equals(sn.qualifiedParameterTypes())) |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
427 |
.collect(toList()); |
33362 | 428 |
|
429 |
// Look through all methods for a method of the same name, with the |
|
430 |
// same computed qualified parameter types |
|
431 |
Status overwrittenStatus = null; |
|
40304
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
432 |
for (MethodSnippet sn : matching) { |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
433 |
overwrittenStatus = sn.status(); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
434 |
SnippetEvent se = new SnippetEvent( |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
435 |
sn, overwrittenStatus, OVERWRITTEN, |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
436 |
false, msi, null, null); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
437 |
sn.setOverwritten(); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
438 |
secondaryEvents.add(se); |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
439 |
state.debug(DBG_EVNT, |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
440 |
"Overwrite event #%d -- key: %s before: %s status: %s sig: %b cause: %s\n", |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
441 |
secondaryEvents.size(), se.snippet(), se.previousStatus(), |
0318f4e75c6d
8143964: JShell API: convert query responses to Stream instead of List
rfield
parents:
39807
diff
changeset
|
442 |
se.status(), se.isSignatureChange(), se.causeSnippet()); |
33362 | 443 |
} |
444 |
return overwrittenStatus; |
|
445 |
} |
|
446 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
447 |
private String computeQualifiedParameterTypes(AnalyzeTask at, MethodSnippet msi) { |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
448 |
String rawSig = TreeDissector.createBySnippet(at, msi).typeOfMethod(msi); |
33362 | 449 |
String signature = expunge(rawSig); |
450 |
int paren = signature.lastIndexOf(')'); |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
451 |
|
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
452 |
// Extract the parameter type string from the method signature, |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
453 |
// if method did not compile use the user-supplied parameter types |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
454 |
return paren >= 0 |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
455 |
? signature.substring(0, paren + 1) |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
456 |
: msi.parameterTypes(); |
33362 | 457 |
} |
458 |
||
37751
77e7bb904a13
8139837: JShell API: make a common JShellException
rfield
parents:
37644
diff
changeset
|
459 |
SnippetEvent event(String value, JShellException exception) { |
33362 | 460 |
boolean wasSignatureChanged = sigChanged(); |
461 |
state.debug(DBG_EVNT, "Snippet: %s id: %s before: %s status: %s sig: %b cause: %s\n", |
|
462 |
si, si.id(), prevStatus, si.status(), wasSignatureChanged, causalSnippet); |
|
463 |
return new SnippetEvent(si, prevStatus, si.status(), |
|
464 |
wasSignatureChanged, causalSnippet, value, exception); |
|
465 |
} |
|
466 |
||
467 |
List<SnippetEvent> secondaryEvents() { |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
468 |
return secondaryEvents==null |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
469 |
? Collections.emptyList() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
470 |
: secondaryEvents; |
33362 | 471 |
} |
472 |
||
473 |
@Override |
|
474 |
public String toString() { |
|
475 |
return "Unit(" + si.name() + ")"; |
|
476 |
} |
|
477 |
||
478 |
/** |
|
479 |
* Separate out the unresolvedDependencies errors from both the other |
|
480 |
* corralling errors and the overall errors. |
|
481 |
*/ |
|
482 |
private static class UnresolvedExtractor { |
|
483 |
||
484 |
private static final String RESOLVE_ERROR_SYMBOL = "symbol:"; |
|
485 |
private static final String RESOLVE_ERROR_LOCATION = "location:"; |
|
486 |
||
487 |
//TODO extract from tree instead -- note: internationalization |
|
488 |
private final Set<String> unresolved = new LinkedHashSet<>(); |
|
489 |
private final DiagList otherErrors = new DiagList(); |
|
490 |
private final DiagList otherAll = new DiagList(); |
|
491 |
||
492 |
UnresolvedExtractor(DiagList diags) { |
|
493 |
for (Diag diag : diags) { |
|
494 |
if (diag.isError()) { |
|
495 |
if (diag.isResolutionError()) { |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
496 |
String m = diag.getMessage(PARSED_LOCALE); |
33362 | 497 |
int symPos = m.indexOf(RESOLVE_ERROR_SYMBOL); |
498 |
if (symPos >= 0) { |
|
499 |
m = m.substring(symPos + RESOLVE_ERROR_SYMBOL.length()); |
|
500 |
int symLoc = m.indexOf(RESOLVE_ERROR_LOCATION); |
|
501 |
if (symLoc >= 0) { |
|
502 |
m = m.substring(0, symLoc); |
|
503 |
} |
|
504 |
m = m.trim(); |
|
505 |
unresolved.add(m); |
|
506 |
continue; |
|
507 |
} |
|
508 |
} |
|
509 |
otherErrors.add(diag); |
|
510 |
} |
|
511 |
otherAll.add(diag); |
|
512 |
} |
|
513 |
} |
|
514 |
||
515 |
DiagList otherCorralledErrors() { |
|
516 |
return otherErrors; |
|
517 |
} |
|
518 |
||
519 |
DiagList otherAll() { |
|
520 |
return otherAll; |
|
521 |
} |
|
522 |
||
523 |
List<String> unresolved() { |
|
524 |
return new ArrayList<>(unresolved); |
|
525 |
} |
|
526 |
} |
|
527 |
} |