src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 104
5e89c4ca637c
child 232
1fbc1cc6e260
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

duke@1 1 /*
xdono@117 2 * Copyright 2005-2008 Sun Microsystems, Inc. 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
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun 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 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any 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 com.sun.tools.javac.tree.JCTree.*;
duke@1 30 import javax.annotation.processing.*;
duke@1 31 import javax.lang.model.element.*;
duke@1 32 import javax.lang.model.type.DeclaredType;
duke@1 33 import javax.lang.model.type.TypeMirror;
duke@1 34 import javax.lang.model.util.*;
duke@1 35 import java.util.*;
duke@1 36
duke@1 37 /**
duke@1 38 * Object providing state about a prior round of annotation processing.
duke@1 39 *
duke@1 40 * <p><b>This is NOT part of any API supported by Sun Microsystems.
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
duke@1 105 * elements are {@linkplain #getSpecifiedTypeElements 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
duke@1 117 DeclaredType annotationTypeElement;
duke@1 118 TypeMirror tm = a.asType();
duke@1 119 if ( tm instanceof DeclaredType )
duke@1 120 annotationTypeElement = (DeclaredType) a.asType();
duke@1 121 else
duke@1 122 throw new AssertionError("Bad implementation type for " + tm);
duke@1 123
duke@1 124 ElementScanner6<Set<Element>, DeclaredType> scanner =
duke@1 125 new AnnotationSetScanner(result);
duke@1 126
duke@1 127 for (Element element : rootElements)
duke@1 128 result = scanner.scan(element, annotationTypeElement);
duke@1 129
duke@1 130 return result;
duke@1 131 }
duke@1 132
duke@1 133 // Could be written as a local class inside getElementsAnnotatedWith
duke@1 134 private class AnnotationSetScanner extends
duke@1 135 ElementScanner6<Set<Element>, DeclaredType> {
duke@1 136 // Insertion-order preserving set
duke@1 137 Set<Element> annotatedElements = new LinkedHashSet<Element>();
duke@1 138
duke@1 139 AnnotationSetScanner(Set<Element> defaultSet) {
duke@1 140 super(defaultSet);
duke@1 141 }
duke@1 142
duke@1 143 @Override
duke@1 144 public Set<Element> scan(Element e, DeclaredType p) {
duke@1 145 java.util.List<? extends AnnotationMirror> annotationMirrors =
duke@1 146 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
duke@1 147 for (AnnotationMirror annotationMirror : annotationMirrors) {
duke@1 148 if (annotationMirror.getAnnotationType().equals(p))
duke@1 149 annotatedElements.add(e);
duke@1 150 }
duke@1 151 e.accept(this, p);
duke@1 152 return annotatedElements;
duke@1 153 }
duke@1 154
duke@1 155 }
duke@1 156
duke@1 157 /**
duke@1 158 * {@inheritdoc}
duke@1 159 */
duke@1 160 public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
duke@1 161 if (!a.isAnnotation())
duke@1 162 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
duke@1 163 String name = a.getCanonicalName();
duke@1 164 if (name == null)
duke@1 165 return Collections.emptySet();
duke@1 166 else {
duke@1 167 TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name);
duke@1 168 if (annotationType == null)
duke@1 169 return Collections.emptySet();
duke@1 170 else
duke@1 171 return getElementsAnnotatedWith(annotationType);
duke@1 172 }
duke@1 173 }
duke@1 174 }

mercurial