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