author | rfield |
Mon, 06 Feb 2017 09:00:02 -0800 | |
changeset 43586 | cc7a4eb79b29 |
parent 43134 | 006808ae5f6e |
child 46185 | f4c981fc7818 |
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; |
|
43134
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
42827
diff
changeset
|
52 |
import jdk.jshell.ExpressionToTypeInfo.ExpressionInfo; |
33362 | 53 |
import jdk.jshell.Key.ErroneousKey; |
54 |
import jdk.jshell.Key.MethodKey; |
|
36499 | 55 |
import jdk.jshell.Key.TypeDeclKey; |
39370
437ba9bd2582
8159111: JShell API: Add access to wrappers and dependencies
rfield
parents:
38908
diff
changeset
|
56 |
import jdk.jshell.Snippet.Kind; |
33362 | 57 |
import jdk.jshell.Snippet.SubKind; |
58 |
import jdk.jshell.TaskFactory.AnalyzeTask; |
|
59 |
import jdk.jshell.TaskFactory.BaseTask; |
|
60 |
import jdk.jshell.TaskFactory.CompileTask; |
|
61 |
import jdk.jshell.TaskFactory.ParseTask; |
|
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 |
} |
|
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
43134
diff
changeset
|
158 |
ParseTask pt = state.taskFactory.parse(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; |
43134
006808ae5f6e
8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents:
42827
diff
changeset
|
299 |
ExpressionInfo ei = ExpressionToTypeInfo.expressionInfo(compileSource, state); |
33362 | 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 |
/** |
|
503 |
* Should a temp var wrap the expression. TODO make this user configurable. |
|
504 |
* |
|
505 |
* @param snippetKind |
|
506 |
* @return |
|
507 |
*/ |
|
508 |
private boolean shouldGenTempVar(SubKind snippetKind) { |
|
509 |
return snippetKind == SubKind.OTHER_EXPRESSION_SUBKIND; |
|
510 |
} |
|
511 |
||
512 |
List<SnippetEvent> drop(Snippet si) { |
|
513 |
Unit c = new Unit(state, si); |
|
41514
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40769
diff
changeset
|
514 |
Set<Unit> outs; |
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40769
diff
changeset
|
515 |
if (si instanceof PersistentSnippet) { |
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40769
diff
changeset
|
516 |
Set<Unit> ins = c.dependents().collect(toSet()); |
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40769
diff
changeset
|
517 |
outs = compileAndLoad(ins); |
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40769
diff
changeset
|
518 |
} else { |
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40769
diff
changeset
|
519 |
outs = Collections.emptySet(); |
a75c2b869d8d
8167128: JShell: /drop of statement gives confusing output
rfield
parents:
40769
diff
changeset
|
520 |
} |
33362 | 521 |
return events(c, outs, null, null); |
522 |
} |
|
523 |
||
524 |
private List<SnippetEvent> declare(Snippet si, DiagList generatedDiagnostics) { |
|
525 |
Unit c = new Unit(state, si, null, generatedDiagnostics); |
|
526 |
Set<Unit> ins = new LinkedHashSet<>(); |
|
527 |
ins.add(c); |
|
528 |
Set<Unit> outs = compileAndLoad(ins); |
|
529 |
||
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
530 |
if (!si.status().isDefined() |
33362 | 531 |
&& si.diagnostics().isEmpty() |
532 |
&& si.unresolved().isEmpty()) { |
|
533 |
// did not succeed, but no record of it, extract from others |
|
534 |
si.setDiagnostics(outs.stream() |
|
535 |
.flatMap(u -> u.snippet().diagnostics().stream()) |
|
536 |
.collect(Collectors.toCollection(DiagList::new))); |
|
537 |
} |
|
538 |
||
539 |
// If appropriate, execute the snippet |
|
540 |
String value = null; |
|
37751
77e7bb904a13
8139837: JShell API: make a common JShellException
rfield
parents:
37644
diff
changeset
|
541 |
JShellException exception = null; |
38908
f0c186d76c8a
8139829: JShell API: No use of fields to return information from public types
rfield
parents:
38535
diff
changeset
|
542 |
if (si.status().isDefined()) { |
37389 | 543 |
if (si.isExecutable()) { |
544 |
try { |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
545 |
value = state.executionControl().invoke(si.classFullName(), DOIT_METHOD_NAME); |
37389 | 546 |
value = si.subKind().hasValue() |
547 |
? expunge(value) |
|
548 |
: ""; |
|
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
549 |
} catch (ResolutionException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
550 |
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
|
551 |
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
|
552 |
} catch (UserException ex) { |
40769
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
553 |
exception = new EvalException(translateExceptionMessage(ex), |
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
554 |
ex.causeExceptionClass(), |
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
555 |
translateExceptionStack(ex)); |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
556 |
} catch (RunException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
557 |
// StopException - no-op |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
558 |
} catch (InternalException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
559 |
state.debug(ex, "invoke"); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
560 |
} catch (EngineTerminationException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
561 |
state.closeDown(); |
37389 | 562 |
} |
563 |
} else if (si.subKind() == SubKind.VAR_DECLARATION_SUBKIND) { |
|
564 |
switch (((VarSnippet) si).typeName()) { |
|
565 |
case "byte": |
|
566 |
case "short": |
|
567 |
case "int": |
|
568 |
case "long": |
|
569 |
value = "0"; |
|
570 |
break; |
|
571 |
case "float": |
|
572 |
case "double": |
|
573 |
value = "0.0"; |
|
574 |
break; |
|
575 |
case "boolean": |
|
576 |
value = "false"; |
|
577 |
break; |
|
578 |
case "char": |
|
579 |
value = "''"; |
|
580 |
break; |
|
581 |
default: |
|
582 |
value = "null"; |
|
583 |
break; |
|
584 |
} |
|
33362 | 585 |
} |
586 |
} |
|
587 |
return events(c, outs, value, exception); |
|
588 |
} |
|
589 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
590 |
private boolean interestingEvent(SnippetEvent e) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
591 |
return e.isSignatureChange() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
592 |
|| e.causeSnippet() == null |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
593 |
|| e.status() != e.previousStatus() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
594 |
|| e.exception() != null; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
595 |
} |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
596 |
|
37751
77e7bb904a13
8139837: JShell API: make a common JShellException
rfield
parents:
37644
diff
changeset
|
597 |
private List<SnippetEvent> events(Unit c, Collection<Unit> outs, String value, JShellException exception) { |
33362 | 598 |
List<SnippetEvent> events = new ArrayList<>(); |
599 |
events.add(c.event(value, exception)); |
|
600 |
events.addAll(outs.stream() |
|
601 |
.filter(u -> u != c) |
|
602 |
.map(u -> u.event(null, null)) |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
603 |
.filter(this::interestingEvent) |
33362 | 604 |
.collect(Collectors.toList())); |
605 |
events.addAll(outs.stream() |
|
606 |
.flatMap(u -> u.secondaryEvents().stream()) |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
607 |
.filter(this::interestingEvent) |
33362 | 608 |
.collect(Collectors.toList())); |
609 |
//System.err.printf("Events: %s\n", events); |
|
610 |
return events; |
|
611 |
} |
|
612 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
613 |
private Set<OuterWrap> outerWrapSet(Collection<Unit> units) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
614 |
return units.stream() |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
615 |
.map(u -> u.snippet().outerWrap()) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
616 |
.collect(toSet()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
617 |
} |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
618 |
|
33362 | 619 |
private Set<Unit> compileAndLoad(Set<Unit> ins) { |
620 |
if (ins.isEmpty()) { |
|
621 |
return ins; |
|
622 |
} |
|
623 |
Set<Unit> replaced = new LinkedHashSet<>(); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
624 |
// Loop until dependencies and errors are stable |
33362 | 625 |
while (true) { |
626 |
state.debug(DBG_GEN, "compileAndLoad %s\n", ins); |
|
627 |
||
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42272
diff
changeset
|
628 |
ins.stream().forEach(Unit::initialize); |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
629 |
ins.stream().forEach(u -> u.setWrap(ins, ins)); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
630 |
AnalyzeTask at = state.taskFactory.new AnalyzeTask(outerWrapSet(ins)); |
33362 | 631 |
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
|
632 |
|
33362 | 633 |
// 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
|
634 |
AnalyzeTask cat; |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
635 |
if (ins.stream().anyMatch(u -> u.corralIfNeeded(ins))) { |
33362 | 636 |
// if any were corralled, re-analyze everything |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
637 |
cat = state.taskFactory.new AnalyzeTask(outerWrapSet(ins)); |
33362 | 638 |
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
|
639 |
} else { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
640 |
cat = at; |
33362 | 641 |
} |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34752
diff
changeset
|
642 |
ins.stream().forEach(u -> u.setStatus(cat)); |
33362 | 643 |
// compile and load the legit snippets |
644 |
boolean success; |
|
645 |
while (true) { |
|
646 |
List<Unit> legit = ins.stream() |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42272
diff
changeset
|
647 |
.filter(Unit::isDefined) |
33362 | 648 |
.collect(toList()); |
649 |
state.debug(DBG_GEN, "compileAndLoad ins = %s -- legit = %s\n", |
|
650 |
ins, legit); |
|
651 |
if (legit.isEmpty()) { |
|
652 |
// no class files can be generated |
|
653 |
success = true; |
|
654 |
} else { |
|
655 |
// re-wrap with legit imports |
|
656 |
legit.stream().forEach(u -> u.setWrap(ins, legit)); |
|
657 |
||
658 |
// generate class files for those capable |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
659 |
CompileTask ct = state.taskFactory.new CompileTask(outerWrapSet(legit)); |
33362 | 660 |
if (!ct.compile()) { |
661 |
// oy! compile failed because of recursive new unresolved |
|
662 |
if (legit.stream() |
|
663 |
.filter(u -> u.smashingErrorDiagnostics(ct)) |
|
664 |
.count() > 0) { |
|
665 |
// try again, with the erroreous removed |
|
666 |
continue; |
|
667 |
} else { |
|
668 |
state.debug(DBG_GEN, "Should never happen error-less failure - %s\n", |
|
669 |
legit); |
|
670 |
} |
|
671 |
} |
|
672 |
||
673 |
// load all new classes |
|
674 |
load(legit.stream() |
|
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
675 |
.flatMap(u -> u.classesToLoad(ct.classList(u.snippet().outerWrap()))) |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
676 |
.collect(toSet())); |
33362 | 677 |
// attempt to redefine the remaining classes |
678 |
List<Unit> toReplace = legit.stream() |
|
679 |
.filter(u -> !u.doRedefines()) |
|
680 |
.collect(toList()); |
|
681 |
||
682 |
// prevent alternating redefine/replace cyclic dependency |
|
683 |
// loop by replacing all that have been replaced |
|
684 |
if (!toReplace.isEmpty()) { |
|
685 |
replaced.addAll(toReplace); |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42272
diff
changeset
|
686 |
replaced.stream().forEach(Unit::markForReplacement); |
33362 | 687 |
} |
688 |
||
689 |
success = toReplace.isEmpty(); |
|
690 |
} |
|
691 |
break; |
|
692 |
} |
|
693 |
||
694 |
// add any new dependencies to the working set |
|
695 |
List<Unit> newDependencies = ins.stream() |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42272
diff
changeset
|
696 |
.flatMap(Unit::effectedDependents) |
33362 | 697 |
.collect(toList()); |
698 |
state.debug(DBG_GEN, "compileAndLoad %s -- deps: %s success: %s\n", |
|
699 |
ins, newDependencies, success); |
|
700 |
if (!ins.addAll(newDependencies) && success) { |
|
701 |
// all classes that could not be directly loaded (because they |
|
702 |
// are new) have been redefined, and no new dependnencies were |
|
703 |
// identified |
|
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42272
diff
changeset
|
704 |
ins.stream().forEach(Unit::finish); |
33362 | 705 |
return ins; |
706 |
} |
|
707 |
} |
|
708 |
} |
|
709 |
||
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
710 |
/** |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
711 |
* If there are classes to load, loads by calling the execution engine. |
42272
82e273c4f2b3
8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents:
41858
diff
changeset
|
712 |
* @param classbytecodes names of the classes to load. |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
37751
diff
changeset
|
713 |
*/ |
42272
82e273c4f2b3
8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents:
41858
diff
changeset
|
714 |
private void load(Collection<ClassBytecodes> classbytecodes) { |
82e273c4f2b3
8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents:
41858
diff
changeset
|
715 |
if (!classbytecodes.isEmpty()) { |
82e273c4f2b3
8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents:
41858
diff
changeset
|
716 |
ClassBytecodes[] cbcs = classbytecodes.toArray(new ClassBytecodes[classbytecodes.size()]); |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
717 |
try { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
718 |
state.executionControl().load(cbcs); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
719 |
state.classTracker.markLoaded(cbcs); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
720 |
} catch (ClassInstallException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
721 |
state.classTracker.markLoaded(cbcs, ex.installed()); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
722 |
} catch (NotImplementedException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
723 |
state.debug(ex, "Seriously?!? load not implemented"); |
42272
82e273c4f2b3
8169519: JShell: Handle start-up failures and hangs gracefully
rfield
parents:
41858
diff
changeset
|
724 |
state.closeDown(); |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
725 |
} catch (EngineTerminationException ex) { |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
726 |
state.closeDown(); |
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39370
diff
changeset
|
727 |
} |
33362 | 728 |
} |
729 |
} |
|
730 |
||
40769
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
731 |
private StackTraceElement[] translateExceptionStack(Exception ex) { |
33362 | 732 |
StackTraceElement[] raw = ex.getStackTrace(); |
733 |
int last = raw.length; |
|
734 |
do { |
|
735 |
if (last == 0) { |
|
736 |
last = raw.length - 1; |
|
737 |
break; |
|
738 |
} |
|
739 |
} while (!isWrap(raw[--last])); |
|
740 |
StackTraceElement[] elems = new StackTraceElement[last + 1]; |
|
741 |
for (int i = 0; i <= last; ++i) { |
|
742 |
StackTraceElement r = raw[i]; |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
743 |
OuterSnippetsClassWrap outer = state.outerMap.getOuter(r.getClassName()); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
744 |
if (outer != null) { |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
745 |
String klass = expunge(r.getClassName()); |
33362 | 746 |
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
|
747 |
int wln = r.getLineNumber() - 1; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
748 |
int line = outer.wrapLineToSnippetLine(wln) + 1; |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
749 |
Snippet sn = outer.wrapLineToSnippet(wln); |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
750 |
String file = "#" + sn.id(); |
33362 | 751 |
elems[i] = new StackTraceElement(klass, method, file, line); |
752 |
} else if (r.getFileName().equals("<none>")) { |
|
753 |
elems[i] = new StackTraceElement(r.getClassName(), r.getMethodName(), null, r.getLineNumber()); |
|
754 |
} else { |
|
755 |
elems[i] = r; |
|
756 |
} |
|
757 |
} |
|
40769
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
758 |
return elems; |
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
759 |
} |
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
760 |
|
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
761 |
private String translateExceptionMessage(Exception ex) { |
33362 | 762 |
String msg = ex.getMessage(); |
40769
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
763 |
return msg.equals("<none>") |
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
764 |
? null |
e57f1a5c9346
8133507: JShell: StackTraceElement#getFileName of EvalException does not use custom id generator
rfield
parents:
39807
diff
changeset
|
765 |
: msg; |
33362 | 766 |
} |
767 |
||
768 |
private boolean isWrap(StackTraceElement ste) { |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
37389
diff
changeset
|
769 |
return PREFIX_PATTERN.matcher(ste.getClassName()).find(); |
33362 | 770 |
} |
771 |
||
772 |
private DiagList modifierDiagnostics(ModifiersTree modtree, |
|
773 |
final TreeDissector dis, boolean isAbstractProhibited) { |
|
774 |
||
775 |
class ModifierDiagnostic extends Diag { |
|
776 |
||
777 |
final boolean fatal; |
|
778 |
final String message; |
|
779 |
||
780 |
ModifierDiagnostic(List<Modifier> list, boolean fatal) { |
|
781 |
this.fatal = fatal; |
|
782 |
StringBuilder sb = new StringBuilder(); |
|
783 |
for (Modifier mod : list) { |
|
784 |
sb.append("'"); |
|
785 |
sb.append(mod.toString()); |
|
786 |
sb.append("' "); |
|
787 |
} |
|
36990 | 788 |
String key = (list.size() > 1) |
789 |
? fatal |
|
790 |
? "jshell.diag.modifier.plural.fatal" |
|
791 |
: "jshell.diag.modifier.plural.ignore" |
|
792 |
: fatal |
|
793 |
? "jshell.diag.modifier.single.fatal" |
|
794 |
: "jshell.diag.modifier.single.ignore"; |
|
795 |
this.message = state.messageFormat(key, sb.toString()); |
|
33362 | 796 |
} |
797 |
||
798 |
@Override |
|
799 |
public boolean isError() { |
|
800 |
return fatal; |
|
801 |
} |
|
802 |
||
803 |
@Override |
|
804 |
public long getPosition() { |
|
805 |
return dis.getStartPosition(modtree); |
|
806 |
} |
|
807 |
||
808 |
@Override |
|
809 |
public long getStartPosition() { |
|
810 |
return dis.getStartPosition(modtree); |
|
811 |
} |
|
812 |
||
813 |
@Override |
|
814 |
public long getEndPosition() { |
|
815 |
return dis.getEndPosition(modtree); |
|
816 |
} |
|
817 |
||
818 |
@Override |
|
819 |
public String getCode() { |
|
820 |
return fatal |
|
821 |
? "jdk.eval.error.illegal.modifiers" |
|
822 |
: "jdk.eval.warn.illegal.modifiers"; |
|
823 |
} |
|
824 |
||
825 |
@Override |
|
826 |
public String getMessage(Locale locale) { |
|
827 |
return message; |
|
828 |
} |
|
829 |
} |
|
830 |
||
831 |
List<Modifier> list = new ArrayList<>(); |
|
832 |
boolean fatal = false; |
|
833 |
for (Modifier mod : modtree.getFlags()) { |
|
834 |
switch (mod) { |
|
835 |
case SYNCHRONIZED: |
|
836 |
case NATIVE: |
|
837 |
list.add(mod); |
|
838 |
fatal = true; |
|
839 |
break; |
|
840 |
case ABSTRACT: |
|
841 |
if (isAbstractProhibited) { |
|
842 |
list.add(mod); |
|
843 |
fatal = true; |
|
844 |
} |
|
845 |
break; |
|
846 |
case PUBLIC: |
|
847 |
case PROTECTED: |
|
848 |
case PRIVATE: |
|
41858
5843b57ce3a6
8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents:
41514
diff
changeset
|
849 |
// quietly ignore, user cannot see effects one way or the other |
5843b57ce3a6
8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents:
41514
diff
changeset
|
850 |
break; |
33362 | 851 |
case STATIC: |
852 |
case FINAL: |
|
853 |
list.add(mod); |
|
854 |
break; |
|
855 |
} |
|
856 |
} |
|
857 |
return list.isEmpty() |
|
858 |
? new DiagList() |
|
859 |
: new DiagList(new ModifierDiagnostic(list, fatal)); |
|
860 |
} |
|
861 |
||
862 |
} |