1 /*
2 * Copyright (c) 1997, 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. 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
48 import javax.lang.model.type.TypeVariable;
49 import javax.lang.model.type.TypeVisitor;
50 import javax.lang.model.type.WildcardType;
51 import javax.lang.model.util.ElementFilter;
52 import javax.lang.model.util.Elements;
53 import javax.lang.model.util.SimpleTypeVisitor6;
54 import javax.lang.model.util.Types;
55 import java.lang.annotation.Annotation;
56 import java.util.Collection;
57 import java.util.HashMap;
58 import java.util.HashSet;
59 import java.util.List;
60 import java.util.Map;
61
62 /**
63 * {@link Navigator} implementation for annotation processing.
64 * TODO: check the spec on how generics are supposed to be handled
65 *
66 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
67 */
68 public class ApNavigator implements Navigator<TypeMirror, TypeElement, VariableElement, ExecutableElement> {
69
70 private final ProcessingEnvironment env;
71
72 private final PrimitiveType primitiveByte;
73
74 public ApNavigator(ProcessingEnvironment env) {
75 this.env = env;
76 this.primitiveByte = env.getTypeUtils().getPrimitiveType(TypeKind.BYTE);
77 }
78
79 public TypeElement getSuperClass(TypeElement typeElement) {
80 if (typeElement.getKind().equals(ElementKind.CLASS)) {
81 TypeMirror sup = typeElement.getSuperclass();
82 if (!sup.getKind().equals(TypeKind.NONE))
83 return (TypeElement) ((DeclaredType) sup).asElement();
84 else
85 return null;
86 }
87 return env.getElementUtils().getTypeElement(Object.class.getName());
88 }
219 public TypeElement asDecl(Class c) {
220 return env.getElementUtils().getTypeElement(getSourceClassName(c));
221 }
222
223 public TypeMirror erasure(TypeMirror t) {
224 Types tu = env.getTypeUtils();
225 t = tu.erasure(t);
226 if (t.getKind().equals(TypeKind.DECLARED)) {
227 DeclaredType dt = (DeclaredType)t;
228 if (!dt.getTypeArguments().isEmpty())
229 return tu.getDeclaredType((TypeElement) dt.asElement());
230 }
231 return t;
232 }
233
234 public boolean isAbstract(TypeElement clazz) {
235 return hasModifier(clazz,Modifier.ABSTRACT);
236 }
237
238 public boolean isFinal(TypeElement clazz) {
239 return hasModifier(clazz,Modifier.FINAL);
240 }
241
242 public VariableElement[] getEnumConstants(TypeElement clazz) {
243 List<? extends Element> elements = env.getElementUtils().getAllMembers(clazz);
244 Collection<VariableElement> constants = new HashSet<VariableElement>();
245 for (Element element : elements) {
246 if (element.getKind().equals(ElementKind.ENUM_CONSTANT)) {
247 constants.add((VariableElement) element);
248 }
249 }
250 return constants.toArray(new VariableElement[constants.size()]);
251 }
252
253 public TypeMirror getVoidType() {
254 return env.getTypeUtils().getNoType(TypeKind.VOID);
255 }
256
257 public String getPackageName(TypeElement clazz) {
258 return env.getElementUtils().getPackageOf(clazz).getQualifiedName().toString();
259 }
260
261 public TypeElement findClass(String className, TypeElement referencePoint) {
262 return env.getElementUtils().getTypeElement(className);
263 }
264
265 public boolean isBridgeMethod(ExecutableElement method) {
266 return method.getModifiers().contains(Modifier.VOLATILE);
267 }
268
269 public boolean isOverriding(ExecutableElement method, TypeElement base) {
270 Elements elements = env.getElementUtils();
271
272 while (true) {
273 for (ExecutableElement m : ElementFilter.methodsIn(elements.getAllMembers(base))) {
274 if (elements.overrides(method, m, base))
275 return true;
276 }
277
278 if (base.getSuperclass().getKind().equals(TypeKind.NONE))
279 return false;
280 base = (TypeElement) env.getTypeUtils().asElement(base.getSuperclass());
281 }
282 }
|
1 /*
2 * Copyright (c) 1997, 2014, 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. 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
48 import javax.lang.model.type.TypeVariable;
49 import javax.lang.model.type.TypeVisitor;
50 import javax.lang.model.type.WildcardType;
51 import javax.lang.model.util.ElementFilter;
52 import javax.lang.model.util.Elements;
53 import javax.lang.model.util.SimpleTypeVisitor6;
54 import javax.lang.model.util.Types;
55 import java.lang.annotation.Annotation;
56 import java.util.Collection;
57 import java.util.HashMap;
58 import java.util.HashSet;
59 import java.util.List;
60 import java.util.Map;
61
62 /**
63 * {@link Navigator} implementation for annotation processing.
64 * TODO: check the spec on how generics are supposed to be handled
65 *
66 * @author Kohsuke Kawaguchi (kk@kohsuke.org)
67 */
68 public final class ApNavigator implements Navigator<TypeMirror, TypeElement, VariableElement, ExecutableElement> {
69
70 private final ProcessingEnvironment env;
71
72 private final PrimitiveType primitiveByte;
73
74 public ApNavigator(ProcessingEnvironment env) {
75 this.env = env;
76 this.primitiveByte = env.getTypeUtils().getPrimitiveType(TypeKind.BYTE);
77 }
78
79 public TypeElement getSuperClass(TypeElement typeElement) {
80 if (typeElement.getKind().equals(ElementKind.CLASS)) {
81 TypeMirror sup = typeElement.getSuperclass();
82 if (!sup.getKind().equals(TypeKind.NONE))
83 return (TypeElement) ((DeclaredType) sup).asElement();
84 else
85 return null;
86 }
87 return env.getElementUtils().getTypeElement(Object.class.getName());
88 }
219 public TypeElement asDecl(Class c) {
220 return env.getElementUtils().getTypeElement(getSourceClassName(c));
221 }
222
223 public TypeMirror erasure(TypeMirror t) {
224 Types tu = env.getTypeUtils();
225 t = tu.erasure(t);
226 if (t.getKind().equals(TypeKind.DECLARED)) {
227 DeclaredType dt = (DeclaredType)t;
228 if (!dt.getTypeArguments().isEmpty())
229 return tu.getDeclaredType((TypeElement) dt.asElement());
230 }
231 return t;
232 }
233
234 public boolean isAbstract(TypeElement clazz) {
235 return hasModifier(clazz,Modifier.ABSTRACT);
236 }
237
238 public boolean isFinal(TypeElement clazz) {
239 return hasModifier(clazz, Modifier.FINAL);
240 }
241
242 public VariableElement[] getEnumConstants(TypeElement clazz) {
243 List<? extends Element> elements = env.getElementUtils().getAllMembers(clazz);
244 Collection<VariableElement> constants = new HashSet<VariableElement>();
245 for (Element element : elements) {
246 if (element.getKind().equals(ElementKind.ENUM_CONSTANT)) {
247 constants.add((VariableElement) element);
248 }
249 }
250 return constants.toArray(new VariableElement[constants.size()]);
251 }
252
253 public TypeMirror getVoidType() {
254 return env.getTypeUtils().getNoType(TypeKind.VOID);
255 }
256
257 public String getPackageName(TypeElement clazz) {
258 return env.getElementUtils().getPackageOf(clazz).getQualifiedName().toString();
259 }
260
261 @Override
262 public TypeElement loadObjectFactory(TypeElement referencePoint, String packageName) {
263 return env.getElementUtils().getTypeElement(packageName + ".ObjectFactory");
264 }
265
266 public boolean isBridgeMethod(ExecutableElement method) {
267 return method.getModifiers().contains(Modifier.VOLATILE);
268 }
269
270 public boolean isOverriding(ExecutableElement method, TypeElement base) {
271 Elements elements = env.getElementUtils();
272
273 while (true) {
274 for (ExecutableElement m : ElementFilter.methodsIn(elements.getAllMembers(base))) {
275 if (elements.overrides(method, m, base))
276 return true;
277 }
278
279 if (base.getSuperclass().getKind().equals(TypeKind.NONE))
280 return false;
281 base = (TypeElement) env.getTypeUtils().asElement(base.getSuperclass());
282 }
283 }
|