001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.processing;
027:
028: import java.lang.annotation.Annotation;
029: import com.sun.tools.javac.util.*;
030: import com.sun.tools.javac.comp.*;
031: import com.sun.tools.javac.tree.JCTree.*;
032: import javax.annotation.processing.*;
033: import javax.lang.model.element.*;
034: import javax.lang.model.type.DeclaredType;
035: import javax.lang.model.type.TypeMirror;
036: import javax.lang.model.util.*;
037: import java.util.*;
038:
039: /**
040: * Object providing state about a prior round of annotation processing.
041: *
042: * <p><b>This is NOT part of any API supported by Sun Microsystems.
043: * If you write code that depends on this, you do so at your own risk.
044: * This code and its internal interfaces are subject to change or
045: * deletion without notice.</b>
046: */
047: @Version("@(#)JavacRoundEnvironment.java 1.13 07/05/05")
048: public class JavacRoundEnvironment implements RoundEnvironment {
049: // Default equals and hashCode methods are okay.
050:
051: private final boolean processingOver;
052: private final boolean errorRaised;
053: private final ProcessingEnvironment processingEnv;
054:
055: // Caller must pass in an immutable set
056: private final Set<? extends Element> rootElements;
057:
058: JavacRoundEnvironment(boolean processingOver, boolean errorRaised,
059: Set<? extends Element> rootElements,
060: ProcessingEnvironment processingEnv) {
061: this .processingOver = processingOver;
062: this .errorRaised = errorRaised;
063: this .rootElements = rootElements;
064: this .processingEnv = processingEnv;
065: }
066:
067: public String toString() {
068: return String.format(
069: "[errorRaised=%b, rootElements=%s, processingOver=%b]",
070: errorRaised, rootElements, processingOver);
071: }
072:
073: public boolean processingOver() {
074: return processingOver;
075: }
076:
077: /**
078: * Returns {@code true} if an error was raised in the prior round
079: * of processing; returns {@code false} otherwise.
080: *
081: * @return {@code true} if an error was raised in the prior round
082: * of processing; returns {@code false} otherwise.
083: */
084: public boolean errorRaised() {
085: return errorRaised;
086: }
087:
088: /**
089: * Returns the type elements specified by the prior round.
090: *
091: * @return the types elements specified by the prior round, or an
092: * empty set if there were none
093: */
094: public Set<? extends Element> getRootElements() {
095: return rootElements;
096: }
097:
098: private static final String NOT_AN_ANNOTATION_TYPE = "The argument does not represent an annotation type: ";
099:
100: /**
101: * Returns the elements annotated with the given annotation type.
102: * Only type elements <i>included</i> in this round of annotation
103: * processing, or declarations of members, parameters, or type
104: * parameters declared within those, are returned. Included type
105: * elements are {@linkplain #getSpecifiedTypeElements specified
106: * types} and any types nested within them.
107: *
108: * @param a annotation type being requested
109: * @return the elements annotated with the given annotation type,
110: * or an empty set if there are none
111: */
112: public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
113: Set<Element> result = Collections.emptySet();
114: if (a.getKind() != ElementKind.ANNOTATION_TYPE)
115: throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE
116: + a);
117:
118: DeclaredType annotationTypeElement;
119: TypeMirror tm = a.asType();
120: if (tm instanceof DeclaredType)
121: annotationTypeElement = (DeclaredType) a.asType();
122: else
123: throw new AssertionError("Bad implementation type for "
124: + tm);
125:
126: ElementScanner6<Set<Element>, DeclaredType> scanner = new AnnotationSetScanner(
127: result);
128:
129: for (Element element : rootElements)
130: result = scanner.scan(element, annotationTypeElement);
131:
132: return result;
133: }
134:
135: // Could be written as a local class inside getElementsAnnotatedWith
136: private class AnnotationSetScanner extends
137: ElementScanner6<Set<Element>, DeclaredType> {
138: // Insertion-order preserving set
139: Set<Element> annotatedElements = new LinkedHashSet<Element>();
140:
141: AnnotationSetScanner(Set<Element> defaultSet) {
142: super (defaultSet);
143: }
144:
145: @Override
146: public Set<Element> scan(Element e, DeclaredType p) {
147: java.util.List<? extends AnnotationMirror> annotationMirrors = processingEnv
148: .getElementUtils().getAllAnnotationMirrors(e);
149: for (AnnotationMirror annotationMirror : annotationMirrors) {
150: if (annotationMirror.getAnnotationType().equals(p))
151: annotatedElements.add(e);
152: }
153: e.accept(this , p);
154: return annotatedElements;
155: }
156:
157: }
158:
159: /**
160: * {@inheritdoc}
161: */
162: public Set<? extends Element> getElementsAnnotatedWith(
163: Class<? extends Annotation> a) {
164: if (!a.isAnnotation())
165: throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE
166: + a);
167: String name = a.getCanonicalName();
168: if (name == null)
169: return Collections.emptySet();
170: else {
171: TypeElement annotationType = processingEnv
172: .getElementUtils().getTypeElement(name);
173: if (annotationType == null)
174: return Collections.emptySet();
175: else
176: return getElementsAnnotatedWith(annotationType);
177: }
178: }
179: }
|