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