duke@1: /* jlahoda@2254: * Copyright (c) 2005, 2014, Oracle and/or its affiliates. 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 ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle 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: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * 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 javax.annotation.processing.*; duke@1: import javax.lang.model.element.*; 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: * jjg@310: *

The methods in this class do not take type annotations into account, jjg@310: * as target types, not java elements. jjg@310: * jjg@581: *

This is NOT part of any supported API. 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 jjg@1358: * elements are {@linkplain #getRootElements 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: jlahoda@2254: ElementScanner8, TypeElement> scanner = jlahoda@2254: new AnnotationSetScanner(result); duke@1: duke@1: for (Element element : rootElements) jlahoda@2254: result = scanner.scan(element, a); 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 jlahoda@2254: ElementScanner8, TypeElement> { duke@1: // Insertion-order preserving set duke@1: Set annotatedElements = new LinkedHashSet(); duke@1: jlahoda@2254: AnnotationSetScanner(Set defaultSet) { duke@1: super(defaultSet); duke@1: } duke@1: duke@1: @Override jlahoda@2254: public Set visitType(TypeElement e, TypeElement p) { darcy@1876: // Type parameters are not considered to be enclosed by a type darcy@1876: scan(e.getTypeParameters(), p); jfranck@2306: return super.visitType(e, p); darcy@1876: } darcy@1876: darcy@1876: @Override jlahoda@2254: public Set visitExecutable(ExecutableElement e, TypeElement p) { darcy@1876: // Type parameters are not considered to be enclosed by an executable darcy@1876: scan(e.getTypeParameters(), p); jfranck@2306: return super.visitExecutable(e, p); darcy@1876: } darcy@1876: darcy@1876: @Override jlahoda@2254: public Set scan(Element e, TypeElement p) { duke@1: java.util.List annotationMirrors = duke@1: processingEnv.getElementUtils().getAllAnnotationMirrors(e); duke@1: for (AnnotationMirror annotationMirror : annotationMirrors) { jlahoda@2254: if (p.equals(annotationMirror.getAnnotationType().asElement())) 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: * {@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: }