Wed, 26 Mar 2014 12:18:11 +0100
8038080: annotation processors don't visit declaration parameter annotations
Reviewed-by: darcy
Contributed-by: cushon@google.com, joel.franck@oracle.com
duke@1 | 1 | /* |
jlahoda@2254 | 2 | * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.processing; |
duke@1 | 27 | |
duke@1 | 28 | import java.lang.annotation.Annotation; |
duke@1 | 29 | import javax.annotation.processing.*; |
duke@1 | 30 | import javax.lang.model.element.*; |
duke@1 | 31 | import javax.lang.model.util.*; |
duke@1 | 32 | import java.util.*; |
duke@1 | 33 | |
duke@1 | 34 | /** |
duke@1 | 35 | * Object providing state about a prior round of annotation processing. |
duke@1 | 36 | * |
jjg@310 | 37 | * <p>The methods in this class do not take type annotations into account, |
jjg@310 | 38 | * as target types, not java elements. |
jjg@310 | 39 | * |
jjg@581 | 40 | * <p><b>This is NOT part of any supported API. |
duke@1 | 41 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 42 | * This code and its internal interfaces are subject to change or |
duke@1 | 43 | * deletion without notice.</b> |
duke@1 | 44 | */ |
duke@1 | 45 | public class JavacRoundEnvironment implements RoundEnvironment { |
duke@1 | 46 | // Default equals and hashCode methods are okay. |
duke@1 | 47 | |
duke@1 | 48 | private final boolean processingOver; |
duke@1 | 49 | private final boolean errorRaised; |
duke@1 | 50 | private final ProcessingEnvironment processingEnv; |
duke@1 | 51 | |
duke@1 | 52 | // Caller must pass in an immutable set |
duke@1 | 53 | private final Set<? extends Element> rootElements; |
duke@1 | 54 | |
duke@1 | 55 | JavacRoundEnvironment(boolean processingOver, |
duke@1 | 56 | boolean errorRaised, |
duke@1 | 57 | Set<? extends Element> rootElements, |
duke@1 | 58 | ProcessingEnvironment processingEnv) { |
duke@1 | 59 | this.processingOver = processingOver; |
duke@1 | 60 | this.errorRaised = errorRaised; |
duke@1 | 61 | this.rootElements = rootElements; |
duke@1 | 62 | this.processingEnv = processingEnv; |
duke@1 | 63 | } |
duke@1 | 64 | |
duke@1 | 65 | public String toString() { |
duke@1 | 66 | return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]", |
duke@1 | 67 | errorRaised, |
duke@1 | 68 | rootElements, |
duke@1 | 69 | processingOver); |
duke@1 | 70 | } |
duke@1 | 71 | |
duke@1 | 72 | public boolean processingOver() { |
duke@1 | 73 | return processingOver; |
duke@1 | 74 | } |
duke@1 | 75 | |
duke@1 | 76 | /** |
duke@1 | 77 | * Returns {@code true} if an error was raised in the prior round |
duke@1 | 78 | * of processing; returns {@code false} otherwise. |
duke@1 | 79 | * |
duke@1 | 80 | * @return {@code true} if an error was raised in the prior round |
duke@1 | 81 | * of processing; returns {@code false} otherwise. |
duke@1 | 82 | */ |
duke@1 | 83 | public boolean errorRaised() { |
duke@1 | 84 | return errorRaised; |
duke@1 | 85 | } |
duke@1 | 86 | |
duke@1 | 87 | /** |
duke@1 | 88 | * Returns the type elements specified by the prior round. |
duke@1 | 89 | * |
duke@1 | 90 | * @return the types elements specified by the prior round, or an |
duke@1 | 91 | * empty set if there were none |
duke@1 | 92 | */ |
duke@1 | 93 | public Set<? extends Element> getRootElements() { |
duke@1 | 94 | return rootElements; |
duke@1 | 95 | } |
duke@1 | 96 | |
duke@1 | 97 | private static final String NOT_AN_ANNOTATION_TYPE = |
duke@1 | 98 | "The argument does not represent an annotation type: "; |
duke@1 | 99 | |
duke@1 | 100 | /** |
duke@1 | 101 | * Returns the elements annotated with the given annotation type. |
duke@1 | 102 | * Only type elements <i>included</i> in this round of annotation |
duke@1 | 103 | * processing, or declarations of members, parameters, or type |
duke@1 | 104 | * parameters declared within those, are returned. Included type |
jjg@1358 | 105 | * elements are {@linkplain #getRootElements specified |
duke@1 | 106 | * types} and any types nested within them. |
duke@1 | 107 | * |
duke@1 | 108 | * @param a annotation type being requested |
duke@1 | 109 | * @return the elements annotated with the given annotation type, |
duke@1 | 110 | * or an empty set if there are none |
duke@1 | 111 | */ |
duke@1 | 112 | public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { |
duke@1 | 113 | Set<Element> result = Collections.emptySet(); |
duke@1 | 114 | if (a.getKind() != ElementKind.ANNOTATION_TYPE) |
duke@1 | 115 | throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
duke@1 | 116 | |
jlahoda@2254 | 117 | ElementScanner8<Set<Element>, TypeElement> scanner = |
jlahoda@2254 | 118 | new AnnotationSetScanner(result); |
duke@1 | 119 | |
duke@1 | 120 | for (Element element : rootElements) |
jlahoda@2254 | 121 | result = scanner.scan(element, a); |
duke@1 | 122 | |
duke@1 | 123 | return result; |
duke@1 | 124 | } |
duke@1 | 125 | |
duke@1 | 126 | // Could be written as a local class inside getElementsAnnotatedWith |
duke@1 | 127 | private class AnnotationSetScanner extends |
jlahoda@2254 | 128 | ElementScanner8<Set<Element>, TypeElement> { |
duke@1 | 129 | // Insertion-order preserving set |
duke@1 | 130 | Set<Element> annotatedElements = new LinkedHashSet<Element>(); |
duke@1 | 131 | |
jlahoda@2254 | 132 | AnnotationSetScanner(Set<Element> defaultSet) { |
duke@1 | 133 | super(defaultSet); |
duke@1 | 134 | } |
duke@1 | 135 | |
duke@1 | 136 | @Override |
jlahoda@2254 | 137 | public Set<Element> visitType(TypeElement e, TypeElement p) { |
darcy@1876 | 138 | // Type parameters are not considered to be enclosed by a type |
darcy@1876 | 139 | scan(e.getTypeParameters(), p); |
jfranck@2306 | 140 | return super.visitType(e, p); |
darcy@1876 | 141 | } |
darcy@1876 | 142 | |
darcy@1876 | 143 | @Override |
jlahoda@2254 | 144 | public Set<Element> visitExecutable(ExecutableElement e, TypeElement p) { |
darcy@1876 | 145 | // Type parameters are not considered to be enclosed by an executable |
darcy@1876 | 146 | scan(e.getTypeParameters(), p); |
jfranck@2306 | 147 | return super.visitExecutable(e, p); |
darcy@1876 | 148 | } |
darcy@1876 | 149 | |
darcy@1876 | 150 | @Override |
jlahoda@2254 | 151 | public Set<Element> scan(Element e, TypeElement p) { |
duke@1 | 152 | java.util.List<? extends AnnotationMirror> annotationMirrors = |
duke@1 | 153 | processingEnv.getElementUtils().getAllAnnotationMirrors(e); |
duke@1 | 154 | for (AnnotationMirror annotationMirror : annotationMirrors) { |
jlahoda@2254 | 155 | if (p.equals(annotationMirror.getAnnotationType().asElement())) |
duke@1 | 156 | annotatedElements.add(e); |
duke@1 | 157 | } |
duke@1 | 158 | e.accept(this, p); |
duke@1 | 159 | return annotatedElements; |
duke@1 | 160 | } |
duke@1 | 161 | } |
duke@1 | 162 | |
duke@1 | 163 | /** |
duke@1 | 164 | * {@inheritdoc} |
duke@1 | 165 | */ |
duke@1 | 166 | public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) { |
duke@1 | 167 | if (!a.isAnnotation()) |
duke@1 | 168 | throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
duke@1 | 169 | String name = a.getCanonicalName(); |
duke@1 | 170 | if (name == null) |
duke@1 | 171 | return Collections.emptySet(); |
duke@1 | 172 | else { |
duke@1 | 173 | TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name); |
duke@1 | 174 | if (annotationType == null) |
duke@1 | 175 | return Collections.emptySet(); |
duke@1 | 176 | else |
duke@1 | 177 | return getElementsAnnotatedWith(annotationType); |
duke@1 | 178 | } |
duke@1 | 179 | } |
duke@1 | 180 | } |