Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2009 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2009 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.modules.openide.util; |
41 |
|
42 |
import java.io.BufferedReader; |
43 |
import java.io.FileNotFoundException; |
44 |
import java.io.IOException; |
45 |
import java.io.InputStream; |
46 |
import java.io.InputStreamReader; |
47 |
import java.io.OutputStream; |
48 |
import java.io.OutputStreamWriter; |
49 |
import java.io.PrintWriter; |
50 |
import java.lang.annotation.Annotation; |
51 |
import java.util.ArrayList; |
52 |
import java.util.HashMap; |
53 |
import java.util.List; |
54 |
import java.util.Map; |
55 |
import java.util.Set; |
56 |
import java.util.WeakHashMap; |
57 |
import javax.annotation.processing.AbstractProcessor; |
58 |
import javax.annotation.processing.ProcessingEnvironment; |
59 |
import javax.annotation.processing.RoundEnvironment; |
60 |
import javax.lang.model.element.AnnotationMirror; |
61 |
import javax.lang.model.element.AnnotationValue; |
62 |
import javax.lang.model.element.Element; |
63 |
import javax.lang.model.element.ExecutableElement; |
64 |
import javax.lang.model.element.Modifier; |
65 |
import javax.lang.model.element.TypeElement; |
66 |
import javax.lang.model.type.TypeMirror; |
67 |
import javax.lang.model.util.ElementFilter; |
68 |
import javax.tools.Diagnostic.Kind; |
69 |
import javax.tools.FileObject; |
70 |
import javax.tools.StandardLocation; |
71 |
|
72 |
/** |
73 |
* Infrastructure for generating {@code META-INF/services/*} and |
74 |
* {@code META-INF/namedservices/*} registrations from annotations. |
75 |
*/ |
76 |
public abstract class AbstractServiceProviderProcessor extends AbstractProcessor { |
77 |
|
78 |
private final Map<ProcessingEnvironment,Map<String,List<String>>> outputFilesByProcessor = new WeakHashMap<ProcessingEnvironment,Map<String,List<String>>>(); |
79 |
private final Map<ProcessingEnvironment,Map<String,List<Element>>> originatingElementsByProcessor = new WeakHashMap<ProcessingEnvironment,Map<String,List<Element>>>(); |
80 |
private final Map<TypeElement,Boolean> verifiedClasses = new WeakHashMap<TypeElement,Boolean>(); |
81 |
|
82 |
/** For access by subclasses. */ |
83 |
protected AbstractServiceProviderProcessor() {} |
84 |
|
85 |
public @Override final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
86 |
if (roundEnv.errorRaised()) { |
87 |
return false; |
88 |
} |
89 |
if (roundEnv.processingOver()) { |
90 |
writeServices(); |
91 |
outputFilesByProcessor.clear(); |
92 |
originatingElementsByProcessor.clear(); |
93 |
return true; |
94 |
} else { |
95 |
return handleProcess(annotations, roundEnv); |
96 |
} |
97 |
} |
98 |
|
99 |
/** |
100 |
* The regular body of {@link #process}. |
101 |
* Called during regular rounds if there are no outstanding errors. |
102 |
* In the last round, one of the processors will write out generated registrations. |
103 |
* @param annotations as in {@link #process} |
104 |
* @param roundEnv as in {@link #process} |
105 |
* @return as in {@link #process} |
106 |
*/ |
107 |
protected abstract boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv); |
108 |
|
109 |
/** |
110 |
* Register a service. |
111 |
* If the class does not have an appropriate signature, an error will be printed and the registration skipped. |
112 |
* @param clazz the service implementation type |
113 |
* @param annotation the (top-level) annotation registering the service, for diagnostic purposes |
114 |
* @param type the type to which the implementation must be assignable |
115 |
* @param path a path under which to register, or "" if inapplicable |
116 |
* @param position a position at which to register, or {@link Integer#MAX_VALUE} to skip |
117 |
* @param supersedes possibly empty list of implementation to supersede |
118 |
*/ |
119 |
protected final void register(TypeElement clazz, Class<? extends Annotation> annotation, |
120 |
TypeMirror type, String path, int position, String[] supersedes) { |
121 |
Boolean verify = verifiedClasses.get(clazz); |
122 |
if (verify == null) { |
123 |
verify = verifyServiceProviderSignature(clazz, annotation); |
124 |
verifiedClasses.put(clazz, verify); |
125 |
} |
126 |
if (!verify) { |
127 |
return; |
128 |
} |
129 |
String impl = processingEnv.getElementUtils().getBinaryName(clazz).toString(); |
130 |
String xface = processingEnv.getElementUtils().getBinaryName((TypeElement) processingEnv.getTypeUtils().asElement(type)).toString(); |
131 |
if (!processingEnv.getTypeUtils().isAssignable(clazz.asType(), type)) { |
132 |
AnnotationMirror ann = findAnnotationMirror(clazz, annotation); |
133 |
processingEnv.getMessager().printMessage(Kind.ERROR, impl + " is not assignable to " + xface, |
134 |
clazz, ann, findAnnotationValue(ann, "service")); |
135 |
return; |
136 |
} |
137 |
processingEnv.getMessager().printMessage(Kind.NOTE, |
138 |
impl + " to be registered as a " + xface + (path.length() > 0 ? " under " + path : "")); |
139 |
String rsrc = (path.length() > 0 ? "META-INF/namedservices/" + path + "/" : "META-INF/services/") + xface; |
140 |
{ |
141 |
Map<String,List<Element>> originatingElements = originatingElementsByProcessor.get(processingEnv); |
142 |
if (originatingElements == null) { |
143 |
originatingElements = new HashMap<String,List<Element>>(); |
144 |
originatingElementsByProcessor.put(processingEnv, originatingElements); |
145 |
} |
146 |
List<Element> origEls = originatingElements.get(rsrc); |
147 |
if (origEls == null) { |
148 |
origEls = new ArrayList<Element>(); |
149 |
originatingElements.put(rsrc, origEls); |
150 |
} |
151 |
origEls.add(clazz); |
152 |
} |
153 |
Map<String,List<String>> outputFiles = outputFilesByProcessor.get(processingEnv); |
154 |
if (outputFiles == null) { |
155 |
outputFiles = new HashMap<String,List<String>>(); |
156 |
outputFilesByProcessor.put(processingEnv, outputFiles); |
157 |
} |
158 |
List<String> lines = outputFiles.get(rsrc); |
159 |
if (lines == null) { |
160 |
lines = new ArrayList<String>(); |
161 |
try { |
162 |
try { |
163 |
FileObject in = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", rsrc); |
164 |
in.openInputStream().close(); |
165 |
processingEnv.getMessager().printMessage(Kind.ERROR, |
166 |
"Cannot generate " + rsrc + " because it already exists in sources: " + in.toUri()); |
167 |
return; |
168 |
} catch (FileNotFoundException x) { |
169 |
// Good. |
170 |
} |
171 |
try { |
172 |
FileObject in = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", rsrc); |
173 |
InputStream is = in.openInputStream(); |
174 |
try { |
175 |
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8")); |
176 |
String line; |
177 |
while ((line = r.readLine()) != null) { |
178 |
lines.add(line); |
179 |
} |
180 |
} finally { |
181 |
is.close(); |
182 |
} |
183 |
} catch (FileNotFoundException x) { |
184 |
// OK, created for the first time |
185 |
} |
186 |
} catch (IOException x) { |
187 |
processingEnv.getMessager().printMessage(Kind.ERROR, x.toString()); |
188 |
return; |
189 |
} |
190 |
outputFiles.put(rsrc, lines); |
191 |
} |
192 |
int idx = lines.indexOf(impl); |
193 |
if (idx != -1) { |
194 |
lines.remove(idx); |
195 |
while (lines.size() > idx && lines.get(idx).matches("#position=.+|#-.+")) { |
196 |
lines.remove(idx); |
197 |
} |
198 |
} |
199 |
lines.add(impl); |
200 |
if (position != Integer.MAX_VALUE) { |
201 |
lines.add("#position=" + position); |
202 |
} |
203 |
for (String exclude : supersedes) { |
204 |
lines.add("#-" + exclude); |
205 |
} |
206 |
} |
207 |
|
208 |
/** |
209 |
* @param element a source element |
210 |
* @param annotation a type of annotation |
211 |
* @return the instance of that annotation on the element, or null if not found |
212 |
*/ |
213 |
private AnnotationMirror findAnnotationMirror(Element element, Class<? extends Annotation> annotation) { |
214 |
for (AnnotationMirror ann : element.getAnnotationMirrors()) { |
215 |
if (processingEnv.getElementUtils().getBinaryName((TypeElement) ann.getAnnotationType().asElement()). |
216 |
contentEquals(annotation.getName())) { |
217 |
return ann; |
218 |
} |
219 |
} |
220 |
return null; |
221 |
} |
222 |
|
223 |
/** |
224 |
* @param annotation an annotation instance (null permitted) |
225 |
* @param name the name of an attribute of that annotation |
226 |
* @return the corresponding value if found |
227 |
*/ |
228 |
private AnnotationValue findAnnotationValue(AnnotationMirror annotation, String name) { |
229 |
if (annotation != null) { |
230 |
for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : annotation.getElementValues().entrySet()) { |
231 |
if (entry.getKey().getSimpleName().contentEquals(name)) { |
232 |
return entry.getValue(); |
233 |
} |
234 |
} |
235 |
} |
236 |
return null; |
237 |
} |
238 |
|
239 |
private final boolean verifyServiceProviderSignature(TypeElement clazz, Class<? extends Annotation> annotation) { |
240 |
AnnotationMirror ann = findAnnotationMirror(clazz, annotation); |
241 |
if (!clazz.getModifiers().contains(Modifier.PUBLIC)) { |
242 |
processingEnv.getMessager().printMessage(Kind.ERROR, clazz + " must be public", clazz, ann); |
243 |
return false; |
244 |
} |
245 |
if (clazz.getModifiers().contains(Modifier.ABSTRACT)) { |
246 |
processingEnv.getMessager().printMessage(Kind.ERROR, clazz + " must not be abstract", clazz, ann); |
247 |
return false; |
248 |
} |
249 |
{ |
250 |
boolean hasDefaultCtor = false; |
251 |
for (ExecutableElement constructor : ElementFilter.constructorsIn(clazz.getEnclosedElements())) { |
252 |
if (constructor.getModifiers().contains(Modifier.PUBLIC) && constructor.getParameters().isEmpty()) { |
253 |
hasDefaultCtor = true; |
254 |
break; |
255 |
} |
256 |
} |
257 |
if (!hasDefaultCtor) { |
258 |
processingEnv.getMessager().printMessage(Kind.ERROR, clazz + " must have a public no-argument constructor", clazz, ann); |
259 |
return false; |
260 |
} |
261 |
} |
262 |
return true; |
263 |
} |
264 |
|
265 |
private void writeServices() { |
266 |
for (Map.Entry<ProcessingEnvironment,Map<String,List<String>>> outputFiles : outputFilesByProcessor.entrySet()) { |
267 |
for (Map.Entry<String, List<String>> entry : outputFiles.getValue().entrySet()) { |
268 |
try { |
269 |
FileObject out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", entry.getKey(), |
270 |
originatingElementsByProcessor.get(outputFiles.getKey()).get(entry.getKey()).toArray(new Element[0])); |
271 |
OutputStream os = out.openOutputStream(); |
272 |
try { |
273 |
PrintWriter w = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); |
274 |
for (String line : entry.getValue()) { |
275 |
w.println(line); |
276 |
} |
277 |
w.flush(); |
278 |
w.close(); |
279 |
} finally { |
280 |
os.close(); |
281 |
} |
282 |
} catch (IOException x) { |
283 |
processingEnv.getMessager().printMessage(Kind.ERROR, "Failed to write to " + entry.getKey() + ": " + x.toString()); |
284 |
} |
285 |
} |
286 |
} |
287 |
} |
288 |
|
289 |
} |