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