duke@1: /* duke@1: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.processing; duke@1: duke@1: import java.lang.annotation.Annotation; duke@1: import com.sun.tools.javac.util.*; duke@1: import com.sun.tools.javac.comp.*; duke@1: import com.sun.tools.javac.tree.JCTree.*; duke@1: import javax.annotation.processing.*; duke@1: import javax.lang.model.element.*; duke@1: import javax.lang.model.type.DeclaredType; duke@1: import javax.lang.model.type.TypeMirror; duke@1: import javax.lang.model.util.*; duke@1: import java.util.*; duke@1: duke@1: /** duke@1: * Object providing state about a prior round of annotation processing. duke@1: * duke@1: *

This is NOT part of any API supported by Sun Microsystems. duke@1: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class JavacRoundEnvironment implements RoundEnvironment { duke@1: // Default equals and hashCode methods are okay. duke@1: duke@1: private final boolean processingOver; duke@1: private final boolean errorRaised; duke@1: private final ProcessingEnvironment processingEnv; duke@1: duke@1: // Caller must pass in an immutable set duke@1: private final Set rootElements; duke@1: duke@1: JavacRoundEnvironment(boolean processingOver, duke@1: boolean errorRaised, duke@1: Set rootElements, duke@1: ProcessingEnvironment processingEnv) { duke@1: this.processingOver = processingOver; duke@1: this.errorRaised = errorRaised; duke@1: this.rootElements = rootElements; duke@1: this.processingEnv = processingEnv; duke@1: } duke@1: duke@1: public String toString() { duke@1: return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]", duke@1: errorRaised, duke@1: rootElements, duke@1: processingOver); duke@1: } duke@1: duke@1: public boolean processingOver() { duke@1: return processingOver; duke@1: } duke@1: duke@1: /** duke@1: * Returns {@code true} if an error was raised in the prior round duke@1: * of processing; returns {@code false} otherwise. duke@1: * duke@1: * @return {@code true} if an error was raised in the prior round duke@1: * of processing; returns {@code false} otherwise. duke@1: */ duke@1: public boolean errorRaised() { duke@1: return errorRaised; duke@1: } duke@1: duke@1: /** duke@1: * Returns the type elements specified by the prior round. duke@1: * duke@1: * @return the types elements specified by the prior round, or an duke@1: * empty set if there were none duke@1: */ duke@1: public Set getRootElements() { duke@1: return rootElements; duke@1: } duke@1: duke@1: private static final String NOT_AN_ANNOTATION_TYPE = duke@1: "The argument does not represent an annotation type: "; duke@1: duke@1: /** duke@1: * Returns the elements annotated with the given annotation type. duke@1: * Only type elements included in this round of annotation duke@1: * processing, or declarations of members, parameters, or type duke@1: * parameters declared within those, are returned. Included type duke@1: * elements are {@linkplain #getSpecifiedTypeElements specified duke@1: * types} and any types nested within them. duke@1: * duke@1: * @param a annotation type being requested duke@1: * @return the elements annotated with the given annotation type, duke@1: * or an empty set if there are none duke@1: */ duke@1: public Set getElementsAnnotatedWith(TypeElement a) { duke@1: Set result = Collections.emptySet(); duke@1: if (a.getKind() != ElementKind.ANNOTATION_TYPE) duke@1: throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); duke@1: duke@1: DeclaredType annotationTypeElement; duke@1: TypeMirror tm = a.asType(); duke@1: if ( tm instanceof DeclaredType ) duke@1: annotationTypeElement = (DeclaredType) a.asType(); duke@1: else duke@1: throw new AssertionError("Bad implementation type for " + tm); duke@1: duke@1: ElementScanner6, DeclaredType> scanner = duke@1: new AnnotationSetScanner(result); duke@1: duke@1: for (Element element : rootElements) duke@1: result = scanner.scan(element, annotationTypeElement); duke@1: duke@1: return result; duke@1: } duke@1: duke@1: // Could be written as a local class inside getElementsAnnotatedWith duke@1: private class AnnotationSetScanner extends duke@1: ElementScanner6, DeclaredType> { duke@1: // Insertion-order preserving set duke@1: Set annotatedElements = new LinkedHashSet(); duke@1: duke@1: AnnotationSetScanner(Set defaultSet) { duke@1: super(defaultSet); duke@1: } duke@1: duke@1: @Override duke@1: public Set scan(Element e, DeclaredType p) { duke@1: java.util.List annotationMirrors = duke@1: processingEnv.getElementUtils().getAllAnnotationMirrors(e); duke@1: for (AnnotationMirror annotationMirror : annotationMirrors) { duke@1: if (annotationMirror.getAnnotationType().equals(p)) duke@1: annotatedElements.add(e); duke@1: } duke@1: e.accept(this, p); duke@1: return annotatedElements; duke@1: } duke@1: duke@1: } duke@1: duke@1: /** duke@1: * {@inheritdoc} duke@1: */ duke@1: public Set getElementsAnnotatedWith(Class a) { duke@1: if (!a.isAnnotation()) duke@1: throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); duke@1: String name = a.getCanonicalName(); duke@1: if (name == null) duke@1: return Collections.emptySet(); duke@1: else { duke@1: TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name); duke@1: if (annotationType == null) duke@1: return Collections.emptySet(); duke@1: else duke@1: return getElementsAnnotatedWith(annotationType); duke@1: } duke@1: } duke@1: }