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