Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.openide.util; |
43 |
|
44 |
import java.io.BufferedReader; |
45 |
import java.io.IOException; |
46 |
import java.io.InputStream; |
47 |
import java.io.InputStreamReader; |
48 |
import java.lang.annotation.Annotation; |
49 |
import java.net.URL; |
50 |
import java.util.ArrayList; |
51 |
import java.util.Enumeration; |
52 |
import java.util.HashSet; |
53 |
import java.util.List; |
54 |
import java.util.Set; |
55 |
import java.util.regex.Matcher; |
56 |
import java.util.regex.Pattern; |
57 |
import javax.annotation.processing.RoundEnvironment; |
58 |
import javax.annotation.processing.SupportedSourceVersion; |
59 |
import javax.lang.model.SourceVersion; |
60 |
import javax.lang.model.element.Element; |
61 |
import javax.lang.model.element.TypeElement; |
62 |
import javax.lang.model.type.TypeMirror; |
63 |
import javax.tools.Diagnostic; |
64 |
import org.openide.util.lookup.NamedServiceDefinition; |
65 |
import org.openide.util.lookup.implspi.AbstractServiceProviderProcessor; |
66 |
|
67 |
@SupportedSourceVersion(SourceVersion.RELEASE_6) |
68 |
public final class NamedServiceProcessor extends AbstractServiceProviderProcessor { |
69 |
private static final String PATH = "META-INF/namedservices.index"; // NOI18N |
70 |
|
71 |
@Override |
72 |
public Set<String> getSupportedAnnotationTypes() { |
73 |
Set<String> all = new HashSet<String>(); |
74 |
all.add(NamedServiceDefinition.class.getName()); |
75 |
searchAnnotations(all, true); |
76 |
return all; |
77 |
} |
78 |
|
79 |
|
80 |
@Override |
81 |
protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
82 |
for (Element e : roundEnv.getElementsAnnotatedWith(NamedServiceDefinition.class)) { |
83 |
NamedServiceDefinition nsd = e.getAnnotation(NamedServiceDefinition.class); |
84 |
register(e, PATH); |
85 |
} |
86 |
|
87 |
Set<String> index = new HashSet<String>(); |
88 |
searchAnnotations(index, false); |
89 |
for (String className : index) { |
90 |
Class<? extends Annotation> c; |
91 |
try { |
92 |
c = Class.forName(className).asSubclass(Annotation.class); |
93 |
} catch (ClassNotFoundException ex) { |
94 |
throw new IllegalStateException(ex); |
95 |
} |
96 |
for (Element e : roundEnv.getElementsAnnotatedWith(c)) { |
97 |
Annotation a = e.getAnnotation(c); |
98 |
NamedServiceDefinition nsd = c.getAnnotation(NamedServiceDefinition.class); |
99 |
int cnt = 0; |
100 |
for (Class<?> type : nsd.serviceType()) { |
101 |
TypeMirror typeMirror = asType(type); |
102 |
if (processingEnv.getTypeUtils().isSubtype(e.asType(), typeMirror)) { |
103 |
cnt++; |
104 |
for (String p : findPath(nsd.path(), a)) { |
105 |
register( |
106 |
e, c, typeMirror, p, |
107 |
findPosition(nsd.position(), a) |
108 |
); |
109 |
} |
110 |
} |
111 |
if (cnt == 0) { |
112 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Not right subclass!", e); |
113 |
} |
114 |
} |
115 |
} |
116 |
} |
117 |
return true; |
118 |
} |
119 |
|
120 |
private TypeMirror asType(Class<?> type) { |
121 |
return processingEnv.getElementUtils().getTypeElement(type.getName()).asType(); |
122 |
} |
123 |
|
124 |
private static Pattern reference; |
125 |
private List<String> findPath(String path, Annotation a) { |
126 |
if (reference == null) { |
127 |
reference = Pattern.compile("@([^/]+)\\(\\)"); // NOI18N |
128 |
} |
129 |
List<String> arr = new ArrayList<String>(); |
130 |
arr.add(path); |
131 |
RESTART: for (;;) { |
132 |
for (int i = 0; i < arr.size(); i++) { |
133 |
Matcher m = reference.matcher(arr.get(i)); |
134 |
if (m.find()) { |
135 |
String methodName = m.group(1); |
136 |
System.err.println("search for " + m.group(1) + " in " + path); |
137 |
Object obj; |
138 |
try { |
139 |
obj = a.getClass().getMethod(methodName).invoke(a); |
140 |
} catch (Exception ex) { |
141 |
throw new IllegalStateException(methodName, ex); |
142 |
} |
143 |
if (obj instanceof String) { |
144 |
arr.set(i, substitute(path, m, (String)obj)); |
145 |
} else if (obj instanceof String[]) { |
146 |
String[] subs = (String[])obj; |
147 |
arr.set(i, substitute(path, m, subs[0])); |
148 |
for (int j = 1; j < subs.length; j++) { |
149 |
arr.add(substitute(path, m, subs[j])); |
150 |
} |
151 |
} else { |
152 |
throw new IllegalStateException("Wrong return value " + obj); // NOI18N |
153 |
} |
154 |
continue RESTART; |
155 |
} |
156 |
} |
157 |
break RESTART; |
158 |
} |
159 |
return arr; |
160 |
} |
161 |
|
162 |
private Integer findPosition(String posDefinition, Annotation a) { |
163 |
if (posDefinition.length() == 1 && posDefinition.charAt(0) == 0) { |
164 |
try { |
165 |
return (Integer)a.getClass().getMethod("position").invoke(a); |
166 |
} catch (NoSuchMethodException ex) { |
167 |
return Integer.MAX_VALUE; |
168 |
} catch (Exception ex) { |
169 |
throw new IllegalStateException(ex); |
170 |
} |
171 |
} |
172 |
try { |
173 |
return (Integer)a.getClass().getMethod(posDefinition).invoke(a); |
174 |
} catch (Exception ex) { |
175 |
throw new IllegalStateException(ex); |
176 |
} |
177 |
} |
178 |
|
179 |
private static void searchAnnotations(Set<String> found, boolean canonicalName) { |
180 |
|
181 |
try { |
182 |
Enumeration<URL> en = NamedServiceProcessor.class.getClassLoader().getResources(PATH); |
183 |
while (en.hasMoreElements()) { |
184 |
URL url = en.nextElement(); |
185 |
InputStream is = url.openStream(); |
186 |
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); // NOI18N |
187 |
|
188 |
// XXX consider using ServiceLoaderLine instead |
189 |
while (true) { |
190 |
String line = reader.readLine(); |
191 |
|
192 |
if (line == null) { |
193 |
break; |
194 |
} |
195 |
line = line.trim(); |
196 |
if (line.startsWith("#")) { // NOI18N |
197 |
continue; |
198 |
} |
199 |
if (canonicalName) { |
200 |
line = line.replace('$', '.'); |
201 |
} |
202 |
found.add(line); |
203 |
} |
204 |
} |
205 |
} catch (IOException ex) { |
206 |
throw new IllegalStateException(ex); |
207 |
} |
208 |
} |
209 |
|
210 |
private static String substitute(String path, Matcher m, String obj) { |
211 |
return path.substring(0, m.start(0)) + obj + path.substring(m.end(0)); |
212 |
} |
213 |
} |