author | rfield |
Sat, 21 May 2016 22:32:08 -0700 | |
changeset 38535 | 4a25025e0b0d |
parent 37751 | 77e7bb904a13 |
child 38908 | f0c186d76c8a |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
2 |
* Copyright (c) 2014, 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 |
package jdk.jshell; |
|
26 |
||
27 |
import java.util.ArrayList; |
|
28 |
import java.util.Collection; |
|
29 |
import java.util.Collections; |
|
30 |
import java.util.List; |
|
31 |
import java.util.Locale; |
|
32 |
import java.util.regex.Matcher; |
|
33 |
import java.util.regex.Pattern; |
|
34 |
import java.util.stream.Collectors; |
|
35 |
import javax.lang.model.element.Modifier; |
|
36 |
import com.sun.source.tree.ArrayTypeTree; |
|
37 |
import com.sun.source.tree.AssignmentTree; |
|
38 |
import com.sun.source.tree.ClassTree; |
|
39 |
import com.sun.source.tree.ExpressionTree; |
|
40 |
import com.sun.source.tree.IdentifierTree; |
|
41 |
import com.sun.source.tree.MethodTree; |
|
42 |
import com.sun.source.tree.ModifiersTree; |
|
43 |
import com.sun.source.tree.Tree; |
|
44 |
import com.sun.source.tree.VariableTree; |
|
45 |
import com.sun.tools.javac.tree.JCTree; |
|
46 |
import com.sun.tools.javac.tree.Pretty; |
|
47 |
import java.io.IOException; |
|
48 |
import java.io.StringWriter; |
|
49 |
import java.io.Writer; |
|
50 |
import java.util.LinkedHashSet; |
|
51 |
import java.util.Set; |
|
52 |
import jdk.jshell.Key.ErroneousKey; |
|
53 |
import jdk.jshell.Key.MethodKey; |
|
36499 | 54 |
import jdk.jshell.Key.TypeDeclKey; |
33362 | 55 |
import jdk.jshell.Snippet.SubKind; |
56 |
import jdk.jshell.TaskFactory.AnalyzeTask; |
|
57 |
import jdk.jshell.TaskFactory.BaseTask; |
|
58 |
import jdk.jshell.TaskFactory.CompileTask; |
|
59 |
import jdk.jshell.TaskFactory.ParseTask; |
|
60 |
import jdk.jshell.TreeDissector.ExpressionInfo; |
|
61 |
import jdk.jshell.Wrap.Range; |
|
62 |
import jdk.jshell.Snippet.Status; |
|
63 |
import static java.util.stream.Collectors.toList; |
|
64 |
import static java.util.stream.Collectors.toSet; |
|
65 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
66 |
import static jdk.jshell.Util.DOIT_METHOD_NAME; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
67 |
import static jdk.jshell.Util.PREFIX_PATTERN; |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
68 |
import static jdk.jshell.Util.expunge; |
33362 | 69 |
import static jdk.jshell.Snippet.SubKind.SINGLE_TYPE_IMPORT_SUBKIND; |
70 |
import static jdk.jshell.Snippet.SubKind.SINGLE_STATIC_IMPORT_SUBKIND; |
|
71 |
import static jdk.jshell.Snippet.SubKind.TYPE_IMPORT_ON_DEMAND_SUBKIND; |
|
72 |
import static jdk.jshell.Snippet.SubKind.STATIC_IMPORT_ON_DEMAND_SUBKIND; |
|
73 |
||
74 |
/** |
|
75 |
* The Evaluation Engine. Source internal analysis, wrapping control, |
|
76 |
* compilation, declaration. redefinition, replacement, and execution. |
|
77 |
* |
|
78 |
* @author Robert Field |
|
79 |
*/ |
|
80 |
class Eval { |
|
81 |
||
82 |
private static final Pattern IMPORT_PATTERN = Pattern.compile("import\\p{javaWhitespace}+(?<static>static\\p{javaWhitespace}+)?(?<fullname>[\\p{L}\\p{N}_\\$\\.]+\\.(?<name>[\\p{L}\\p{N}_\\$]+|\\*))"); |
|
83 |
||
84 |
private int varNumber = 0; |
|
85 |
||
86 |
private final JShell state; |
|
87 |
||
88 |
Eval(JShell state) { |
|
89 |
this.state = state; |
|
90 |
} |
|
91 |
||
92 |
List<SnippetEvent> eval(String userSource) throws IllegalStateException { |
|
93 |
String compileSource = Util.trimEnd(new MaskCommentsAndModifiers(userSource, false).cleared()); |
|
94 |
if (compileSource.length() == 0) { |
|
95 |
return Collections.emptyList(); |
|
96 |
} |
|
97 |
// String folding messes up position information. |
|
98 |
ParseTask pt = state.taskFactory.new ParseTask(compileSource); |
|
99 |
if (pt.getDiagnostics().hasOtherThanNotStatementErrors()) { |
|
100 |
return compileFailResult(pt, userSource); |
|
101 |
} |
|
102 |
||
103 |
List<? extends Tree> units = pt.units(); |
|
104 |
if (units.isEmpty()) { |
|
105 |
return compileFailResult(pt, userSource); |
|
106 |
} |
|
107 |
// Erase illegal modifiers |
|
108 |
compileSource = new MaskCommentsAndModifiers(compileSource, true).cleared(); |
|
109 |
Tree unitTree = units.get(0); |
|
110 |
state.debug(DBG_GEN, "Kind: %s -- %s\n", unitTree.getKind(), unitTree); |
|
111 |
switch (unitTree.getKind()) { |
|
112 |
case IMPORT: |
|
113 |
return processImport(userSource, compileSource); |
|
114 |
case VARIABLE: |
|
115 |
return processVariables(userSource, units, compileSource, pt); |
|
116 |
case EXPRESSION_STATEMENT: |
|
117 |
return processExpression(userSource, compileSource); |
|
118 |
case CLASS: |
|
119 |
return processClass(userSource, unitTree, compileSource, SubKind.CLASS_SUBKIND, pt); |
|
120 |
case ENUM: |
|
121 |
return processClass(userSource, unitTree, compileSource, SubKind.ENUM_SUBKIND, pt); |
|
122 |
case ANNOTATION_TYPE: |
|
123 |
return processClass(userSource, unitTree, compileSource, SubKind.ANNOTATION_TYPE_SUBKIND, pt); |
|
124 |
case INTERFACE: |
|
125 |
return processClass(userSource, unitTree, compileSource, SubKind.INTERFACE_SUBKIND, pt); |
|
126 |
case METHOD: |
|
127 |
return processMethod(userSource, unitTree, compileSource, pt); |
|
128 |
default: |
|
129 |
return processStatement(userSource, compileSource); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
private List<SnippetEvent> processImport(String userSource, String compileSource) { |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
134 |
Wrap guts = Wrap.simpleWrap(compileSource); |
33362 | 135 |
Matcher mat = IMPORT_PATTERN.matcher(compileSource); |
136 |
String fullname; |
|
137 |
String name; |
|
138 |
boolean isStatic; |
|
139 |
if (mat.find()) { |
|
140 |
isStatic = mat.group("static") != null; |
|
141 |
name = mat.group("name"); |
|
142 |
fullname = mat.group("fullname"); |
|
143 |
} else { |
|
144 |
// bad import -- fake it |
|
145 |
isStatic = compileSource.contains("static"); |
|
146 |
name = fullname = compileSource; |
|
147 |
} |
|
148 |
String fullkey = (isStatic ? "static-" : "") + fullname; |
|
149 |
boolean isStar = name.equals("*"); |
|
150 |
String keyName = isStar |
|
151 |
? fullname |
|
152 |
: name; |
|
153 |
SubKind snippetKind = isStar |
|
154 |
? (isStatic ? STATIC_IMPORT_ON_DEMAND_SUBKIND : TYPE_IMPORT_ON_DEMAND_SUBKIND) |
|
155 |
: (isStatic ? SINGLE_STATIC_IMPORT_SUBKIND : SINGLE_TYPE_IMPORT_SUBKIND); |
|
156 |
Snippet snip = new ImportSnippet(state.keyMap.keyForImport(keyName, snippetKind), |
|
157 |
userSource, guts, fullname, name, snippetKind, fullkey, isStatic, isStar); |
|
158 |
return declare(snip); |
|
159 |
} |
|
160 |
||
161 |
private static class EvalPretty extends Pretty { |
|
162 |
||
163 |
private final Writer out; |
|
164 |
||
165 |
public EvalPretty(Writer writer, boolean bln) { |
|
166 |
super(writer, bln); |
|
167 |
this.out = writer; |
|
168 |
} |
|
169 |
||
170 |
/** |
|
171 |
* Print string, DO NOT replacing all non-ascii character with unicode |
|
172 |
* escapes. |
|
173 |
*/ |
|
174 |
@Override |
|
175 |
public void print(Object o) throws IOException { |
|
176 |
out.write(o.toString()); |
|
177 |
} |
|
178 |
||
179 |
static String prettyExpr(JCTree tree, boolean bln) { |
|
180 |
StringWriter out = new StringWriter(); |
|
181 |
try { |
|
182 |
new EvalPretty(out, bln).printExpr(tree); |
|
183 |
} catch (IOException e) { |
|
184 |
throw new AssertionError(e); |
|
185 |
} |
|
186 |
return out.toString(); |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
private List<SnippetEvent> processVariables(String userSource, List<? extends Tree> units, String compileSource, ParseTask pt) { |
|
191 |
List<SnippetEvent> allEvents = new ArrayList<>(); |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
192 |
TreeDissector dis = TreeDissector.createByFirstClass(pt); |
33362 | 193 |
for (Tree unitTree : units) { |
194 |
VariableTree vt = (VariableTree) unitTree; |
|
195 |
String name = vt.getName().toString(); |
|
196 |
String typeName = EvalPretty.prettyExpr((JCTree) vt.getType(), false); |
|
197 |
Tree baseType = vt.getType(); |
|
198 |
TreeDependencyScanner tds = new TreeDependencyScanner(); |
|
199 |
tds.scan(baseType); // Not dependent on initializer |
|
200 |
StringBuilder sbBrackets = new StringBuilder(); |
|
201 |
while (baseType instanceof ArrayTypeTree) { |
|
202 |
//TODO handle annotations too |
|
203 |
baseType = ((ArrayTypeTree) baseType).getType(); |
|
204 |
sbBrackets.append("[]"); |
|
205 |
} |
|
206 |
Range rtype = dis.treeToRange(baseType); |
|
207 |
Range runit = dis.treeToRange(vt); |
|
208 |
runit = new Range(runit.begin, runit.end - 1); |
|
209 |
ExpressionTree it = vt.getInitializer(); |
|
210 |
Range rinit = null; |
|
211 |
int nameMax = runit.end - 1; |
|
212 |
SubKind subkind; |
|
213 |
if (it != null) { |
|
214 |
subkind = SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND; |
|
215 |
rinit = dis.treeToRange(it); |
|
216 |
nameMax = rinit.begin - 1; |
|
217 |
} else { |
|
218 |
subkind = SubKind.VAR_DECLARATION_SUBKIND; |
|
219 |
} |
|
220 |
int nameStart = compileSource.lastIndexOf(name, nameMax); |
|
221 |
if (nameStart < 0) { |
|
222 |
throw new AssertionError("Name '" + name + "' not found"); |
|
223 |
} |
|
224 |
int nameEnd = nameStart + name.length(); |
|
225 |
Range rname = new Range(nameStart, nameEnd); |
|
226 |
Wrap guts = Wrap.varWrap(compileSource, rtype, sbBrackets.toString(), rname, rinit); |
|
227 |
Snippet snip = new VarSnippet(state.keyMap.keyForVariable(name), userSource, guts, |
|
228 |
name, subkind, typeName, |
|
229 |
tds.declareReferences()); |
|
230 |
DiagList modDiag = modifierDiagnostics(vt.getModifiers(), dis, true); |
|
231 |
List<SnippetEvent> res1 = declare(snip, modDiag); |
|
232 |
allEvents.addAll(res1); |
|
233 |
} |
|
234 |
||
235 |
return allEvents; |
|
236 |
} |
|
237 |
||
238 |
private List<SnippetEvent> processExpression(String userSource, String compileSource) { |
|
239 |
String name = null; |
|
240 |
ExpressionInfo ei = typeOfExpression(compileSource); |
|
241 |
ExpressionTree assignVar; |
|
242 |
Wrap guts; |
|
243 |
Snippet snip; |
|
244 |
if (ei != null && ei.isNonVoid) { |
|
245 |
String typeName = ei.typeName; |
|
246 |
SubKind subkind; |
|
247 |
if (ei.tree instanceof IdentifierTree) { |
|
248 |
IdentifierTree id = (IdentifierTree) ei.tree; |
|
249 |
name = id.getName().toString(); |
|
250 |
subkind = SubKind.VAR_VALUE_SUBKIND; |
|
251 |
||
252 |
} else if (ei.tree instanceof AssignmentTree |
|
253 |
&& (assignVar = ((AssignmentTree) ei.tree).getVariable()) instanceof IdentifierTree) { |
|
254 |
name = assignVar.toString(); |
|
255 |
subkind = SubKind.ASSIGNMENT_SUBKIND; |
|
256 |
} else { |
|
257 |
subkind = SubKind.OTHER_EXPRESSION_SUBKIND; |
|
258 |
} |
|
259 |
if (shouldGenTempVar(subkind)) { |
|
260 |
if (state.tempVariableNameGenerator != null) { |
|
261 |
name = state.tempVariableNameGenerator.get(); |
|
262 |
} |
|
263 |
while (name == null || state.keyMap.doesVariableNameExist(name)) { |
|
264 |
name = "$" + ++varNumber; |
|
265 |
} |
|
266 |
guts = Wrap.tempVarWrap(compileSource, typeName, name); |
|
267 |
Collection<String> declareReferences = null; //TODO |
|
268 |
snip = new VarSnippet(state.keyMap.keyForVariable(name), userSource, guts, |
|
269 |
name, SubKind.TEMP_VAR_EXPRESSION_SUBKIND, typeName, declareReferences); |
|
270 |
} else { |
|
271 |
guts = Wrap.methodReturnWrap(compileSource); |
|
272 |
snip = new ExpressionSnippet(state.keyMap.keyForExpression(name, typeName), userSource, guts, |
|
273 |
name, subkind); |
|
274 |
} |
|
275 |
} else { |
|
276 |
guts = Wrap.methodWrap(compileSource); |
|
277 |
if (ei == null) { |
|
278 |
// We got no type info, check for not a statement by trying |
|
279 |
AnalyzeTask at = trialCompile(guts); |
|
280 |
if (at.getDiagnostics().hasNotStatement()) { |
|
281 |
guts = Wrap.methodReturnWrap(compileSource); |
|
282 |
at = trialCompile(guts); |
|
283 |
} |
|
284 |
if (at.hasErrors()) { |
|
285 |
return compileFailResult(at, userSource); |
|
286 |
} |
|
287 |
} |
|
288 |
snip = new StatementSnippet(state.keyMap.keyForStatement(), userSource, guts); |
|
289 |
} |
|
290 |
return declare(snip); |
|
291 |
} |
|
292 |
||
293 |
private List<SnippetEvent> processClass(String userSource, Tree unitTree, String compileSource, SubKind snippetKind, ParseTask pt) { |
|
294 |
TreeDependencyScanner tds = new TreeDependencyScanner(); |
|
295 |
tds.scan(unitTree); |
|
296 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
297 |
TreeDissector dis = TreeDissector.createByFirstClass(pt); |
33362 | 298 |
|
299 |
ClassTree klassTree = (ClassTree) unitTree; |
|
300 |
String name = klassTree.getSimpleName().toString(); |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
301 |
DiagList modDiag = modifierDiagnostics(klassTree.getModifiers(), dis, false); |
36499 | 302 |
TypeDeclKey key = state.keyMap.keyForClass(name); |
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
303 |
// Corralling mutates. Must be last use of pt, unitTree, klassTree |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
304 |
Wrap corralled = new Corraller(key.index(), pt.getContext()).corralType(klassTree); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
305 |
|
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
306 |
Wrap guts = Wrap.classMemberWrap(compileSource); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
307 |
Snippet snip = new TypeDeclSnippet(key, userSource, guts, |
33362 | 308 |
name, snippetKind, |
309 |
corralled, tds.declareReferences(), tds.bodyReferences()); |
|
310 |
return declare(snip, modDiag); |
|
311 |
} |
|
312 |
||
313 |
private List<SnippetEvent> processStatement(String userSource, String compileSource) { |
|
314 |
Wrap guts = Wrap.methodWrap(compileSource); |
|
315 |
// Check for unreachable by trying |
|
316 |
AnalyzeTask at = trialCompile(guts); |
|
317 |
if (at.hasErrors()) { |
|
318 |
if (at.getDiagnostics().hasUnreachableError()) { |
|
319 |
guts = Wrap.methodUnreachableSemiWrap(compileSource); |
|
320 |
at = trialCompile(guts); |
|
321 |
if (at.hasErrors()) { |
|
322 |
if (at.getDiagnostics().hasUnreachableError()) { |
|
323 |
// Without ending semicolon |
|
324 |
guts = Wrap.methodUnreachableWrap(compileSource); |
|
325 |
at = trialCompile(guts); |
|
326 |
} |
|
327 |
if (at.hasErrors()) { |
|
328 |
return compileFailResult(at, userSource); |
|
329 |
} |
|
330 |
} |
|
331 |
} else { |
|
332 |
return compileFailResult(at, userSource); |
|
333 |
} |
|
334 |
} |
|
335 |
Snippet snip = new StatementSnippet(state.keyMap.keyForStatement(), userSource, guts); |
|
336 |
return declare(snip); |
|
337 |
} |
|
338 |
||
339 |
private AnalyzeTask trialCompile(Wrap guts) { |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
340 |
OuterWrap outer = state.outerMap.wrapInTrialClass(guts); |
33362 | 341 |
return state.taskFactory.new AnalyzeTask(outer); |
342 |
} |
|
343 |
||
344 |
private List<SnippetEvent> processMethod(String userSource, Tree unitTree, String compileSource, ParseTask pt) { |
|
345 |
TreeDependencyScanner tds = new TreeDependencyScanner(); |
|
346 |
tds.scan(unitTree); |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
347 |
TreeDissector dis = TreeDissector.createByFirstClass(pt); |
33362 | 348 |
|
349 |
MethodTree mt = (MethodTree) unitTree; |
|
350 |
String name = mt.getName().toString(); |
|
351 |
String parameterTypes |
|
352 |
= mt.getParameters() |
|
353 |
.stream() |
|
354 |
.map(param -> dis.treeToRange(param.getType()).part(compileSource)) |
|
355 |
.collect(Collectors.joining(",")); |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
356 |
Tree returnType = mt.getReturnType(); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
357 |
DiagList modDiag = modifierDiagnostics(mt.getModifiers(), dis, true); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
358 |
MethodKey key = state.keyMap.keyForMethod(name, parameterTypes); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
359 |
// Corralling mutates. Must be last use of pt, unitTree, mt |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
360 |
Wrap corralled = new Corraller(key.index(), pt.getContext()).corralMethod(mt); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
361 |
|
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
362 |
if (modDiag.hasErrors()) { |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
363 |
return compileFailResult(modDiag, userSource); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
364 |
} |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
365 |
Wrap guts = Wrap.classMemberWrap(compileSource); |
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
366 |
Range typeRange = dis.treeToRange(returnType); |
33362 | 367 |
String signature = "(" + parameterTypes + ")" + typeRange.part(compileSource); |
368 |
||
369 |
Snippet snip = new MethodSnippet(key, userSource, guts, |
|
36780
6bf2bef08a91
8152925: JShell: enable corralling of any type declaration, including enum
rfield
parents:
36499
diff
changeset
|
370 |
name, signature, |
33362 | 371 |
corralled, tds.declareReferences(), tds.bodyReferences()); |
372 |
return declare(snip, modDiag); |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* The snippet has failed, return with the rejected event |
|
377 |
* |
|
378 |
* @param xt the task from which to extract the failure diagnostics |
|
379 |
* @param userSource the incoming bad user source |
|
380 |
* @return a rejected snippet event |
|
381 |
*/ |
|
382 |
private List<SnippetEvent> compileFailResult(BaseTask xt, String userSource) { |
|
383 |
return compileFailResult(xt.getDiagnostics(), userSource); |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* The snippet has failed, return with the rejected event |
|
388 |
* |
|
389 |
* @param diags the failure diagnostics |
|
390 |
* @param userSource the incoming bad user source |
|
391 |
* @return a rejected snippet event |
|
392 |
*/ |
|
393 |
private List<SnippetEvent> compileFailResult(DiagList diags, String userSource) { |
|
394 |
ErroneousKey key = state.keyMap.keyForErroneous(); |
|
395 |
Snippet snip = new ErroneousSnippet(key, userSource, null, SubKind.UNKNOWN_SUBKIND); |
|
396 |
snip.setFailed(diags); |
|
397 |
state.maps.installSnippet(snip); |
|
398 |
return Collections.singletonList(new SnippetEvent( |
|
399 |
snip, Status.NONEXISTENT, Status.REJECTED, |
|
400 |
false, null, null, null) |
|
401 |
); |
|
402 |
} |
|
403 |
||
404 |
private ExpressionInfo typeOfExpression(String expression) { |
|
405 |
Wrap guts = Wrap.methodReturnWrap(expression); |
|
406 |
TaskFactory.AnalyzeTask at = trialCompile(guts); |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
407 |
if (!at.hasErrors() && at.firstCuTree() != null) { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
408 |
return TreeDissector.createByFirstClass(at) |
36160
f42d362d0d17
8131027: JShell API/tool: suggest imports for a class
jlahoda
parents:
34857
diff
changeset
|
409 |
.typeOfReturnStatement(at, state); |
33362 | 410 |
} |
411 |
return null; |
|
412 |
} |
|
413 |
||
414 |
/** |
|
415 |
* Should a temp var wrap the expression. TODO make this user configurable. |
|
416 |
* |
|
417 |
* @param snippetKind |
|
418 |
* @return |
|
419 |
*/ |
|
420 |
private boolean shouldGenTempVar(SubKind snippetKind) { |
|
421 |
return snippetKind == SubKind.OTHER_EXPRESSION_SUBKIND; |
|
422 |
} |
|
423 |
||
424 |
List<SnippetEvent> drop(Snippet si) { |
|
425 |
Unit c = new Unit(state, si); |
|
426 |
||
427 |
Set<Unit> ins = c.dependents().collect(toSet()); |
|
428 |
Set<Unit> outs = compileAndLoad(ins); |
|
429 |
||
430 |
return events(c, outs, null, null); |
|
431 |
} |
|
432 |
||
433 |
private List<SnippetEvent> declare(Snippet si) { |
|
434 |
return declare(si, new DiagList()); |
|
435 |
} |
|
436 |
||
437 |
private List<SnippetEvent> declare(Snippet si, DiagList generatedDiagnostics) { |
|
438 |
Unit c = new Unit(state, si, null, generatedDiagnostics); |
|
439 |
Set<Unit> ins = new LinkedHashSet<>(); |
|
440 |
ins.add(c); |
|
441 |
Set<Unit> outs = compileAndLoad(ins); |
|
442 |
||
443 |
if (!si.status().isDefined |
|
444 |
&& si.diagnostics().isEmpty() |
|
445 |
&& si.unresolved().isEmpty()) { |
|
446 |
// did not succeed, but no record of it, extract from others |
|
447 |
si.setDiagnostics(outs.stream() |
|
448 |
.flatMap(u -> u.snippet().diagnostics().stream()) |
|
449 |
.collect(Collectors.toCollection(DiagList::new))); |
|
450 |
} |
|
451 |
||
452 |
// If appropriate, execute the snippet |
|
453 |
String value = null; |
|
37751
77e7bb904a13
8139837: JShell API: make a common JShellException
rfield
parents:
37644
diff
changeset
|
454 |
JShellException exception = null; |
37389 | 455 |
if (si.status().isDefined) { |
456 |
if (si.isExecutable()) { |
|
457 |
try { |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
458 |
value = state.executionControl().invoke(si.classFullName(), DOIT_METHOD_NAME); |
37389 | 459 |
value = si.subKind().hasValue() |
460 |
? expunge(value) |
|
461 |
: ""; |
|
462 |
} catch (EvalException ex) { |
|
463 |
exception = translateExecutionException(ex); |
|
37751
77e7bb904a13
8139837: JShell API: make a common JShellException
rfield
parents:
37644
diff
changeset
|
464 |
} catch (JShellException ex) { |
77e7bb904a13
8139837: JShell API: make a common JShellException
rfield
parents:
37644
diff
changeset
|
465 |
// UnresolvedReferenceException |
37389 | 466 |
exception = ex; |
467 |
} |
|
468 |
} else if (si.subKind() == SubKind.VAR_DECLARATION_SUBKIND) { |
|
469 |
switch (((VarSnippet) si).typeName()) { |
|
470 |
case "byte": |
|
471 |
case "short": |
|
472 |
case "int": |
|
473 |
case "long": |
|
474 |
value = "0"; |
|
475 |
break; |
|
476 |
case "float": |
|
477 |
case "double": |
|
478 |
value = "0.0"; |
|
479 |
break; |
|
480 |
case "boolean": |
|
481 |
value = "false"; |
|
482 |
break; |
|
483 |
case "char": |
|
484 |
value = "''"; |
|
485 |
break; |
|
486 |
default: |
|
487 |
value = "null"; |
|
488 |
break; |
|
489 |
} |
|
33362 | 490 |
} |
491 |
} |
|
492 |
return events(c, outs, value, exception); |
|
493 |
} |
|
494 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
495 |
private boolean interestingEvent(SnippetEvent e) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
496 |
return e.isSignatureChange() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
497 |
|| e.causeSnippet() == null |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
498 |
|| e.status() != e.previousStatus() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
499 |
|| e.exception() != null; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
500 |
} |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
501 |
|
37751
77e7bb904a13
8139837: JShell API: make a common JShellException
rfield
parents:
37644
diff
changeset
|
502 |
private List<SnippetEvent> events(Unit c, Collection<Unit> outs, String value, JShellException exception) { |
33362 | 503 |
List<SnippetEvent> events = new ArrayList<>(); |
504 |
events.add(c.event(value, exception)); |
|
505 |
events.addAll(outs.stream() |
|
506 |
.filter(u -> u != c) |
|
507 |
.map(u -> u.event(null, null)) |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
508 |
.filter(this::interestingEvent) |
33362 | 509 |
.collect(Collectors.toList())); |
510 |
events.addAll(outs.stream() |
|
511 |
.flatMap(u -> u.secondaryEvents().stream()) |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
512 |
.filter(this::interestingEvent) |
33362 | 513 |
.collect(Collectors.toList())); |
514 |
//System.err.printf("Events: %s\n", events); |
|
515 |
return events; |
|
516 |
} |
|
517 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
518 |
private Set<OuterWrap> outerWrapSet(Collection<Unit> units) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
519 |
return units.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
520 |
.map(u -> u.snippet().outerWrap()) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
521 |
.collect(toSet()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
522 |
} |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
523 |
|
33362 | 524 |
private Set<Unit> compileAndLoad(Set<Unit> ins) { |
525 |
if (ins.isEmpty()) { |
|
526 |
return ins; |
|
527 |
} |
|
528 |
Set<Unit> replaced = new LinkedHashSet<>(); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
529 |
// Loop until dependencies and errors are stable |
33362 | 530 |
while (true) { |
531 |
state.debug(DBG_GEN, "compileAndLoad %s\n", ins); |
|
532 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
533 |
ins.stream().forEach(u -> u.initialize()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
534 |
ins.stream().forEach(u -> u.setWrap(ins, ins)); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
535 |
AnalyzeTask at = state.taskFactory.new AnalyzeTask(outerWrapSet(ins)); |
33362 | 536 |
ins.stream().forEach(u -> u.setDiagnostics(at)); |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
537 |
|
33362 | 538 |
// corral any Snippets that need it |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
539 |
AnalyzeTask cat; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
540 |
if (ins.stream().anyMatch(u -> u.corralIfNeeded(ins))) { |
33362 | 541 |
// if any were corralled, re-analyze everything |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
542 |
cat = state.taskFactory.new AnalyzeTask(outerWrapSet(ins)); |
33362 | 543 |
ins.stream().forEach(u -> u.setCorralledDiagnostics(cat)); |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
544 |
} else { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
545 |
cat = at; |
33362 | 546 |
} |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
547 |
ins.stream().forEach(u -> u.setStatus(cat)); |
33362 | 548 |
// compile and load the legit snippets |
549 |
boolean success; |
|
550 |
while (true) { |
|
551 |
List<Unit> legit = ins.stream() |
|
552 |
.filter(u -> u.isDefined()) |
|
553 |
.collect(toList()); |
|
554 |
state.debug(DBG_GEN, "compileAndLoad ins = %s -- legit = %s\n", |
|
555 |
ins, legit); |
|
556 |
if (legit.isEmpty()) { |
|
557 |
// no class files can be generated |
|
558 |
success = true; |
|
559 |
} else { |
|
560 |
// re-wrap with legit imports |
|
561 |
legit.stream().forEach(u -> u.setWrap(ins, legit)); |
|
562 |
||
563 |
// generate class files for those capable |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
564 |
CompileTask ct = state.taskFactory.new CompileTask(outerWrapSet(legit)); |
33362 | 565 |
if (!ct.compile()) { |
566 |
// oy! compile failed because of recursive new unresolved |
|
567 |
if (legit.stream() |
|
568 |
.filter(u -> u.smashingErrorDiagnostics(ct)) |
|
569 |
.count() > 0) { |
|
570 |
// try again, with the erroreous removed |
|
571 |
continue; |
|
572 |
} else { |
|
573 |
state.debug(DBG_GEN, "Should never happen error-less failure - %s\n", |
|
574 |
legit); |
|
575 |
} |
|
576 |
} |
|
577 |
||
578 |
// load all new classes |
|
579 |
load(legit.stream() |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
580 |
.flatMap(u -> u.classesToLoad(ct.classList(u.snippet().outerWrap()))) |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
581 |
.collect(toSet())); |
33362 | 582 |
// attempt to redefine the remaining classes |
583 |
List<Unit> toReplace = legit.stream() |
|
584 |
.filter(u -> !u.doRedefines()) |
|
585 |
.collect(toList()); |
|
586 |
||
587 |
// prevent alternating redefine/replace cyclic dependency |
|
588 |
// loop by replacing all that have been replaced |
|
589 |
if (!toReplace.isEmpty()) { |
|
590 |
replaced.addAll(toReplace); |
|
591 |
replaced.stream().forEach(u -> u.markForReplacement()); |
|
592 |
} |
|
593 |
||
594 |
success = toReplace.isEmpty(); |
|
595 |
} |
|
596 |
break; |
|
597 |
} |
|
598 |
||
599 |
// add any new dependencies to the working set |
|
600 |
List<Unit> newDependencies = ins.stream() |
|
601 |
.flatMap(u -> u.effectedDependents()) |
|
602 |
.collect(toList()); |
|
603 |
state.debug(DBG_GEN, "compileAndLoad %s -- deps: %s success: %s\n", |
|
604 |
ins, newDependencies, success); |
|
605 |
if (!ins.addAll(newDependencies) && success) { |
|
606 |
// all classes that could not be directly loaded (because they |
|
607 |
// are new) have been redefined, and no new dependnencies were |
|
608 |
// identified |
|
609 |
ins.stream().forEach(u -> u.finish()); |
|
610 |
return ins; |
|
611 |
} |
|
612 |
} |
|
613 |
} |
|
614 |
||
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
615 |
/** |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
616 |
* If there are classes to load, loads by calling the execution engine. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
617 |
* @param classnames names of the classes to load. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
618 |
*/ |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
619 |
private void load(Collection<String> classnames) { |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
620 |
if (!classnames.isEmpty()) { |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
621 |
state.executionControl().load(classnames); |
33362 | 622 |
} |
623 |
} |
|
624 |
||
625 |
private EvalException translateExecutionException(EvalException ex) { |
|
626 |
StackTraceElement[] raw = ex.getStackTrace(); |
|
627 |
int last = raw.length; |
|
628 |
do { |
|
629 |
if (last == 0) { |
|
630 |
last = raw.length - 1; |
|
631 |
break; |
|
632 |
} |
|
633 |
} while (!isWrap(raw[--last])); |
|
634 |
StackTraceElement[] elems = new StackTraceElement[last + 1]; |
|
635 |
for (int i = 0; i <= last; ++i) { |
|
636 |
StackTraceElement r = raw[i]; |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
637 |
OuterSnippetsClassWrap outer = state.outerMap.getOuter(r.getClassName()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
638 |
if (outer != null) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
639 |
String klass = expunge(r.getClassName()); |
33362 | 640 |
String method = r.getMethodName().equals(DOIT_METHOD_NAME) ? "" : r.getMethodName(); |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
641 |
int wln = r.getLineNumber() - 1; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
642 |
int line = outer.wrapLineToSnippetLine(wln) + 1; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
643 |
Snippet sn = outer.wrapLineToSnippet(wln); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
644 |
String file = "#" + sn.id(); |
33362 | 645 |
elems[i] = new StackTraceElement(klass, method, file, line); |
646 |
} else if (r.getFileName().equals("<none>")) { |
|
647 |
elems[i] = new StackTraceElement(r.getClassName(), r.getMethodName(), null, r.getLineNumber()); |
|
648 |
} else { |
|
649 |
elems[i] = r; |
|
650 |
} |
|
651 |
} |
|
652 |
String msg = ex.getMessage(); |
|
653 |
if (msg.equals("<none>")) { |
|
654 |
msg = null; |
|
655 |
} |
|
656 |
return new EvalException(msg, ex.getExceptionClassName(), elems); |
|
657 |
} |
|
658 |
||
659 |
private boolean isWrap(StackTraceElement ste) { |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
660 |
return PREFIX_PATTERN.matcher(ste.getClassName()).find(); |
33362 | 661 |
} |
662 |
||
663 |
private DiagList modifierDiagnostics(ModifiersTree modtree, |
|
664 |
final TreeDissector dis, boolean isAbstractProhibited) { |
|
665 |
||
666 |
class ModifierDiagnostic extends Diag { |
|
667 |
||
668 |
final boolean fatal; |
|
669 |
final String message; |
|
670 |
||
671 |
ModifierDiagnostic(List<Modifier> list, boolean fatal) { |
|
672 |
this.fatal = fatal; |
|
673 |
StringBuilder sb = new StringBuilder(); |
|
674 |
for (Modifier mod : list) { |
|
675 |
sb.append("'"); |
|
676 |
sb.append(mod.toString()); |
|
677 |
sb.append("' "); |
|
678 |
} |
|
36990 | 679 |
String key = (list.size() > 1) |
680 |
? fatal |
|
681 |
? "jshell.diag.modifier.plural.fatal" |
|
682 |
: "jshell.diag.modifier.plural.ignore" |
|
683 |
: fatal |
|
684 |
? "jshell.diag.modifier.single.fatal" |
|
685 |
: "jshell.diag.modifier.single.ignore"; |
|
686 |
this.message = state.messageFormat(key, sb.toString()); |
|
33362 | 687 |
} |
688 |
||
689 |
@Override |
|
690 |
public boolean isError() { |
|
691 |
return fatal; |
|
692 |
} |
|
693 |
||
694 |
@Override |
|
695 |
public long getPosition() { |
|
696 |
return dis.getStartPosition(modtree); |
|
697 |
} |
|
698 |
||
699 |
@Override |
|
700 |
public long getStartPosition() { |
|
701 |
return dis.getStartPosition(modtree); |
|
702 |
} |
|
703 |
||
704 |
@Override |
|
705 |
public long getEndPosition() { |
|
706 |
return dis.getEndPosition(modtree); |
|
707 |
} |
|
708 |
||
709 |
@Override |
|
710 |
public String getCode() { |
|
711 |
return fatal |
|
712 |
? "jdk.eval.error.illegal.modifiers" |
|
713 |
: "jdk.eval.warn.illegal.modifiers"; |
|
714 |
} |
|
715 |
||
716 |
@Override |
|
717 |
public String getMessage(Locale locale) { |
|
718 |
return message; |
|
719 |
} |
|
720 |
} |
|
721 |
||
722 |
List<Modifier> list = new ArrayList<>(); |
|
723 |
boolean fatal = false; |
|
724 |
for (Modifier mod : modtree.getFlags()) { |
|
725 |
switch (mod) { |
|
726 |
case SYNCHRONIZED: |
|
727 |
case NATIVE: |
|
728 |
list.add(mod); |
|
729 |
fatal = true; |
|
730 |
break; |
|
731 |
case ABSTRACT: |
|
732 |
if (isAbstractProhibited) { |
|
733 |
list.add(mod); |
|
734 |
fatal = true; |
|
735 |
} |
|
736 |
break; |
|
737 |
case PUBLIC: |
|
738 |
case PROTECTED: |
|
739 |
case PRIVATE: |
|
740 |
case STATIC: |
|
741 |
case FINAL: |
|
742 |
list.add(mod); |
|
743 |
break; |
|
744 |
} |
|
745 |
} |
|
746 |
return list.isEmpty() |
|
747 |
? new DiagList() |
|
748 |
: new DiagList(new ModifierDiagnostic(list, fatal)); |
|
749 |
} |
|
750 |
||
751 |
} |