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