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