1 /* 2 * Copyright (c) 2005, 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 23 * questions. 24 */ 25 26 package com.sun.tools.javac.processing; 27 28 import java.lang.annotation.Annotation; 29 import javax.annotation.processing.*; 30 import javax.lang.model.element.*; 31 import javax.lang.model.util.*; 32 import java.util.*; 33 34 import com.sun.tools.javac.util.DefinedBy; 35 import com.sun.tools.javac.util.DefinedBy.Api; 36 37 /** 38 * Object providing state about a prior round of annotation processing. 39 * 40 * <p>The methods in this class do not take type annotations into account, 41 * as target types, not java elements. 42 * 43 * <p><b>This is NOT part of any supported API. 44 * If you write code that depends on this, you do so at your own risk. 45 * This code and its internal interfaces are subject to change or 46 * deletion without notice.</b> 47 */ 48 public class JavacRoundEnvironment implements RoundEnvironment { 49 // Default equals and hashCode methods are okay. 50 51 private final boolean processingOver; 52 private final boolean errorRaised; 53 private final ProcessingEnvironment processingEnv; 54 private final Elements eltUtils; 55 56 // Caller must pass in an immutable set 57 private final Set<? extends Element> rootElements; 58 59 JavacRoundEnvironment(boolean processingOver, 60 boolean errorRaised, 61 Set<? extends Element> rootElements, 62 ProcessingEnvironment processingEnv) { 63 this.processingOver = processingOver; 64 this.errorRaised = errorRaised; 65 this.rootElements = rootElements; 66 this.processingEnv = processingEnv; 67 this.eltUtils = processingEnv.getElementUtils(); 68 } 69 70 public String toString() { 71 return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]", 72 errorRaised, 73 rootElements, 74 processingOver); 75 } 76 77 @DefinedBy(Api.ANNOTATION_PROCESSING) 78 public boolean processingOver() { 79 return processingOver; 80 } 81 82 /** 83 * Returns {@code true} if an error was raised in the prior round 84 * of processing; returns {@code false} otherwise. 85 * 86 * @return {@code true} if an error was raised in the prior round 87 * of processing; returns {@code false} otherwise. 88 */ 89 @DefinedBy(Api.ANNOTATION_PROCESSING) 90 public boolean errorRaised() { 91 return errorRaised; 92 } 93 94 /** 95 * Returns the type elements specified by the prior round. 96 * 97 * @return the types elements specified by the prior round, or an 98 * empty set if there were none 99 */ 100 @DefinedBy(Api.ANNOTATION_PROCESSING) 101 public Set<? extends Element> getRootElements() { 102 return rootElements; 103 } 104 105 private static final String NOT_AN_ANNOTATION_TYPE = 106 "The argument does not represent an annotation type: "; 107 108 /** 109 * Returns the elements annotated with the given annotation type. 110 * Only type elements <i>included</i> in this round of annotation 111 * processing, or declarations of members, parameters, or type 112 * parameters declared within those, are returned. Included type 113 * elements are {@linkplain #getRootElements specified 114 * types} and any types nested within them. 115 * 116 * @param a annotation type being requested 117 * @return the elements annotated with the given annotation type, 118 * or an empty set if there are none 119 */ 120 @DefinedBy(Api.ANNOTATION_PROCESSING) 121 public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { 122 throwIfNotAnnotation(a); 123 124 Set<Element> result = Collections.emptySet(); 125 ElementScanner9<Set<Element>, TypeElement> scanner = 126 new AnnotationSetScanner(result); 127 128 for (Element element : rootElements) 129 result = scanner.scan(element, a); 130 131 return result; 132 } 133 134 @DefinedBy(Api.ANNOTATION_PROCESSING) 135 public Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... annotations) { 136 Set<TypeElement> annotationSet = new LinkedHashSet<>(annotations.length); 137 for (TypeElement annotation : annotations) { 138 throwIfNotAnnotation(annotation); 139 annotationSet.add(annotation); 140 } 141 142 Set<Element> result = Collections.emptySet(); 143 ElementScanner9<Set<Element>, Set<TypeElement>> scanner = 144 new AnnotationSetMultiScanner(result); 145 146 for (Element element : rootElements) 147 result = scanner.scan(element, annotationSet); 148 149 return result; 150 } 151 152 private static abstract class ElementScanningIncludingTypeParameters<R, P> 153 extends ElementScanner9<R, P> { 154 155 protected ElementScanningIncludingTypeParameters(R defaultValue) { 156 super(defaultValue); 157 } 158 159 @Override @DefinedBy(Api.LANGUAGE_MODEL) 160 public R visitType(TypeElement e, P p) { 161 // Type parameters are not considered to be enclosed by a type 162 scan(e.getTypeParameters(), p); 163 return super.visitType(e, p); 164 } 165 166 @Override @DefinedBy(Api.LANGUAGE_MODEL) 167 public R visitExecutable(ExecutableElement e, P p) { 168 // Type parameters are not considered to be enclosed by an executable 169 scan(e.getTypeParameters(), p); 170 return super.visitExecutable(e, p); 171 } 172 } 173 174 // Could be written as a local class inside getElementsAnnotatedWith 175 private class AnnotationSetScanner extends 176 ElementScanningIncludingTypeParameters<Set<Element>, TypeElement> { 177 // Insertion-order preserving set 178 Set<Element> annotatedElements = new LinkedHashSet<>(); 179 180 AnnotationSetScanner(Set<Element> defaultSet) { 181 super(defaultSet); 182 } 183 184 @Override @DefinedBy(Api.LANGUAGE_MODEL) 185 public Set<Element> scan(Element e, TypeElement p) { 186 java.util.List<? extends AnnotationMirror> annotationMirrors = 187 eltUtils.getAllAnnotationMirrors(e); 188 for (AnnotationMirror annotationMirror : annotationMirrors) { 189 if (p.equals(annotationMirror.getAnnotationType().asElement())) 190 annotatedElements.add(e); 191 } 192 e.accept(this, p); 193 return annotatedElements; 194 } 195 } 196 197 private class AnnotationSetMultiScanner extends 198 ElementScanningIncludingTypeParameters<Set<Element>, Set<TypeElement>> { 199 // Insertion-order preserving set 200 Set<Element> annotatedElements = new LinkedHashSet<>(); 201 202 AnnotationSetMultiScanner(Set<Element> defaultSet) { 203 super(defaultSet); 204 } 205 206 @Override @DefinedBy(Api.LANGUAGE_MODEL) 207 public Set<Element> scan(Element e, Set<TypeElement> p) { 208 java.util.List<? extends AnnotationMirror> annotationMirrors = 209 eltUtils.getAllAnnotationMirrors(e); 210 for (AnnotationMirror annotationMirror : annotationMirrors) { 211 if (p.contains(annotationMirror.getAnnotationType().asElement())) 212 annotatedElements.add(e); 213 } 214 e.accept(this, p); 215 return annotatedElements; 216 } 217 } 218 219 /** 220 * {@inheritDoc} 221 */ 222 @DefinedBy(Api.ANNOTATION_PROCESSING) 223 public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) { 224 throwIfNotAnnotation(a); 225 String name = a.getCanonicalName(); 226 if (name == null) 227 return Collections.emptySet(); 228 else { 229 TypeElement annotationType = eltUtils.getTypeElement(name); 230 if (annotationType == null) 231 return Collections.emptySet(); 232 else 233 return getElementsAnnotatedWith(annotationType); 234 } 235 } 236 237 @DefinedBy(Api.ANNOTATION_PROCESSING) 238 public Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> annotations) { 239 List<TypeElement> annotationsAsElements = new ArrayList<>(annotations.size()); 240 241 for (Class<? extends Annotation> annotation : annotations) { 242 throwIfNotAnnotation(annotation); 243 String name = annotation.getCanonicalName(); 244 if (name == null) 245 continue; 246 annotationsAsElements.add(eltUtils.getTypeElement(name)); 247 } 248 249 return getElementsAnnotatedWithAny(annotationsAsElements.toArray(new TypeElement[0])); 250 } 251 252 private void throwIfNotAnnotation(Class<? extends Annotation> a) { 253 if (!a.isAnnotation()) 254 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); 255 } 256 257 private void throwIfNotAnnotation(TypeElement a) { 258 if (a.getKind() != ElementKind.ANNOTATION_TYPE) 259 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); 260 } 261 }