author | jjg |
Fri, 27 May 2016 12:37:18 -0700 | |
changeset 38616 | 31e503b8ea65 |
parent 36526 | 3b41f1c69604 |
child 43871 | f164f4506389 |
permissions | -rw-r--r-- |
12016 | 1 |
/* |
38616 | 2 |
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. |
12016 | 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 com.sun.tools.javac.api; |
|
27 |
||
14258 | 28 |
import java.util.Collection; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
29 |
import java.util.LinkedHashSet; |
12016 | 30 |
import java.util.Locale; |
38616 | 31 |
import java.util.Objects; |
36526 | 32 |
import java.util.ServiceLoader; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
33 |
import java.util.Set; |
31506
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
34 |
import java.util.stream.Collectors; |
12016 | 35 |
|
36 |
import javax.annotation.processing.Processor; |
|
37 |
import javax.lang.model.element.Element; |
|
38 |
import javax.lang.model.type.TypeMirror; |
|
39 |
import javax.lang.model.util.Elements; |
|
40 |
import javax.lang.model.util.Types; |
|
41 |
import javax.tools.JavaFileObject; |
|
42 |
||
43 |
import com.sun.source.tree.CompilationUnitTree; |
|
44 |
import com.sun.source.tree.Tree; |
|
45 |
import com.sun.source.util.JavacTask; |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
46 |
import com.sun.source.util.Plugin; |
12016 | 47 |
import com.sun.source.util.TaskListener; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
48 |
import com.sun.tools.doclint.DocLint; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
49 |
import com.sun.tools.javac.main.JavaCompiler; |
12016 | 50 |
import com.sun.tools.javac.model.JavacElements; |
51 |
import com.sun.tools.javac.model.JavacTypes; |
|
31506
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
52 |
import com.sun.tools.javac.platform.PlatformDescription; |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
53 |
import com.sun.tools.javac.platform.PlatformDescription.PluginInfo; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
54 |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
12016 | 55 |
import com.sun.tools.javac.tree.JCTree; |
56 |
import com.sun.tools.javac.util.Context; |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
57 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
58 |
import com.sun.tools.javac.util.DefinedBy.Api; |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
59 |
import com.sun.tools.javac.util.List; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
60 |
import com.sun.tools.javac.util.Log; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
61 |
import com.sun.tools.javac.util.PropagatedException; |
12016 | 62 |
|
63 |
/** |
|
64 |
* Provides basic functionality for implementations of JavacTask. |
|
65 |
* |
|
66 |
* <p><b>This is NOT part of any supported API. |
|
67 |
* If you write code that depends on this, you do so at your own |
|
68 |
* risk. This code and its internal interfaces are subject to change |
|
69 |
* or deletion without notice.</b></p> |
|
70 |
*/ |
|
71 |
public class BasicJavacTask extends JavacTask { |
|
72 |
protected Context context; |
|
73 |
private TaskListener taskListener; |
|
74 |
||
14952 | 75 |
public static JavacTask instance(Context context) { |
76 |
JavacTask instance = context.get(JavacTask.class); |
|
77 |
if (instance == null) |
|
78 |
instance = new BasicJavacTask(context, true); |
|
79 |
return instance; |
|
80 |
} |
|
81 |
||
12016 | 82 |
public BasicJavacTask(Context c, boolean register) { |
83 |
context = c; |
|
84 |
if (register) |
|
85 |
context.put(JavacTask.class, this); |
|
86 |
} |
|
87 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
88 |
@Override @DefinedBy(Api.COMPILER_TREE) |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
89 |
public Iterable<? extends CompilationUnitTree> parse() { |
12016 | 90 |
throw new IllegalStateException(); |
91 |
} |
|
92 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
93 |
@Override @DefinedBy(Api.COMPILER_TREE) |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
94 |
public Iterable<? extends Element> analyze() { |
12016 | 95 |
throw new IllegalStateException(); |
96 |
} |
|
97 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
98 |
@Override @DefinedBy(Api.COMPILER_TREE) |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
99 |
public Iterable<? extends JavaFileObject> generate() { |
12016 | 100 |
throw new IllegalStateException(); |
101 |
} |
|
102 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
103 |
@Override @DefinedBy(Api.COMPILER_TREE) |
12016 | 104 |
public void setTaskListener(TaskListener tl) { |
105 |
MultiTaskListener mtl = MultiTaskListener.instance(context); |
|
106 |
if (taskListener != null) |
|
107 |
mtl.remove(taskListener); |
|
108 |
if (tl != null) |
|
109 |
mtl.add(tl); |
|
110 |
taskListener = tl; |
|
111 |
} |
|
112 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
113 |
@Override @DefinedBy(Api.COMPILER_TREE) |
12016 | 114 |
public void addTaskListener(TaskListener taskListener) { |
115 |
MultiTaskListener mtl = MultiTaskListener.instance(context); |
|
116 |
mtl.add(taskListener); |
|
117 |
} |
|
118 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
119 |
@Override @DefinedBy(Api.COMPILER_TREE) |
12016 | 120 |
public void removeTaskListener(TaskListener taskListener) { |
121 |
MultiTaskListener mtl = MultiTaskListener.instance(context); |
|
122 |
mtl.remove(taskListener); |
|
123 |
} |
|
124 |
||
125 |
public Collection<TaskListener> getTaskListeners() { |
|
126 |
MultiTaskListener mtl = MultiTaskListener.instance(context); |
|
127 |
return mtl.getTaskListeners(); |
|
128 |
} |
|
129 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
130 |
@Override @DefinedBy(Api.COMPILER_TREE) |
12016 | 131 |
public TypeMirror getTypeMirror(Iterable<? extends Tree> path) { |
132 |
// TODO: Should complete attribution if necessary |
|
133 |
Tree last = null; |
|
38616 | 134 |
for (Tree node : path) { |
135 |
last = Objects.requireNonNull(node); |
|
136 |
} |
|
137 |
if (last == null) { |
|
138 |
throw new IllegalArgumentException("empty path"); |
|
139 |
} |
|
140 |
return ((JCTree) last).type; |
|
12016 | 141 |
} |
142 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
143 |
@Override @DefinedBy(Api.COMPILER_TREE) |
12016 | 144 |
public Elements getElements() { |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
145 |
if (context == null) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
146 |
throw new IllegalStateException(); |
12016 | 147 |
return JavacElements.instance(context); |
148 |
} |
|
149 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
150 |
@Override @DefinedBy(Api.COMPILER_TREE) |
12016 | 151 |
public Types getTypes() { |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
152 |
if (context == null) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
153 |
throw new IllegalStateException(); |
12016 | 154 |
return JavacTypes.instance(context); |
155 |
} |
|
156 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
157 |
@Override @DefinedBy(Api.COMPILER) |
12016 | 158 |
public void setProcessors(Iterable<? extends Processor> processors) { |
159 |
throw new IllegalStateException(); |
|
160 |
} |
|
161 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
162 |
@Override @DefinedBy(Api.COMPILER) |
12016 | 163 |
public void setLocale(Locale locale) { |
164 |
throw new IllegalStateException(); |
|
165 |
} |
|
166 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
26264
diff
changeset
|
167 |
@Override @DefinedBy(Api.COMPILER) |
12016 | 168 |
public Boolean call() { |
169 |
throw new IllegalStateException(); |
|
170 |
} |
|
171 |
||
14044
0070e9c44020
7196462: JavacProcessingEnvironment should tolerate BasicJavacTask
jjg
parents:
12016
diff
changeset
|
172 |
/** |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
173 |
* For internal use only. |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
174 |
* This method will be removed without warning. |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
175 |
* @return the context |
14044
0070e9c44020
7196462: JavacProcessingEnvironment should tolerate BasicJavacTask
jjg
parents:
12016
diff
changeset
|
176 |
*/ |
14548
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14258
diff
changeset
|
177 |
public Context getContext() { |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14258
diff
changeset
|
178 |
return context; |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14258
diff
changeset
|
179 |
} |
aa687b312c97
8001098: Provide a simple light-weight "plug-in" mechanism for javac
jjg
parents:
14258
diff
changeset
|
180 |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
181 |
public void initPlugins(Set<List<String>> pluginOpts) { |
31506
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
182 |
PlatformDescription platformProvider = context.get(PlatformDescription.class); |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
183 |
|
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
184 |
if (platformProvider != null) { |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
185 |
for (PluginInfo<Plugin> pluginDesc : platformProvider.getPlugins()) { |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
186 |
java.util.List<String> options = |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
187 |
pluginDesc.getOptions().entrySet().stream() |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
188 |
.map(e -> e.getKey() + "=" + e.getValue()) |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
189 |
.collect(Collectors.toList()); |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
190 |
try { |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
191 |
pluginDesc.getPlugin().init(this, options.toArray(new String[options.size()])); |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
192 |
} catch (RuntimeException ex) { |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
193 |
throw new PropagatedException(ex); |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
194 |
} |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
195 |
} |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
196 |
} |
4e07f827a794
8072480: javac should support compilation for a specific platform version
jlahoda
parents:
27387
diff
changeset
|
197 |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
198 |
if (pluginOpts.isEmpty()) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
199 |
return; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
200 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
201 |
Set<List<String>> pluginsToCall = new LinkedHashSet<>(pluginOpts); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
202 |
JavacProcessingEnvironment pEnv = JavacProcessingEnvironment.instance(context); |
36526 | 203 |
ServiceLoader<Plugin> sl = pEnv.getServiceLoader(Plugin.class); |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
204 |
for (Plugin plugin : sl) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
205 |
for (List<String> p : pluginsToCall) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
206 |
if (plugin.getName().equals(p.head)) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
207 |
pluginsToCall.remove(p); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
208 |
try { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
209 |
plugin.init(this, p.tail.toArray(new String[p.tail.size()])); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
210 |
} catch (RuntimeException ex) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
211 |
throw new PropagatedException(ex); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
212 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
213 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
214 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
215 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
216 |
for (List<String> p: pluginsToCall) { |
27387
54142fc27e8a
8063039: incorrect message reference or broken message file
jjg
parents:
26266
diff
changeset
|
217 |
Log.instance(context).error("plugin.not.found", p.head); |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
218 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
219 |
} |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
220 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
221 |
public void initDocLint(List<String> docLintOpts) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
222 |
if (docLintOpts.isEmpty()) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
223 |
return; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
224 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
225 |
new DocLint().init(this, docLintOpts.toArray(new String[docLintOpts.size()])); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
226 |
JavaCompiler.instance(context).keepComments = true; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
25874
diff
changeset
|
227 |
} |
12016 | 228 |
} |