author | rfield |
Mon, 25 Apr 2016 08:50:16 -0700 | |
changeset 37644 | 33cf53901cac |
parent 37005 | 71210037624f |
child 37751 | 77e7bb904a13 |
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.Map; |
|
34 |
import java.util.Set; |
|
35 |
import java.util.stream.Stream; |
|
36 |
import com.sun.jdi.ReferenceType; |
|
37 |
import jdk.jshell.Snippet.Kind; |
|
38 |
import jdk.jshell.Snippet.Status; |
|
39 |
import jdk.jshell.Snippet.SubKind; |
|
40 |
import jdk.jshell.TaskFactory.AnalyzeTask; |
|
41 |
import jdk.jshell.ClassTracker.ClassInfo; |
|
42 |
import jdk.jshell.TaskFactory.CompileTask; |
|
43 |
import static java.util.stream.Collectors.toList; |
|
44 |
import static java.util.stream.Collectors.toMap; |
|
45 |
import static java.util.stream.Collectors.toSet; |
|
46 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_EVNT; |
|
47 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; |
|
48 |
import static jdk.jshell.Snippet.Status.OVERWRITTEN; |
|
49 |
import static jdk.jshell.Snippet.Status.RECOVERABLE_DEFINED; |
|
50 |
import static jdk.jshell.Snippet.Status.RECOVERABLE_NOT_DEFINED; |
|
51 |
import static jdk.jshell.Snippet.Status.REJECTED; |
|
52 |
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
|
53 |
import static jdk.jshell.Util.PARSED_LOCALE; |
33362 | 54 |
import static jdk.jshell.Util.expunge; |
55 |
||
56 |
/** |
|
57 |
* Tracks the compilation and load of a new or updated snippet. |
|
58 |
* @author Robert Field |
|
59 |
*/ |
|
60 |
final class Unit { |
|
61 |
||
62 |
private final JShell state; |
|
63 |
private final Snippet si; |
|
64 |
private final Snippet siOld; |
|
65 |
private final boolean isDependency; |
|
66 |
private final boolean isNew; |
|
67 |
private final Snippet causalSnippet; |
|
68 |
private final DiagList generatedDiagnostics; |
|
69 |
||
70 |
private int seq; |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
71 |
private String classNameInitial; |
33362 | 72 |
private Wrap activeGuts; |
73 |
private Status status; |
|
74 |
private Status prevStatus; |
|
75 |
private boolean signatureChanged; |
|
76 |
private DiagList compilationDiagnostics; |
|
77 |
private DiagList recompilationDiagnostics = null; |
|
78 |
private List<String> unresolved; |
|
79 |
private SnippetEvent replaceOldEvent; |
|
80 |
private List<SnippetEvent> secondaryEvents; |
|
81 |
private boolean isAttemptingCorral; |
|
82 |
private List<ClassInfo> toRedefine; |
|
83 |
private boolean dependenciesNeeded; |
|
84 |
||
85 |
Unit(JShell state, Snippet si, Snippet causalSnippet, |
|
86 |
DiagList generatedDiagnostics) { |
|
87 |
this.state = state; |
|
88 |
this.si = si; |
|
89 |
this.isDependency = causalSnippet != null; |
|
90 |
this.siOld = isDependency |
|
91 |
? si |
|
92 |
: state.maps.getSnippet(si.key()); |
|
93 |
this.isNew = siOld == null; |
|
94 |
this.causalSnippet = causalSnippet; |
|
95 |
this.generatedDiagnostics = generatedDiagnostics; |
|
96 |
||
97 |
this.seq = isNew? 0 : siOld.sequenceNumber(); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
98 |
this.classNameInitial = isNew? "<none>" : siOld.className(); |
33362 | 99 |
this.prevStatus = (isNew || isDependency) |
100 |
? si.status() |
|
101 |
: siOld.status(); |
|
102 |
si.setSequenceNumber(seq); |
|
103 |
} |
|
104 |
||
105 |
// Drop entry |
|
106 |
Unit(JShell state, Snippet si) { |
|
107 |
this.state = state; |
|
108 |
this.si = si; |
|
109 |
this.siOld = null; |
|
110 |
this.isDependency = false; |
|
111 |
this.isNew = false; |
|
112 |
this.causalSnippet = null; |
|
113 |
this.generatedDiagnostics = new DiagList(); |
|
114 |
this.prevStatus = si.status(); |
|
115 |
si.setDropped(); |
|
116 |
this.status = si.status(); |
|
117 |
} |
|
118 |
||
119 |
@Override |
|
120 |
public int hashCode() { |
|
121 |
return si.hashCode(); |
|
122 |
} |
|
123 |
||
124 |
@Override |
|
125 |
public boolean equals(Object o) { |
|
126 |
return (o instanceof Unit) |
|
127 |
? si.equals(((Unit) o).si) |
|
128 |
: false; |
|
129 |
} |
|
130 |
||
131 |
Snippet snippet() { |
|
132 |
return si; |
|
133 |
} |
|
134 |
||
135 |
boolean isDependency() { |
|
136 |
return isDependency; |
|
137 |
} |
|
138 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
139 |
void initialize() { |
33362 | 140 |
isAttemptingCorral = false; |
141 |
dependenciesNeeded = false; |
|
142 |
toRedefine = null; // assure NPE if classToLoad not called |
|
143 |
activeGuts = si.guts(); |
|
144 |
markOldDeclarationOverwritten(); |
|
145 |
} |
|
146 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
147 |
// Set the outer wrap of our Snippet |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
148 |
void setWrap(Collection<Unit> exceptUnit, Collection<Unit> plusUnfiltered) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
149 |
if (isImport()) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
150 |
si.setOuterWrap(state.outerMap.wrapImport(activeGuts, si)); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
151 |
} else { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
152 |
// 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
|
153 |
List<Unit> units; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
154 |
if (snippet().kind() == Kind.METHOD) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
155 |
String name = ((MethodSnippet) snippet()).name(); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
156 |
units = plusUnfiltered.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
157 |
.filter(u -> u.snippet().kind() == Kind.METHOD && |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
158 |
((MethodSnippet) u.snippet()).name().equals(name)) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
159 |
.collect(toList()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
160 |
} else { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
161 |
units = Collections.singletonList(this); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
162 |
} |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
163 |
// Keys to exclude from imports |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
164 |
Set<Key> except = exceptUnit.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
165 |
.map(u -> u.snippet().key()) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
166 |
.collect(toSet()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
167 |
// Snippets to add to imports |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
168 |
Collection<Snippet> plus = plusUnfiltered.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
169 |
.filter(u -> !units.contains(u)) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
170 |
.map(u -> u.snippet()) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
171 |
.collect(toList()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
172 |
// Snippets to wrap in an outer |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
173 |
List<Snippet> snippets = units.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
174 |
.map(u -> u.snippet()) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
175 |
.collect(toList()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
176 |
// Snippet wraps to wrap in an outer |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
177 |
List<Wrap> wraps = units.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
178 |
.map(u -> u.activeGuts) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
179 |
.collect(toList()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
180 |
// Set the outer wrap for this snippet |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
181 |
si.setOuterWrap(state.outerMap.wrapInClass(except, plus, snippets, wraps)); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
182 |
} |
33362 | 183 |
} |
184 |
||
185 |
void setDiagnostics(AnalyzeTask ct) { |
|
186 |
setDiagnostics(ct.getDiagnostics().ofUnit(this)); |
|
187 |
} |
|
188 |
||
189 |
void setDiagnostics(DiagList diags) { |
|
190 |
compilationDiagnostics = diags; |
|
191 |
UnresolvedExtractor ue = new UnresolvedExtractor(diags); |
|
192 |
unresolved = ue.unresolved(); |
|
193 |
state.debug(DBG_GEN, "++setCompilationInfo() %s\n%s\n-- diags: %s\n", |
|
194 |
si, si.outerWrap().wrapped(), diags); |
|
195 |
} |
|
196 |
||
197 |
private boolean isRecoverable() { |
|
198 |
// Unit failed, use corralling if it is defined on this Snippet, |
|
199 |
// and either all the errors are resolution errors or this is a |
|
200 |
// redeclare of an existing method |
|
201 |
return compilationDiagnostics.hasErrors() |
|
202 |
&& si instanceof DeclarationSnippet |
|
203 |
&& (isDependency() |
|
204 |
|| (si.subKind() != SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND |
|
205 |
&& compilationDiagnostics.hasResolutionErrorsAndNoOthers())); |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* If it meets the conditions for corralling, install the corralled wrap |
|
210 |
* @return true is the corralled wrap was installed |
|
211 |
*/ |
|
212 |
boolean corralIfNeeded(Collection<Unit> working) { |
|
213 |
if (isRecoverable() |
|
214 |
&& si.corralled() != null) { |
|
215 |
activeGuts = si.corralled(); |
|
216 |
setWrap(working, working); |
|
217 |
return isAttemptingCorral = true; |
|
218 |
} |
|
219 |
return isAttemptingCorral = false; |
|
220 |
} |
|
221 |
||
222 |
void setCorralledDiagnostics(AnalyzeTask cct) { |
|
223 |
// set corralled diagnostics, but don't reset unresolved |
|
224 |
recompilationDiagnostics = cct.getDiagnostics().ofUnit(this); |
|
225 |
state.debug(DBG_GEN, "++recomp %s\n%s\n-- diags: %s\n", |
|
226 |
si, si.outerWrap().wrapped(), recompilationDiagnostics); |
|
227 |
} |
|
228 |
||
229 |
boolean smashingErrorDiagnostics(CompileTask ct) { |
|
230 |
if (isDefined()) { |
|
231 |
// set corralled diagnostics, but don't reset unresolved |
|
232 |
DiagList dl = ct.getDiagnostics().ofUnit(this); |
|
233 |
if (dl.hasErrors()) { |
|
234 |
setDiagnostics(dl); |
|
235 |
status = RECOVERABLE_NOT_DEFINED; |
|
236 |
// overwrite orginal bytes |
|
237 |
state.debug(DBG_GEN, "++smashingErrorDiagnostics %s\n%s\n-- diags: %s\n", |
|
238 |
si, si.outerWrap().wrapped(), dl); |
|
239 |
return true; |
|
240 |
} |
|
241 |
} |
|
242 |
return false; |
|
243 |
} |
|
244 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
245 |
void setStatus(AnalyzeTask at) { |
33362 | 246 |
if (!compilationDiagnostics.hasErrors()) { |
247 |
status = VALID; |
|
248 |
} else if (isRecoverable()) { |
|
249 |
if (isAttemptingCorral && !recompilationDiagnostics.hasErrors()) { |
|
250 |
status = RECOVERABLE_DEFINED; |
|
251 |
} else { |
|
252 |
status = RECOVERABLE_NOT_DEFINED; |
|
253 |
} |
|
254 |
} else { |
|
255 |
status = REJECTED; |
|
256 |
} |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
257 |
checkForOverwrite(at); |
33362 | 258 |
|
259 |
state.debug(DBG_GEN, "setStatus() %s - status: %s\n", |
|
260 |
si, status); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Must be called for each unit |
|
265 |
* @return |
|
266 |
*/ |
|
267 |
boolean isDefined() { |
|
268 |
return status.isDefined; |
|
269 |
} |
|
270 |
||
271 |
/** |
|
272 |
* Process the class information from the last compile. |
|
273 |
* Requires loading of returned list. |
|
274 |
* @return the list of classes to load |
|
275 |
*/ |
|
276 |
Stream<ClassInfo> classesToLoad(List<ClassInfo> cil) { |
|
277 |
toRedefine = new ArrayList<>(); |
|
278 |
List<ClassInfo> toLoad = new ArrayList<>(); |
|
279 |
if (status.isDefined && !isImport()) { |
|
280 |
cil.stream().forEach(ci -> { |
|
281 |
if (!ci.isLoaded()) { |
|
282 |
if (ci.getReferenceTypeOrNull() == null) { |
|
283 |
toLoad.add(ci); |
|
284 |
ci.setLoaded(); |
|
285 |
dependenciesNeeded = true; |
|
286 |
} else { |
|
287 |
toRedefine.add(ci); |
|
288 |
} |
|
289 |
} |
|
290 |
}); |
|
291 |
} |
|
292 |
return toLoad.stream(); |
|
293 |
} |
|
294 |
||
295 |
/** |
|
296 |
* Redefine classes needing redefine. |
|
297 |
* classesToLoad() must be called first. |
|
298 |
* @return true if all redefines succeeded (can be vacuously true) |
|
299 |
*/ |
|
300 |
boolean doRedefines() { |
|
301 |
if (toRedefine.isEmpty()) { |
|
302 |
return true; |
|
303 |
} |
|
304 |
Map<ReferenceType, byte[]> mp = toRedefine.stream() |
|
305 |
.collect(toMap(ci -> ci.getReferenceTypeOrNull(), ci -> ci.getBytes())); |
|
306 |
if (state.executionControl().commandRedefine(mp)) { |
|
307 |
// success, mark as loaded |
|
308 |
toRedefine.stream().forEach(ci -> ci.setLoaded()); |
|
309 |
return true; |
|
310 |
} else { |
|
311 |
// failed to redefine |
|
312 |
return false; |
|
313 |
} |
|
314 |
} |
|
315 |
||
316 |
void markForReplacement() { |
|
317 |
// increment for replace class wrapper |
|
318 |
si.setSequenceNumber(++seq); |
|
319 |
} |
|
320 |
||
321 |
private boolean isImport() { |
|
322 |
return si.kind() == Kind.IMPORT; |
|
323 |
} |
|
324 |
||
325 |
private boolean sigChanged() { |
|
326 |
return (status.isDefined != prevStatus.isDefined) |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
327 |
|| (status.isDefined && !si.className().equals(classNameInitial)) |
33362 | 328 |
|| signatureChanged; |
329 |
} |
|
330 |
||
331 |
Stream<Unit> effectedDependents() { |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
332 |
//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
|
333 |
// sigChanged(), dependenciesNeeded, status); |
33362 | 334 |
return sigChanged() || dependenciesNeeded || status == RECOVERABLE_NOT_DEFINED |
335 |
? dependents() |
|
336 |
: Stream.empty(); |
|
337 |
} |
|
338 |
||
339 |
Stream<Unit> dependents() { |
|
340 |
return state.maps.getDependents(si) |
|
341 |
.stream() |
|
342 |
.filter(xsi -> xsi != si && xsi.status().isActive) |
|
343 |
.map(xsi -> new Unit(state, xsi, si, new DiagList())); |
|
344 |
} |
|
345 |
||
346 |
void finish() { |
|
347 |
recordCompilation(); |
|
348 |
state.maps.installSnippet(si); |
|
349 |
} |
|
350 |
||
351 |
private void markOldDeclarationOverwritten() { |
|
352 |
if (si != siOld && siOld != null && siOld.status().isActive) { |
|
353 |
// Mark the old declaraion as replaced |
|
354 |
replaceOldEvent = new SnippetEvent(siOld, |
|
355 |
siOld.status(), OVERWRITTEN, |
|
356 |
false, si, null, null); |
|
357 |
siOld.setOverwritten(); |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
private DiagList computeDiagnostics() { |
|
362 |
DiagList diagnostics = new DiagList(); |
|
363 |
DiagList diags = compilationDiagnostics; |
|
364 |
if (status == RECOVERABLE_DEFINED || status == RECOVERABLE_NOT_DEFINED) { |
|
365 |
UnresolvedExtractor ue = new UnresolvedExtractor(diags); |
|
366 |
diagnostics.addAll(ue.otherAll()); |
|
367 |
} else { |
|
368 |
unresolved = Collections.emptyList(); |
|
369 |
diagnostics.addAll(diags); |
|
370 |
} |
|
371 |
diagnostics.addAll(generatedDiagnostics); |
|
372 |
return diagnostics; |
|
373 |
} |
|
374 |
||
375 |
private void recordCompilation() { |
|
376 |
state.maps.mapDependencies(si); |
|
377 |
DiagList diags = computeDiagnostics(); |
|
378 |
si.setCompilationStatus(status, unresolved, diags); |
|
379 |
state.debug(DBG_GEN, "recordCompilation: %s -- status %s, unresolved %s\n", |
|
380 |
si, status, unresolved); |
|
381 |
} |
|
382 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
383 |
private void checkForOverwrite(AnalyzeTask at) { |
33362 | 384 |
secondaryEvents = new ArrayList<>(); |
385 |
if (replaceOldEvent != null) secondaryEvents.add(replaceOldEvent); |
|
386 |
||
387 |
// Defined methods can overwrite methods of other (equivalent) snippets |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
388 |
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
|
389 |
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
|
390 |
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
|
391 |
String nqpt = computeQualifiedParameterTypes(at, msi); |
33362 | 392 |
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
|
393 |
msi.setQualifiedParamaterTypes(nqpt); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
394 |
Status overwrittenStatus = overwriteMatchingMethod(msi); |
33362 | 395 |
if (overwrittenStatus != null) { |
396 |
prevStatus = overwrittenStatus; |
|
397 |
signatureChanged = true; |
|
398 |
} |
|
399 |
} |
|
400 |
} |
|
401 |
} |
|
402 |
||
403 |
// Check if there is a method whose user-declared parameter types are |
|
404 |
// different (and thus has a different snippet) but whose compiled parameter |
|
405 |
// 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
|
406 |
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
|
407 |
String qpt = msi.qualifiedParameterTypes(); |
33362 | 408 |
|
409 |
// Look through all methods for a method of the same name, with the |
|
410 |
// same computed qualified parameter types |
|
411 |
Status overwrittenStatus = null; |
|
412 |
for (MethodSnippet sn : state.methods()) { |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
413 |
if (sn != null && sn != msi && sn.status().isActive && sn.name().equals(msi.name())) { |
33362 | 414 |
if (qpt.equals(sn.qualifiedParameterTypes())) { |
415 |
overwrittenStatus = sn.status(); |
|
416 |
SnippetEvent se = new SnippetEvent( |
|
417 |
sn, overwrittenStatus, OVERWRITTEN, |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
418 |
false, msi, null, null); |
33362 | 419 |
sn.setOverwritten(); |
420 |
secondaryEvents.add(se); |
|
421 |
state.debug(DBG_EVNT, |
|
422 |
"Overwrite event #%d -- key: %s before: %s status: %s sig: %b cause: %s\n", |
|
423 |
secondaryEvents.size(), se.snippet(), se.previousStatus(), |
|
424 |
se.status(), se.isSignatureChange(), se.causeSnippet()); |
|
425 |
} |
|
426 |
} |
|
427 |
} |
|
428 |
return overwrittenStatus; |
|
429 |
} |
|
430 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
431 |
private String computeQualifiedParameterTypes(AnalyzeTask at, MethodSnippet msi) { |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
432 |
String rawSig = TreeDissector.createBySnippet(at, msi).typeOfMethod(msi); |
33362 | 433 |
String signature = expunge(rawSig); |
434 |
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
|
435 |
|
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
436 |
// 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
|
437 |
// 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
|
438 |
return paren >= 0 |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
33362
diff
changeset
|
439 |
? 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
|
440 |
: msi.parameterTypes(); |
33362 | 441 |
} |
442 |
||
443 |
SnippetEvent event(String value, Exception exception) { |
|
444 |
boolean wasSignatureChanged = sigChanged(); |
|
445 |
state.debug(DBG_EVNT, "Snippet: %s id: %s before: %s status: %s sig: %b cause: %s\n", |
|
446 |
si, si.id(), prevStatus, si.status(), wasSignatureChanged, causalSnippet); |
|
447 |
return new SnippetEvent(si, prevStatus, si.status(), |
|
448 |
wasSignatureChanged, causalSnippet, value, exception); |
|
449 |
} |
|
450 |
||
451 |
List<SnippetEvent> secondaryEvents() { |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
452 |
return secondaryEvents==null |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
453 |
? Collections.emptyList() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37005
diff
changeset
|
454 |
: secondaryEvents; |
33362 | 455 |
} |
456 |
||
457 |
@Override |
|
458 |
public String toString() { |
|
459 |
return "Unit(" + si.name() + ")"; |
|
460 |
} |
|
461 |
||
462 |
/** |
|
463 |
* Separate out the unresolvedDependencies errors from both the other |
|
464 |
* corralling errors and the overall errors. |
|
465 |
*/ |
|
466 |
private static class UnresolvedExtractor { |
|
467 |
||
468 |
private static final String RESOLVE_ERROR_SYMBOL = "symbol:"; |
|
469 |
private static final String RESOLVE_ERROR_LOCATION = "location:"; |
|
470 |
||
471 |
//TODO extract from tree instead -- note: internationalization |
|
472 |
private final Set<String> unresolved = new LinkedHashSet<>(); |
|
473 |
private final DiagList otherErrors = new DiagList(); |
|
474 |
private final DiagList otherAll = new DiagList(); |
|
475 |
||
476 |
UnresolvedExtractor(DiagList diags) { |
|
477 |
for (Diag diag : diags) { |
|
478 |
if (diag.isError()) { |
|
479 |
if (diag.isResolutionError()) { |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
480 |
String m = diag.getMessage(PARSED_LOCALE); |
33362 | 481 |
int symPos = m.indexOf(RESOLVE_ERROR_SYMBOL); |
482 |
if (symPos >= 0) { |
|
483 |
m = m.substring(symPos + RESOLVE_ERROR_SYMBOL.length()); |
|
484 |
int symLoc = m.indexOf(RESOLVE_ERROR_LOCATION); |
|
485 |
if (symLoc >= 0) { |
|
486 |
m = m.substring(0, symLoc); |
|
487 |
} |
|
488 |
m = m.trim(); |
|
489 |
unresolved.add(m); |
|
490 |
continue; |
|
491 |
} |
|
492 |
} |
|
493 |
otherErrors.add(diag); |
|
494 |
} |
|
495 |
otherAll.add(diag); |
|
496 |
} |
|
497 |
} |
|
498 |
||
499 |
DiagList otherCorralledErrors() { |
|
500 |
return otherErrors; |
|
501 |
} |
|
502 |
||
503 |
DiagList otherAll() { |
|
504 |
return otherAll; |
|
505 |
} |
|
506 |
||
507 |
List<String> unresolved() { |
|
508 |
return new ArrayList<>(unresolved); |
|
509 |
} |
|
510 |
} |
|
511 |
} |