author | rfield |
Fri, 16 Feb 2018 16:18:55 -0800 | |
changeset 49092 | 6dc5e0cdb44c |
parent 48610 | a587f95313f1 |
child 49518 | d0ff431a596e |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
2 |
* Copyright (c) 2014, 2018, 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 |
||
26 |
package jdk.jshell; |
|
27 |
||
28 |
import com.sun.source.tree.CompilationUnitTree; |
|
29 |
import com.sun.source.tree.Tree; |
|
30 |
import com.sun.source.util.Trees; |
|
31 |
import com.sun.tools.javac.api.JavacTaskImpl; |
|
32 |
import com.sun.tools.javac.util.Context; |
|
33 |
import java.util.ArrayList; |
|
34 |
import java.util.List; |
|
35 |
import javax.tools.Diagnostic; |
|
36 |
import javax.tools.DiagnosticCollector; |
|
37 |
import javax.tools.JavaCompiler; |
|
38 |
import javax.tools.JavaFileManager; |
|
39 |
import javax.tools.JavaFileObject; |
|
40 |
import javax.tools.ToolProvider; |
|
41 |
import static jdk.jshell.Util.*; |
|
42 |
import com.sun.source.tree.ImportTree; |
|
43 |
import com.sun.tools.javac.code.Types; |
|
44 |
import com.sun.tools.javac.util.JavacMessages; |
|
45 |
import jdk.jshell.MemoryFileManager.OutputMemoryJavaFileObject; |
|
46 |
import java.util.Collections; |
|
47 |
import java.util.Locale; |
|
48 |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
|
49 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_GEN; |
|
50 |
import java.io.File; |
|
51 |
import java.util.Collection; |
|
52 |
import java.util.HashMap; |
|
53 |
import java.util.LinkedHashMap; |
|
54 |
import java.util.Map; |
|
55 |
import java.util.stream.Collectors; |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
56 |
import static java.util.stream.Collectors.toList; |
33362 | 57 |
import java.util.stream.Stream; |
58 |
import javax.lang.model.util.Elements; |
|
59 |
import javax.tools.FileObject; |
|
60 |
import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject; |
|
38526
6f5838874afc
8144062: Move jdk.Version to java.lang.Runtime.Version
iris
parents:
37939
diff
changeset
|
61 |
import java.lang.Runtime.Version; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
62 |
import java.nio.CharBuffer; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
63 |
import java.util.function.BiFunction; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
64 |
import com.sun.source.tree.ClassTree; |
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
43134
diff
changeset
|
65 |
import com.sun.source.tree.Tree.Kind; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
66 |
import com.sun.source.util.TaskEvent; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
67 |
import com.sun.source.util.TaskListener; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
68 |
import com.sun.tools.javac.api.JavacTaskPool; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
69 |
import com.sun.tools.javac.code.ClassFinder; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
70 |
import com.sun.tools.javac.code.Kinds; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
71 |
import com.sun.tools.javac.code.Symbol; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
72 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
73 |
import com.sun.tools.javac.code.Symbol.PackageSymbol; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
74 |
import com.sun.tools.javac.code.Symbol.TypeSymbol; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
75 |
import com.sun.tools.javac.code.Symbol.VarSymbol; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
76 |
import com.sun.tools.javac.code.Symtab; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
77 |
import com.sun.tools.javac.code.Type; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
78 |
import com.sun.tools.javac.comp.Attr; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
79 |
import com.sun.tools.javac.comp.AttrContext; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
80 |
import com.sun.tools.javac.comp.Enter; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
81 |
import com.sun.tools.javac.comp.Env; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
82 |
import com.sun.tools.javac.comp.Resolve; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
83 |
import com.sun.tools.javac.parser.Parser; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
84 |
import com.sun.tools.javac.parser.ParserFactory; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
85 |
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
86 |
import com.sun.tools.javac.tree.JCTree.JCExpression; |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
87 |
import com.sun.tools.javac.tree.JCTree.JCTypeCast; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
88 |
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
89 |
import com.sun.tools.javac.tree.JCTree.Tag; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
90 |
import com.sun.tools.javac.util.Context.Factory; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
91 |
import com.sun.tools.javac.util.Log; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
92 |
import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
93 |
import com.sun.tools.javac.util.Names; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
94 |
import jdk.jshell.Snippet.Status; |
33362 | 95 |
|
96 |
/** |
|
97 |
* The primary interface to the compiler API. Parsing, analysis, and |
|
98 |
* compilation to class files (in memory). |
|
99 |
* @author Robert Field |
|
100 |
*/ |
|
101 |
class TaskFactory { |
|
102 |
||
103 |
private final JavaCompiler compiler; |
|
104 |
private final MemoryFileManager fileManager; |
|
105 |
private final JShell state; |
|
106 |
private String classpath = System.getProperty("java.class.path"); |
|
36497
9c4840131512
8150632: jdk.jshell.TaskFactory should use jdk.Version to check for java.specification.version
simonis
parents:
35000
diff
changeset
|
107 |
private final static Version INITIAL_SUPPORTED_VER = Version.parse("9"); |
33362 | 108 |
|
109 |
TaskFactory(JShell state) { |
|
110 |
this.state = state; |
|
111 |
this.compiler = ToolProvider.getSystemJavaCompiler(); |
|
112 |
if (compiler == null) { |
|
113 |
throw new UnsupportedOperationException("Compiler not available, must be run with full JDK 9."); |
|
114 |
} |
|
36497
9c4840131512
8150632: jdk.jshell.TaskFactory should use jdk.Version to check for java.specification.version
simonis
parents:
35000
diff
changeset
|
115 |
Version current = Version.parse(System.getProperty("java.specification.version")); |
39809
74b7aea686d8
8161236: Runtime.Version.{compareTo, equals}IgnoreOpt should be renamed
iris
parents:
39807
diff
changeset
|
116 |
if (INITIAL_SUPPORTED_VER.compareToIgnoreOptional(current) > 0) { |
33362 | 117 |
throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9."); |
118 |
} |
|
119 |
this.fileManager = new MemoryFileManager( |
|
120 |
compiler.getStandardFileManager(null, null, null), state); |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
121 |
initTaskPool(); |
33362 | 122 |
} |
123 |
||
124 |
void addToClasspath(String path) { |
|
125 |
classpath = classpath + File.pathSeparator + path; |
|
126 |
List<String> args = new ArrayList<>(); |
|
127 |
args.add(classpath); |
|
128 |
fileManager().handleOption("-classpath", args.iterator()); |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
129 |
initTaskPool(); |
33362 | 130 |
} |
131 |
||
132 |
MemoryFileManager fileManager() { |
|
133 |
return fileManager; |
|
134 |
} |
|
135 |
||
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
136 |
public <Z> Z parse(String source, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
137 |
boolean forceExpression, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
138 |
Worker<ParseTask, Z> worker) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
139 |
StringSourceHandler sh = new StringSourceHandler(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
140 |
return runTask(Stream.of(source), |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
141 |
sh, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
142 |
List.of("-XDallowStringFolding=false", "-proc:none", |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
143 |
"-XDneedsReplParserFactory=" + forceExpression), |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
144 |
(jti, diagnostics) -> new ParseTask(sh, jti, diagnostics, forceExpression), |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
145 |
worker); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
146 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
147 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
148 |
public <Z> Z analyze(OuterWrap wrap, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
149 |
Worker<AnalyzeTask, Z> worker) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
150 |
return analyze(Collections.singletonList(wrap), worker); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
151 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
152 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
153 |
public <Z> Z analyze(OuterWrap wrap, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
154 |
List<String> extraArgs, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
155 |
Worker<AnalyzeTask, Z> worker) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
156 |
return analyze(Collections.singletonList(wrap), extraArgs, worker); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
157 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
158 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
159 |
public <Z> Z analyze(Collection<OuterWrap> wraps, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
160 |
Worker<AnalyzeTask, Z> worker) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
161 |
return analyze(wraps, Collections.emptyList(), worker); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
162 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
163 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
164 |
public <Z> Z analyze(Collection<OuterWrap> wraps, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
165 |
List<String> extraArgs, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
166 |
Worker<AnalyzeTask, Z> worker) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
167 |
WrapSourceHandler sh = new WrapSourceHandler(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
168 |
List<String> allOptions = new ArrayList<>(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
169 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
170 |
allOptions.add("--should-stop:at=FLOW"); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
171 |
allOptions.add("-Xlint:unchecked"); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
172 |
allOptions.add("-proc:none"); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
173 |
allOptions.addAll(extraArgs); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
174 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
175 |
return runTask(wraps.stream(), |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
176 |
sh, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
177 |
allOptions, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
178 |
(jti, diagnostics) -> new AnalyzeTask(sh, jti, diagnostics), |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
179 |
worker); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
180 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
181 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
182 |
public <Z> Z compile(Collection<OuterWrap> wraps, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
183 |
Worker<CompileTask, Z> worker) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
184 |
WrapSourceHandler sh = new WrapSourceHandler(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
185 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
186 |
return runTask(wraps.stream(), |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
187 |
sh, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
188 |
List.of("-Xlint:unchecked", "-proc:none", "-parameters"), |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
189 |
(jti, diagnostics) -> new CompileTask(sh, jti, diagnostics), |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
190 |
worker); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
191 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
192 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
193 |
private <S, T extends BaseTask, Z> Z runTask(Stream<S> inputs, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
194 |
SourceHandler<S> sh, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
195 |
List<String> options, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
196 |
BiFunction<JavacTaskImpl, DiagnosticCollector<JavaFileObject>, T> creator, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
197 |
Worker<T, Z> worker) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
198 |
List<String> allOptions = new ArrayList<>(options.size() + state.extraCompilerOptions.size()); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
199 |
allOptions.addAll(options); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
200 |
allOptions.addAll(state.extraCompilerOptions); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
201 |
Iterable<? extends JavaFileObject> compilationUnits = inputs |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
202 |
.map(in -> sh.sourceToFileObject(fileManager, in)) |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
203 |
.collect(Collectors.toList()); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
204 |
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
205 |
return javacTaskPool.getTask(null, fileManager, diagnostics, allOptions, null, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
206 |
compilationUnits, task -> { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
207 |
JavacTaskImpl jti = (JavacTaskImpl) task; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
208 |
Context context = jti.getContext(); |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
209 |
DisableAccessibilityResolve.preRegister(context); |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
210 |
jti.addTaskListener(new TaskListenerImpl(context, state)); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
211 |
try { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
212 |
return worker.withTask(creator.apply(jti, diagnostics)); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
213 |
} finally { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
214 |
//additional cleanup: purge the REPL package: |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
215 |
Symtab syms = Symtab.instance(context); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
216 |
Names names = Names.instance(context); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
217 |
PackageSymbol repl = syms.getPackage(syms.unnamedModule, names.fromString(Util.REPL_PACKAGE)); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
218 |
if (repl != null) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
219 |
for (ClassSymbol clazz : syms.getAllClasses()) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
220 |
if (clazz.packge() == repl) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
221 |
syms.removeClass(syms.unnamedModule, clazz.flatName()); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
222 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
223 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
224 |
repl.members_field = null; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
225 |
repl.completer = ClassFinder.instance(context).getCompleter(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
226 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
227 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
228 |
}); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
229 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
230 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
231 |
interface Worker<T extends BaseTask, Z> { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
232 |
public Z withTask(T task); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
233 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
234 |
|
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
43134
diff
changeset
|
235 |
// Parse a snippet and return our parse task handler |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
236 |
<Z> Z parse(final String source, Worker<ParseTask, Z> worker) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
237 |
return parse(source, false, pt -> { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
238 |
if (!pt.units().isEmpty() |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
239 |
&& pt.units().get(0).getKind() == Kind.EXPRESSION_STATEMENT |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
240 |
&& pt.getDiagnostics().hasOtherThanNotStatementErrors()) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
241 |
// It failed, it may be an expression being incorrectly |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
242 |
// parsed as having a leading type variable, example: a < b |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
243 |
// Try forcing interpretation as an expression |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
244 |
return parse(source, true, ept -> { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
245 |
if (!ept.getDiagnostics().hasOtherThanNotStatementErrors()) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
246 |
return worker.withTask(ept); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
247 |
} else { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
248 |
return worker.withTask(pt); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
249 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
250 |
}); |
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
43134
diff
changeset
|
251 |
} |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
252 |
return worker.withTask(pt); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
253 |
}); |
43586
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
43134
diff
changeset
|
254 |
} |
cc7a4eb79b29
8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents:
43134
diff
changeset
|
255 |
|
33362 | 256 |
private interface SourceHandler<T> { |
257 |
||
258 |
JavaFileObject sourceToFileObject(MemoryFileManager fm, T t); |
|
259 |
||
260 |
Diag diag(Diagnostic<? extends JavaFileObject> d); |
|
261 |
} |
|
262 |
||
263 |
private class StringSourceHandler implements SourceHandler<String> { |
|
264 |
||
265 |
@Override |
|
266 |
public JavaFileObject sourceToFileObject(MemoryFileManager fm, String src) { |
|
267 |
return fm.createSourceFileObject(src, "$NeverUsedName$", src); |
|
268 |
} |
|
269 |
||
270 |
@Override |
|
271 |
public Diag diag(final Diagnostic<? extends JavaFileObject> d) { |
|
272 |
return new Diag() { |
|
273 |
||
274 |
@Override |
|
275 |
public boolean isError() { |
|
276 |
return d.getKind() == Diagnostic.Kind.ERROR; |
|
277 |
} |
|
278 |
||
279 |
@Override |
|
280 |
public long getPosition() { |
|
281 |
return d.getPosition(); |
|
282 |
} |
|
283 |
||
284 |
@Override |
|
285 |
public long getStartPosition() { |
|
286 |
return d.getStartPosition(); |
|
287 |
} |
|
288 |
||
289 |
@Override |
|
290 |
public long getEndPosition() { |
|
291 |
return d.getEndPosition(); |
|
292 |
} |
|
293 |
||
294 |
@Override |
|
295 |
public String getCode() { |
|
296 |
return d.getCode(); |
|
297 |
} |
|
298 |
||
299 |
@Override |
|
300 |
public String getMessage(Locale locale) { |
|
301 |
return expunge(d.getMessage(locale)); |
|
302 |
} |
|
303 |
}; |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
private class WrapSourceHandler implements SourceHandler<OuterWrap> { |
|
308 |
||
309 |
@Override |
|
310 |
public JavaFileObject sourceToFileObject(MemoryFileManager fm, OuterWrap w) { |
|
311 |
return fm.createSourceFileObject(w, w.classFullName(), w.wrapped()); |
|
312 |
} |
|
313 |
||
49092
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
314 |
/** |
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
315 |
* Get the source information from the wrap. If this is external, or |
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
316 |
* otherwise does not have wrap info, just use source code. |
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
317 |
* @param d the Diagnostic from the compiler |
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
318 |
* @return the corresponding Diag |
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
319 |
*/ |
33362 | 320 |
@Override |
321 |
public Diag diag(Diagnostic<? extends JavaFileObject> d) { |
|
49092
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
322 |
JavaFileObject jfo = d.getSource(); |
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
323 |
return jfo instanceof SourceMemoryJavaFileObject |
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
324 |
? ((OuterWrap) ((SourceMemoryJavaFileObject) jfo).getOrigin()).wrapDiag(d) |
6dc5e0cdb44c
8196133: JShell crashes when attempting to use bad source file in class path
rfield
parents:
48610
diff
changeset
|
325 |
: new StringSourceHandler().diag(d); |
33362 | 326 |
} |
327 |
} |
|
328 |
||
329 |
/** |
|
330 |
* Parse a snippet of code (as a String) using the parser subclass. Return |
|
331 |
* the parse tree (and errors). |
|
332 |
*/ |
|
333 |
class ParseTask extends BaseTask { |
|
334 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
335 |
private final Iterable<? extends CompilationUnitTree> cuts; |
33362 | 336 |
private final List<? extends Tree> units; |
337 |
||
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
338 |
private ParseTask(SourceHandler<String> sh, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
339 |
JavacTaskImpl task, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
340 |
DiagnosticCollector<JavaFileObject> diagnostics, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
341 |
boolean forceExpression) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
342 |
super(sh, task, diagnostics); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
343 |
ReplParserFactory.preRegister(context, forceExpression); |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
344 |
cuts = parse(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
345 |
units = Util.stream(cuts) |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
346 |
.flatMap(cut -> { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
347 |
List<? extends ImportTree> imps = cut.getImports(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
348 |
return (!imps.isEmpty() ? imps : cut.getTypeDecls()).stream(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
349 |
}) |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
350 |
.collect(toList()); |
33362 | 351 |
} |
352 |
||
353 |
private Iterable<? extends CompilationUnitTree> parse() { |
|
354 |
try { |
|
355 |
return task.parse(); |
|
356 |
} catch (Exception ex) { |
|
357 |
throw new InternalError("Exception during parse - " + ex.getMessage(), ex); |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
List<? extends Tree> units() { |
|
362 |
return units; |
|
363 |
} |
|
364 |
||
365 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
366 |
Iterable<? extends CompilationUnitTree> cuTrees() { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
367 |
return cuts; |
33362 | 368 |
} |
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Run the normal "analyze()" pass of the compiler over the wrapped snippet. |
|
373 |
*/ |
|
374 |
class AnalyzeTask extends BaseTask { |
|
375 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
376 |
private final Iterable<? extends CompilationUnitTree> cuts; |
33362 | 377 |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
378 |
private AnalyzeTask(SourceHandler<OuterWrap> sh, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
379 |
JavacTaskImpl task, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
380 |
DiagnosticCollector<JavaFileObject> diagnostics) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
381 |
super(sh, task, diagnostics); |
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
382 |
cuts = analyze(); |
33362 | 383 |
} |
384 |
||
385 |
private Iterable<? extends CompilationUnitTree> analyze() { |
|
386 |
try { |
|
387 |
Iterable<? extends CompilationUnitTree> cuts = task.parse(); |
|
388 |
task.analyze(); |
|
389 |
return cuts; |
|
390 |
} catch (Exception ex) { |
|
391 |
throw new InternalError("Exception during analyze - " + ex.getMessage(), ex); |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
396 |
Iterable<? extends CompilationUnitTree> cuTrees() { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
397 |
return cuts; |
33362 | 398 |
} |
399 |
||
400 |
Elements getElements() { |
|
401 |
return task.getElements(); |
|
402 |
} |
|
403 |
||
404 |
javax.lang.model.util.Types getTypes() { |
|
405 |
return task.getTypes(); |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
/** |
|
410 |
* Unit the wrapped snippet to class files. |
|
411 |
*/ |
|
412 |
class CompileTask extends BaseTask { |
|
413 |
||
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
414 |
private final Map<OuterWrap, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>(); |
33362 | 415 |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
416 |
CompileTask(SourceHandler<OuterWrap>sh, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
417 |
JavacTaskImpl jti, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
418 |
DiagnosticCollector<JavaFileObject> diagnostics) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
419 |
super(sh, jti, diagnostics); |
33362 | 420 |
} |
421 |
||
422 |
boolean compile() { |
|
423 |
fileManager.registerClassFileCreationListener(this::listenForNewClassFile); |
|
424 |
boolean result = task.call(); |
|
425 |
fileManager.registerClassFileCreationListener(null); |
|
426 |
return result; |
|
427 |
} |
|
428 |
||
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
429 |
// Returns the list of classes generated during this compile. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
430 |
// Stores the mapping between class name and current compiled bytes. |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
431 |
List<String> classList(OuterWrap w) { |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
432 |
List<OutputMemoryJavaFileObject> l = classObjs.get(w); |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
433 |
if (l == null) { |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
434 |
return Collections.emptyList(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
435 |
} |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
436 |
List<String> list = new ArrayList<>(); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
437 |
for (OutputMemoryJavaFileObject fo : l) { |
39807
ba0ff343d241
8160127: JShell API: extract abstract JDI and abstract streaming implementations of ExecutionControl
rfield
parents:
39601
diff
changeset
|
438 |
state.classTracker.setCurrentBytes(fo.getName(), fo.getBytes()); |
38535
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
439 |
list.add(fo.getName()); |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
440 |
} |
4a25025e0b0d
8156101: JShell SPI: Provide a pluggable execution control SPI
rfield
parents:
38526
diff
changeset
|
441 |
return list; |
33362 | 442 |
} |
443 |
||
444 |
private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location, |
|
445 |
String className, JavaFileObject.Kind kind, FileObject sibling) { |
|
446 |
//debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind); |
|
447 |
if (location == CLASS_OUTPUT) { |
|
448 |
state.debug(DBG_GEN, "Compiler generating class %s\n", className); |
|
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
449 |
OuterWrap w = ((sibling instanceof SourceMemoryJavaFileObject) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
450 |
&& (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof OuterWrap)) |
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
451 |
? (OuterWrap) ((SourceMemoryJavaFileObject) sibling).getOrigin() |
33362 | 452 |
: null; |
37644
33cf53901cac
8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents:
36526
diff
changeset
|
453 |
classObjs.compute(w, (k, v) -> (v == null)? new ArrayList<>() : v) |
33362 | 454 |
.add(jfo); |
455 |
} |
|
456 |
} |
|
457 |
||
458 |
@Override |
|
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
459 |
Iterable<? extends CompilationUnitTree> cuTrees() { |
33362 | 460 |
throw new UnsupportedOperationException("Not supported."); |
461 |
} |
|
462 |
} |
|
463 |
||
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
464 |
private JavacTaskPool javacTaskPool; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
465 |
|
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
466 |
private void initTaskPool() { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
467 |
javacTaskPool = new JavacTaskPool(5); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
468 |
} |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
469 |
|
33362 | 470 |
abstract class BaseTask { |
471 |
||
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
472 |
final DiagnosticCollector<JavaFileObject> diagnostics; |
33362 | 473 |
final JavacTaskImpl task; |
474 |
private DiagList diags = null; |
|
475 |
private final SourceHandler<?> sourceHandler; |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
476 |
final Context context; |
33362 | 477 |
private Types types; |
478 |
private JavacMessages messages; |
|
479 |
private Trees trees; |
|
480 |
||
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
481 |
private <T>BaseTask(SourceHandler<T> sh, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
482 |
JavacTaskImpl task, |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
483 |
DiagnosticCollector<JavaFileObject> diagnostics) { |
33362 | 484 |
this.sourceHandler = sh; |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
485 |
this.task = task; |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
486 |
context = task.getContext(); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
487 |
this.diagnostics = diagnostics; |
33362 | 488 |
} |
489 |
||
34857
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
490 |
abstract Iterable<? extends CompilationUnitTree> cuTrees(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
491 |
|
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
492 |
CompilationUnitTree firstCuTree() { |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
493 |
return cuTrees().iterator().next(); |
14d1224cfed3
8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents:
34092
diff
changeset
|
494 |
} |
33362 | 495 |
|
496 |
Diag diag(Diagnostic<? extends JavaFileObject> diag) { |
|
497 |
return sourceHandler.diag(diag); |
|
498 |
} |
|
499 |
||
500 |
Context getContext() { |
|
501 |
return context; |
|
502 |
} |
|
503 |
||
504 |
Types types() { |
|
505 |
if (types == null) { |
|
506 |
types = Types.instance(context); |
|
507 |
} |
|
508 |
return types; |
|
509 |
} |
|
510 |
||
511 |
JavacMessages messages() { |
|
512 |
if (messages == null) { |
|
513 |
messages = JavacMessages.instance(context); |
|
514 |
} |
|
515 |
return messages; |
|
516 |
} |
|
517 |
||
518 |
Trees trees() { |
|
519 |
if (trees == null) { |
|
520 |
trees = Trees.instance(task); |
|
521 |
} |
|
522 |
return trees; |
|
523 |
} |
|
524 |
||
525 |
// ------------------ diags functionality |
|
526 |
||
527 |
DiagList getDiagnostics() { |
|
528 |
if (diags == null) { |
|
529 |
LinkedHashMap<String, Diag> diagMap = new LinkedHashMap<>(); |
|
530 |
for (Diagnostic<? extends JavaFileObject> in : diagnostics.getDiagnostics()) { |
|
531 |
Diag d = diag(in); |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
532 |
String uniqueKey = d.getCode() + ":" + d.getPosition() + ":" + d.getMessage(PARSED_LOCALE); |
33362 | 533 |
diagMap.put(uniqueKey, d); |
534 |
} |
|
535 |
diags = new DiagList(diagMap.values()); |
|
536 |
} |
|
537 |
return diags; |
|
538 |
} |
|
539 |
||
540 |
boolean hasErrors() { |
|
541 |
return getDiagnostics().hasErrors(); |
|
542 |
} |
|
543 |
||
544 |
String shortErrorMessage() { |
|
545 |
StringBuilder sb = new StringBuilder(); |
|
546 |
for (Diag diag : getDiagnostics()) { |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
547 |
for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) { |
33362 | 548 |
if (!line.trim().startsWith("location:")) { |
549 |
sb.append(line); |
|
550 |
} |
|
551 |
} |
|
552 |
} |
|
553 |
return sb.toString(); |
|
554 |
} |
|
555 |
||
556 |
void debugPrintDiagnostics(String src) { |
|
557 |
for (Diag diag : getDiagnostics()) { |
|
558 |
state.debug(DBG_GEN, "ERROR --\n"); |
|
35000
952a7b4652f0
8146368: JShell: couldn't smash the error when it's Japanese locale
rfield
parents:
34857
diff
changeset
|
559 |
for (String line : diag.getMessage(PARSED_LOCALE).split("\\r?\\n")) { |
33362 | 560 |
if (!line.trim().startsWith("location:")) { |
561 |
state.debug(DBG_GEN, "%s\n", line); |
|
562 |
} |
|
563 |
} |
|
564 |
int start = (int) diag.getStartPosition(); |
|
565 |
int end = (int) diag.getEndPosition(); |
|
566 |
if (src != null) { |
|
567 |
String[] srcLines = src.split("\\r?\\n"); |
|
568 |
for (String line : srcLines) { |
|
569 |
state.debug(DBG_GEN, "%s\n", line); |
|
570 |
} |
|
571 |
||
572 |
StringBuilder sb = new StringBuilder(); |
|
573 |
for (int i = 0; i < start; ++i) { |
|
574 |
sb.append(' '); |
|
575 |
} |
|
576 |
sb.append('^'); |
|
577 |
if (end > start) { |
|
578 |
for (int i = start + 1; i < end; ++i) { |
|
579 |
sb.append('-'); |
|
580 |
} |
|
581 |
sb.append('^'); |
|
582 |
} |
|
583 |
state.debug(DBG_GEN, "%s\n", sb.toString()); |
|
584 |
} |
|
585 |
state.debug(DBG_GEN, "printDiagnostics start-pos = %d ==> %d -- wrap = %s\n", |
|
586 |
diag.getStartPosition(), start, this); |
|
587 |
state.debug(DBG_GEN, "Code: %s\n", diag.getCode()); |
|
588 |
state.debug(DBG_GEN, "Pos: %d (%d - %d) -- %s\n", diag.getPosition(), |
|
589 |
diag.getStartPosition(), diag.getEndPosition(), diag.getMessage(null)); |
|
590 |
} |
|
591 |
} |
|
592 |
} |
|
593 |
||
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
594 |
/**The variable types inferred for "var"s may be non-denotable. |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
595 |
* jshell desugars these variables into fields, and fields must have |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
596 |
* a denotable type. So these fields are declared with some simpler denotable |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
597 |
* type, and the listener here enhances the types of the fields to be the full |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
598 |
* inferred types. This is mainly when the inferred type contains: |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
599 |
* -intersection types (e.g. <Z extends Runnable&CharSequence> Z get() {...} var z = get();) |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
600 |
* -types that are inaccessible at the given place |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
601 |
* |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
602 |
* This type enhancement does not need to do anything about anonymous classes, as these |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
603 |
* are desugared into member classes. |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
604 |
*/ |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
605 |
private static final class TaskListenerImpl implements TaskListener { |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
606 |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
607 |
private final Context context; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
608 |
private final JShell state; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
609 |
/* Keep the original (declaration) types of the fields that were enhanced. |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
610 |
* The declaration types need to be put back before writing the fields |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
611 |
* into classfiles.*/ |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
612 |
private final Map<VarSymbol, Type> var2OriginalType = new HashMap<>(); |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
613 |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
614 |
public TaskListenerImpl(Context context, JShell state) { |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
615 |
this.context = context; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
616 |
this.state = state; |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
617 |
} |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
618 |
|
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
619 |
@Override |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
620 |
public void started(TaskEvent e) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
621 |
if (e.getKind() != TaskEvent.Kind.GENERATE) |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
622 |
return ; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
623 |
//clear enhanced types in fields we are about to write to the classfiles: |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
624 |
for (Tree clazz : e.getCompilationUnit().getTypeDecls()) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
625 |
ClassTree ct = (ClassTree) clazz; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
626 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
627 |
for (Tree member : ct.getMembers()) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
628 |
if (member.getKind() != Tree.Kind.VARIABLE) |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
629 |
continue; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
630 |
VarSymbol vsym = ((JCVariableDecl) member).sym; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
631 |
Type original = var2OriginalType.remove(vsym); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
632 |
if (original != null) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
633 |
vsym.type = original; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
634 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
635 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
636 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
637 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
638 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
639 |
private boolean variablesSet = false; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
640 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
641 |
@Override |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
642 |
public void finished(TaskEvent e) { |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
643 |
if (e.getKind() != TaskEvent.Kind.ENTER || variablesSet) |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
644 |
return ; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
645 |
state.maps |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
646 |
.snippetList() |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
647 |
.stream() |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
648 |
.filter(s -> s.status() == Status.VALID) |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
649 |
.filter(s -> s.kind() == Snippet.Kind.VAR) |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
650 |
.filter(s -> s.subKind() == Snippet.SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND || |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
651 |
s.subKind() == Snippet.SubKind.TEMP_VAR_EXPRESSION_SUBKIND) |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
652 |
.forEach(s -> setVariableType((VarSnippet) s)); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
653 |
variablesSet = true; |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
654 |
} |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
655 |
|
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
656 |
/* If the snippet contain enhanced types, enhance the type of |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
657 |
* the variable from snippet s to be the enhanced type. |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
658 |
*/ |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
659 |
private void setVariableType(VarSnippet s) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
660 |
String typeName = s.fullTypeName; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
661 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
662 |
if (typeName == null) |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
663 |
return ; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
664 |
|
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
665 |
Symtab syms = Symtab.instance(context); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
666 |
Names names = Names.instance(context); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
667 |
Log log = Log.instance(context); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
668 |
ParserFactory parserFactory = ParserFactory.instance(context); |
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
669 |
Attr attr = Attr.instance(context); |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
670 |
Enter enter = Enter.instance(context); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
671 |
DisableAccessibilityResolve rs = (DisableAccessibilityResolve) Resolve.instance(context); |
47350
d65c3b21081c
8186694: JShell: speed-up compilation by reusing compiler instances
jlahoda
parents:
47268
diff
changeset
|
672 |
|
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
673 |
//find the variable: |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
674 |
ClassSymbol clazz = syms.getClass(syms.unnamedModule, names.fromString(s.classFullName())); |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
675 |
if (clazz == null || !clazz.isCompleted()) |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
676 |
return; |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
677 |
VarSymbol field = (VarSymbol) clazz.members().findFirst(names.fromString(s.name()), sym -> sym.kind == Kinds.Kind.VAR); |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
678 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
679 |
if (field != null && !var2OriginalType.containsKey(field)) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
680 |
//if it was not enhanced yet: |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
681 |
//ignore any errors: |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
682 |
JavaFileObject prev = log.useSource(null); |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
683 |
DiscardDiagnosticHandler h = new DiscardDiagnosticHandler(log); |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
684 |
try { |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
685 |
//parse the type as a cast, i.e. "(<typeName>) x". This is to support |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
686 |
//intersection types: |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
687 |
CharBuffer buf = CharBuffer.wrap(("(" + typeName +")x\u0000").toCharArray(), 0, typeName.length() + 3); |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
688 |
Parser parser = parserFactory.newParser(buf, false, false, false); |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
689 |
JCExpression expr = parser.parseExpression(); |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
690 |
if (expr.hasTag(Tag.TYPECAST)) { |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
691 |
//if parsed OK, attribute and set the type: |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
692 |
var2OriginalType.put(field, field.type); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
693 |
|
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
694 |
JCTypeCast tree = (JCTypeCast) expr; |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
695 |
rs.runWithoutAccessChecks(() -> { |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
696 |
field.type = attr.attribType(tree.clazz, |
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
697 |
enter.getEnvs().iterator().next().enclClass.sym); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
698 |
}); |
47268
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
699 |
} |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
700 |
} finally { |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
701 |
log.popDiagnosticHandler(h); |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
702 |
log.useSource(prev); |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
703 |
} |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
704 |
} |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
705 |
} |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
706 |
} |
48ec75306997
8177466: Add compiler support for local variable type-inference
mcimadamore
parents:
47216
diff
changeset
|
707 |
|
48610
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
708 |
private static final class DisableAccessibilityResolve extends Resolve { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
709 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
710 |
public static void preRegister(Context context) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
711 |
if (context.get(Marker.class) == null) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
712 |
context.put(resolveKey, ((Factory<Resolve>) c -> new DisableAccessibilityResolve(c))); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
713 |
context.put(Marker.class, new Marker()); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
714 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
715 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
716 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
717 |
private boolean noAccessChecks; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
718 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
719 |
public DisableAccessibilityResolve(Context context) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
720 |
super(context); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
721 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
722 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
723 |
/**Run the given Runnable with all access checks disabled. |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
724 |
* |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
725 |
* @param r Runnnable to run |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
726 |
*/ |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
727 |
public void runWithoutAccessChecks(Runnable r) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
728 |
boolean prevNoAccessCheckes = noAccessChecks; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
729 |
try { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
730 |
noAccessChecks = true; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
731 |
r.run(); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
732 |
} finally { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
733 |
noAccessChecks = prevNoAccessCheckes; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
734 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
735 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
736 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
737 |
@Override |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
738 |
public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
739 |
if (noAccessChecks) return true; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
740 |
return super.isAccessible(env, c, checkInner); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
741 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
742 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
743 |
@Override |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
744 |
public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) { |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
745 |
if (noAccessChecks) return true; |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
746 |
return super.isAccessible(env, site, sym, checkInner); |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
747 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
748 |
|
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
749 |
private static final class Marker {} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
750 |
} |
a587f95313f1
8191842: JShell: Inferred type information is lost when assigning types to a \"var\"
jlahoda
parents:
47350
diff
changeset
|
751 |
|
33362 | 752 |
} |