43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
|
|
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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.serviceprovider.processor;
|
|
24 |
|
|
25 |
import java.io.IOException;
|
|
26 |
import java.io.OutputStreamWriter;
|
|
27 |
import java.io.PrintWriter;
|
46371
|
28 |
import java.util.HashMap;
|
43972
|
29 |
import java.util.HashSet;
|
46371
|
30 |
import java.util.Map;
|
|
31 |
import java.util.Map.Entry;
|
43972
|
32 |
import java.util.Set;
|
|
33 |
|
|
34 |
import javax.annotation.processing.AbstractProcessor;
|
|
35 |
import javax.annotation.processing.FilerException;
|
|
36 |
import javax.annotation.processing.RoundEnvironment;
|
|
37 |
import javax.annotation.processing.SupportedAnnotationTypes;
|
|
38 |
import javax.lang.model.SourceVersion;
|
|
39 |
import javax.lang.model.element.Element;
|
48861
|
40 |
import javax.lang.model.element.ElementKind;
|
|
41 |
import javax.lang.model.element.PackageElement;
|
43972
|
42 |
import javax.lang.model.element.TypeElement;
|
|
43 |
import javax.lang.model.type.MirroredTypeException;
|
|
44 |
import javax.lang.model.type.TypeMirror;
|
|
45 |
import javax.tools.Diagnostic.Kind;
|
|
46 |
import javax.tools.FileObject;
|
|
47 |
import javax.tools.StandardLocation;
|
|
48 |
|
|
49 |
import org.graalvm.compiler.serviceprovider.ServiceProvider;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Processes classes annotated with {@link ServiceProvider}. For a service defined by {@code S} and
|
|
53 |
* a class {@code P} implementing the service, this processor generates the file
|
|
54 |
* {@code META-INF/providers/P} whose contents are a single line containing the fully qualified name
|
|
55 |
* of {@code S}.
|
|
56 |
*/
|
|
57 |
@SupportedAnnotationTypes("org.graalvm.compiler.serviceprovider.ServiceProvider")
|
|
58 |
public class ServiceProviderProcessor extends AbstractProcessor {
|
|
59 |
|
|
60 |
private final Set<TypeElement> processed = new HashSet<>();
|
46371
|
61 |
private final Map<TypeElement, String> serviceProviders = new HashMap<>();
|
43972
|
62 |
|
|
63 |
@Override
|
|
64 |
public SourceVersion getSupportedSourceVersion() {
|
|
65 |
return SourceVersion.latest();
|
|
66 |
}
|
|
67 |
|
|
68 |
private boolean verifyAnnotation(TypeMirror serviceInterface, TypeElement serviceProvider) {
|
|
69 |
if (!processingEnv.getTypeUtils().isSubtype(serviceProvider.asType(), serviceInterface)) {
|
|
70 |
String msg = String.format("Service provider class %s must implement service interface %s", serviceProvider.getSimpleName(), serviceInterface);
|
|
71 |
processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider);
|
|
72 |
return false;
|
|
73 |
}
|
|
74 |
|
|
75 |
return true;
|
|
76 |
}
|
|
77 |
|
|
78 |
private void processElement(TypeElement serviceProvider) {
|
|
79 |
if (processed.contains(serviceProvider)) {
|
|
80 |
return;
|
|
81 |
}
|
|
82 |
|
|
83 |
processed.add(serviceProvider);
|
|
84 |
ServiceProvider annotation = serviceProvider.getAnnotation(ServiceProvider.class);
|
|
85 |
if (annotation != null) {
|
|
86 |
try {
|
|
87 |
annotation.value();
|
|
88 |
} catch (MirroredTypeException ex) {
|
48861
|
89 |
TypeMirror service = ex.getTypeMirror();
|
|
90 |
if (verifyAnnotation(service, serviceProvider)) {
|
46371
|
91 |
if (serviceProvider.getNestingKind().isNested()) {
|
|
92 |
/*
|
|
93 |
* This is a simplifying constraint that means we don't have to process the
|
|
94 |
* qualified name to insert '$' characters at the relevant positions.
|
|
95 |
*/
|
|
96 |
String msg = String.format("Service provider class %s must be a top level class", serviceProvider.getSimpleName());
|
|
97 |
processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider);
|
|
98 |
} else {
|
48861
|
99 |
/*
|
|
100 |
* Since the definition of the service class is not necessarily modifiable,
|
|
101 |
* we need to support a non-top-level service class and ensure its name is
|
|
102 |
* properly expressed with '$' separating nesting levels instead of '.'.
|
|
103 |
*/
|
|
104 |
TypeElement serviceElement = (TypeElement) processingEnv.getTypeUtils().asElement(service);
|
|
105 |
String serviceName = serviceElement.getSimpleName().toString();
|
|
106 |
Element enclosing = serviceElement.getEnclosingElement();
|
|
107 |
while (enclosing != null) {
|
|
108 |
final ElementKind kind = enclosing.getKind();
|
|
109 |
if (kind == ElementKind.PACKAGE) {
|
|
110 |
serviceName = ((PackageElement) enclosing).getQualifiedName().toString() + "." + serviceName;
|
|
111 |
break;
|
|
112 |
} else if (kind == ElementKind.CLASS || kind == ElementKind.INTERFACE) {
|
|
113 |
serviceName = ((TypeElement) enclosing).getSimpleName().toString() + "$" + serviceName;
|
|
114 |
enclosing = enclosing.getEnclosingElement();
|
|
115 |
} else {
|
|
116 |
String msg = String.format("Cannot generate provider descriptor for service class %s as it is not nested in a package, class or interface",
|
|
117 |
serviceElement.getQualifiedName());
|
|
118 |
processingEnv.getMessager().printMessage(Kind.ERROR, msg, serviceProvider);
|
|
119 |
return;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
serviceProviders.put(serviceProvider, serviceName);
|
46371
|
123 |
}
|
43972
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
46371
|
129 |
private void writeProviderFile(TypeElement serviceProvider, String interfaceName) {
|
43972
|
130 |
String filename = "META-INF/providers/" + serviceProvider.getQualifiedName();
|
|
131 |
try {
|
|
132 |
FileObject file = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", filename, serviceProvider);
|
|
133 |
PrintWriter writer = new PrintWriter(new OutputStreamWriter(file.openOutputStream(), "UTF-8"));
|
|
134 |
writer.println(interfaceName);
|
|
135 |
writer.close();
|
|
136 |
} catch (IOException e) {
|
|
137 |
processingEnv.getMessager().printMessage(isBug367599(e) ? Kind.NOTE : Kind.ERROR, e.getMessage(), serviceProvider);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
/**
|
|
142 |
* Determines if a given exception is (most likely) caused by
|
|
143 |
* <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599">Bug 367599</a>.
|
|
144 |
*/
|
46371
|
145 |
private static boolean isBug367599(Throwable t) {
|
43972
|
146 |
if (t instanceof FilerException) {
|
|
147 |
for (StackTraceElement ste : t.getStackTrace()) {
|
|
148 |
if (ste.toString().contains("org.eclipse.jdt.internal.apt.pluggable.core.filer.IdeFilerImpl.create")) {
|
|
149 |
// See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599
|
|
150 |
return true;
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
46371
|
154 |
return t.getCause() != null && isBug367599(t.getCause());
|
43972
|
155 |
}
|
|
156 |
|
|
157 |
@Override
|
|
158 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
|
159 |
if (roundEnv.processingOver()) {
|
46371
|
160 |
for (Entry<TypeElement, String> e : serviceProviders.entrySet()) {
|
|
161 |
writeProviderFile(e.getKey(), e.getValue());
|
|
162 |
}
|
|
163 |
serviceProviders.clear();
|
43972
|
164 |
return true;
|
|
165 |
}
|
|
166 |
|
|
167 |
for (Element element : roundEnv.getElementsAnnotatedWith(ServiceProvider.class)) {
|
|
168 |
assert element.getKind().isClass();
|
|
169 |
processElement((TypeElement) element);
|
|
170 |
}
|
|
171 |
|
|
172 |
return true;
|
|
173 |
}
|
|
174 |
}
|