Mon, 23 Aug 2010 15:13:33 -0700
6976747: JCDiagnostic: replace "boolean mandatory" with new "Set<JCDiagnostic.Flag>"
Reviewed-by: mcimadamore
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2005, 2009, Oracle and/or its affiliates. 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 |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle 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 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * 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 | * |
jjg@310 | 40 | * <p>The methods in this class do not take type annotations into account, |
jjg@310 | 41 | * as target types, not java elements. |
jjg@310 | 42 | * |
jjg@581 | 43 | * <p><b>This is NOT part of any supported API. |
duke@1 | 44 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 45 | * This code and its internal interfaces are subject to change or |
duke@1 | 46 | * deletion without notice.</b> |
duke@1 | 47 | */ |
duke@1 | 48 | public class JavacRoundEnvironment implements RoundEnvironment { |
duke@1 | 49 | // Default equals and hashCode methods are okay. |
duke@1 | 50 | |
duke@1 | 51 | private final boolean processingOver; |
duke@1 | 52 | private final boolean errorRaised; |
duke@1 | 53 | private final ProcessingEnvironment processingEnv; |
duke@1 | 54 | |
duke@1 | 55 | // Caller must pass in an immutable set |
duke@1 | 56 | private final Set<? extends Element> rootElements; |
duke@1 | 57 | |
duke@1 | 58 | JavacRoundEnvironment(boolean processingOver, |
duke@1 | 59 | boolean errorRaised, |
duke@1 | 60 | Set<? extends Element> rootElements, |
duke@1 | 61 | ProcessingEnvironment processingEnv) { |
duke@1 | 62 | this.processingOver = processingOver; |
duke@1 | 63 | this.errorRaised = errorRaised; |
duke@1 | 64 | this.rootElements = rootElements; |
duke@1 | 65 | this.processingEnv = processingEnv; |
duke@1 | 66 | } |
duke@1 | 67 | |
duke@1 | 68 | public String toString() { |
duke@1 | 69 | return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]", |
duke@1 | 70 | errorRaised, |
duke@1 | 71 | rootElements, |
duke@1 | 72 | processingOver); |
duke@1 | 73 | } |
duke@1 | 74 | |
duke@1 | 75 | public boolean processingOver() { |
duke@1 | 76 | return processingOver; |
duke@1 | 77 | } |
duke@1 | 78 | |
duke@1 | 79 | /** |
duke@1 | 80 | * Returns {@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 | * @return {@code true} if an error was raised in the prior round |
duke@1 | 84 | * of processing; returns {@code false} otherwise. |
duke@1 | 85 | */ |
duke@1 | 86 | public boolean errorRaised() { |
duke@1 | 87 | return errorRaised; |
duke@1 | 88 | } |
duke@1 | 89 | |
duke@1 | 90 | /** |
duke@1 | 91 | * Returns the type elements specified by the prior round. |
duke@1 | 92 | * |
duke@1 | 93 | * @return the types elements specified by the prior round, or an |
duke@1 | 94 | * empty set if there were none |
duke@1 | 95 | */ |
duke@1 | 96 | public Set<? extends Element> getRootElements() { |
duke@1 | 97 | return rootElements; |
duke@1 | 98 | } |
duke@1 | 99 | |
duke@1 | 100 | private static final String NOT_AN_ANNOTATION_TYPE = |
duke@1 | 101 | "The argument does not represent an annotation type: "; |
duke@1 | 102 | |
duke@1 | 103 | /** |
duke@1 | 104 | * Returns the elements annotated with the given annotation type. |
duke@1 | 105 | * Only type elements <i>included</i> in this round of annotation |
duke@1 | 106 | * processing, or declarations of members, parameters, or type |
duke@1 | 107 | * parameters declared within those, are returned. Included type |
duke@1 | 108 | * elements are {@linkplain #getSpecifiedTypeElements specified |
duke@1 | 109 | * types} and any types nested within them. |
duke@1 | 110 | * |
duke@1 | 111 | * @param a annotation type being requested |
duke@1 | 112 | * @return the elements annotated with the given annotation type, |
duke@1 | 113 | * or an empty set if there are none |
duke@1 | 114 | */ |
duke@1 | 115 | public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { |
duke@1 | 116 | Set<Element> result = Collections.emptySet(); |
darcy@232 | 117 | Types typeUtil = processingEnv.getTypeUtils(); |
duke@1 | 118 | if (a.getKind() != ElementKind.ANNOTATION_TYPE) |
duke@1 | 119 | throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
duke@1 | 120 | |
duke@1 | 121 | DeclaredType annotationTypeElement; |
duke@1 | 122 | TypeMirror tm = a.asType(); |
duke@1 | 123 | if ( tm instanceof DeclaredType ) |
duke@1 | 124 | annotationTypeElement = (DeclaredType) a.asType(); |
duke@1 | 125 | else |
duke@1 | 126 | throw new AssertionError("Bad implementation type for " + tm); |
duke@1 | 127 | |
darcy@575 | 128 | ElementScanner7<Set<Element>, DeclaredType> scanner = |
darcy@232 | 129 | new AnnotationSetScanner(result, typeUtil); |
duke@1 | 130 | |
duke@1 | 131 | for (Element element : rootElements) |
duke@1 | 132 | result = scanner.scan(element, annotationTypeElement); |
duke@1 | 133 | |
duke@1 | 134 | return result; |
duke@1 | 135 | } |
duke@1 | 136 | |
duke@1 | 137 | // Could be written as a local class inside getElementsAnnotatedWith |
duke@1 | 138 | private class AnnotationSetScanner extends |
darcy@575 | 139 | ElementScanner7<Set<Element>, DeclaredType> { |
duke@1 | 140 | // Insertion-order preserving set |
duke@1 | 141 | Set<Element> annotatedElements = new LinkedHashSet<Element>(); |
darcy@232 | 142 | Types typeUtil; |
duke@1 | 143 | |
darcy@232 | 144 | AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) { |
duke@1 | 145 | super(defaultSet); |
darcy@232 | 146 | this.typeUtil = typeUtil; |
duke@1 | 147 | } |
duke@1 | 148 | |
duke@1 | 149 | @Override |
duke@1 | 150 | public Set<Element> scan(Element e, DeclaredType p) { |
duke@1 | 151 | java.util.List<? extends AnnotationMirror> annotationMirrors = |
duke@1 | 152 | processingEnv.getElementUtils().getAllAnnotationMirrors(e); |
duke@1 | 153 | for (AnnotationMirror annotationMirror : annotationMirrors) { |
darcy@232 | 154 | if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p)) |
duke@1 | 155 | annotatedElements.add(e); |
duke@1 | 156 | } |
duke@1 | 157 | e.accept(this, p); |
duke@1 | 158 | return annotatedElements; |
duke@1 | 159 | } |
duke@1 | 160 | |
duke@1 | 161 | } |
duke@1 | 162 | |
duke@1 | 163 | /** |
duke@1 | 164 | * {@inheritdoc} |
duke@1 | 165 | */ |
duke@1 | 166 | public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) { |
duke@1 | 167 | if (!a.isAnnotation()) |
duke@1 | 168 | throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
duke@1 | 169 | String name = a.getCanonicalName(); |
duke@1 | 170 | if (name == null) |
duke@1 | 171 | return Collections.emptySet(); |
duke@1 | 172 | else { |
duke@1 | 173 | TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name); |
duke@1 | 174 | if (annotationType == null) |
duke@1 | 175 | return Collections.emptySet(); |
duke@1 | 176 | else |
duke@1 | 177 | return getElementsAnnotatedWith(annotationType); |
duke@1 | 178 | } |
duke@1 | 179 | } |
duke@1 | 180 | } |