< prev index next >

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

Print this page




  34 import com.sun.tools.javac.util.DefinedBy;
  35 import com.sun.tools.javac.util.DefinedBy.Api;
  36 
  37 /**
  38  * Object providing state about a prior round of annotation processing.
  39  *
  40  * <p>The methods in this class do not take type annotations into account,
  41  * as target types, not java elements.
  42  *
  43  * <p><b>This is NOT part of any supported API.
  44  * If you write code that depends on this, you do so at your own risk.
  45  * This code and its internal interfaces are subject to change or
  46  * deletion without notice.</b>
  47  */
  48 public class JavacRoundEnvironment implements RoundEnvironment {
  49     // Default equals and hashCode methods are okay.
  50 
  51     private final boolean processingOver;
  52     private final boolean errorRaised;
  53     private final ProcessingEnvironment processingEnv;

  54 
  55     // Caller must pass in an immutable set
  56     private final Set<? extends Element> rootElements;
  57 
  58     JavacRoundEnvironment(boolean processingOver,
  59                           boolean errorRaised,
  60                           Set<? extends Element> rootElements,
  61                           ProcessingEnvironment processingEnv) {
  62         this.processingOver = processingOver;
  63         this.errorRaised = errorRaised;
  64         this.rootElements = rootElements;
  65         this.processingEnv = processingEnv;

  66     }
  67 
  68     public String toString() {
  69         return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]",
  70                              errorRaised,
  71                              rootElements,
  72                              processingOver);
  73     }
  74 
  75     @DefinedBy(Api.ANNOTATION_PROCESSING)
  76     public boolean processingOver() {
  77         return processingOver;
  78     }
  79 
  80     /**
  81      * Returns {@code true} if an error was raised in the prior round
  82      * of processing; returns {@code false} otherwise.
  83      *
  84      * @return {@code true} if an error was raised in the prior round
  85      * of processing; returns {@code false} otherwise.


 100         return rootElements;
 101     }
 102 
 103     private static final String NOT_AN_ANNOTATION_TYPE =
 104         "The argument does not represent an annotation type: ";
 105 
 106     /**
 107      * Returns the elements annotated with the given annotation type.
 108      * Only type elements <i>included</i> in this round of annotation
 109      * processing, or declarations of members, parameters, or type
 110      * parameters declared within those, are returned.  Included type
 111      * elements are {@linkplain #getRootElements specified
 112      * types} and any types nested within them.
 113      *
 114      * @param a  annotation type being requested
 115      * @return the elements annotated with the given annotation type,
 116      * or an empty set if there are none
 117      */
 118     @DefinedBy(Api.ANNOTATION_PROCESSING)
 119     public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
 120         Set<Element> result = Collections.emptySet();
 121         if (a.getKind() != ElementKind.ANNOTATION_TYPE)
 122             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 123 

 124         ElementScanner9<Set<Element>, TypeElement> scanner =
 125             new AnnotationSetScanner(result);
 126 
 127         for (Element element : rootElements)
 128             result = scanner.scan(element, a);
 129 
 130         return result;
 131     }
 132 
 133     // Could be written as a local class inside getElementsAnnotatedWith
 134     private class AnnotationSetScanner extends
 135         ElementScanner9<Set<Element>, TypeElement> {
 136         // Insertion-order preserving set
 137         Set<Element> annotatedElements = new LinkedHashSet<>();


 138 
 139         AnnotationSetScanner(Set<Element> defaultSet) {
 140             super(defaultSet);













 141         }
 142 
 143         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 144         public Set<Element> visitType(TypeElement e, TypeElement p) {
 145             // Type parameters are not considered to be enclosed by a type
 146             scan(e.getTypeParameters(), p);
 147             return super.visitType(e, p);
 148         }
 149 
 150         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 151         public Set<Element> visitExecutable(ExecutableElement e, TypeElement p) {
 152             // Type parameters are not considered to be enclosed by an executable
 153             scan(e.getTypeParameters(), p);
 154             return super.visitExecutable(e, p);
 155         }











 156 
 157         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 158         public Set<Element> scan(Element e, TypeElement p) {
 159             java.util.List<? extends AnnotationMirror> annotationMirrors =
 160                 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
 161             for (AnnotationMirror annotationMirror : annotationMirrors) {
 162                 if (p.equals(annotationMirror.getAnnotationType().asElement()))
 163                     annotatedElements.add(e);
 164             }
 165             e.accept(this, p);
 166             return annotatedElements;
 167         }
 168     }
 169 






















 170     /**
 171      * {@inheritDoc}
 172      */
 173     @DefinedBy(Api.ANNOTATION_PROCESSING)
 174     public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
 175         if (!a.isAnnotation())
 176             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 177         String name = a.getCanonicalName();
 178         if (name == null)
 179             return Collections.emptySet();
 180         else {
 181             TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name);
 182             if (annotationType == null)
 183                 return Collections.emptySet();
 184             else
 185                 return getElementsAnnotatedWith(annotationType);
 186         }

























 187     }
 188 }


  34 import com.sun.tools.javac.util.DefinedBy;
  35 import com.sun.tools.javac.util.DefinedBy.Api;
  36 
  37 /**
  38  * Object providing state about a prior round of annotation processing.
  39  *
  40  * <p>The methods in this class do not take type annotations into account,
  41  * as target types, not java elements.
  42  *
  43  * <p><b>This is NOT part of any supported API.
  44  * If you write code that depends on this, you do so at your own risk.
  45  * This code and its internal interfaces are subject to change or
  46  * deletion without notice.</b>
  47  */
  48 public class JavacRoundEnvironment implements RoundEnvironment {
  49     // Default equals and hashCode methods are okay.
  50 
  51     private final boolean processingOver;
  52     private final boolean errorRaised;
  53     private final ProcessingEnvironment processingEnv;
  54     private final Elements eltUtils;
  55 
  56     // Caller must pass in an immutable set
  57     private final Set<? extends Element> rootElements;
  58 
  59     JavacRoundEnvironment(boolean processingOver,
  60                           boolean errorRaised,
  61                           Set<? extends Element> rootElements,
  62                           ProcessingEnvironment processingEnv) {
  63         this.processingOver = processingOver;
  64         this.errorRaised = errorRaised;
  65         this.rootElements = rootElements;
  66         this.processingEnv = processingEnv;
  67         this.eltUtils = processingEnv.getElementUtils();
  68     }
  69 
  70     public String toString() {
  71         return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]",
  72                              errorRaised,
  73                              rootElements,
  74                              processingOver);
  75     }
  76 
  77     @DefinedBy(Api.ANNOTATION_PROCESSING)
  78     public boolean processingOver() {
  79         return processingOver;
  80     }
  81 
  82     /**
  83      * Returns {@code true} if an error was raised in the prior round
  84      * of processing; returns {@code false} otherwise.
  85      *
  86      * @return {@code true} if an error was raised in the prior round
  87      * of processing; returns {@code false} otherwise.


 102         return rootElements;
 103     }
 104 
 105     private static final String NOT_AN_ANNOTATION_TYPE =
 106         "The argument does not represent an annotation type: ";
 107 
 108     /**
 109      * Returns the elements annotated with the given annotation type.
 110      * Only type elements <i>included</i> in this round of annotation
 111      * processing, or declarations of members, parameters, or type
 112      * parameters declared within those, are returned.  Included type
 113      * elements are {@linkplain #getRootElements specified
 114      * types} and any types nested within them.
 115      *
 116      * @param a  annotation type being requested
 117      * @return the elements annotated with the given annotation type,
 118      * or an empty set if there are none
 119      */
 120     @DefinedBy(Api.ANNOTATION_PROCESSING)
 121     public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
 122         throwIfNotAnnotation(a);


 123 
 124         Set<Element> result = Collections.emptySet();
 125         ElementScanner9<Set<Element>, TypeElement> scanner =
 126             new AnnotationSetScanner(result);
 127 
 128         for (Element element : rootElements)
 129             result = scanner.scan(element, a);
 130 
 131         return result;
 132     }
 133 
 134     @DefinedBy(Api.ANNOTATION_PROCESSING)
 135     public Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... annotations) {
 136         Set<TypeElement> annotationSet = new LinkedHashSet<>(annotations.length);
 137         for (TypeElement annotation : annotations) {
 138             throwIfNotAnnotation(annotation);
 139             annotationSet.add(annotation);
 140         }
 141 
 142         Set<Element> result = Collections.emptySet();
 143         ElementScanner9<Set<Element>, Set<TypeElement>> scanner =
 144             new AnnotationSetMultiScanner(result);
 145 
 146         for (Element element : rootElements)
 147             result = scanner.scan(element, annotationSet);
 148 
 149         return result;
 150     }
 151 
 152     private static abstract class ElementScanningIncludingTypeParameters<R, P>
 153         extends ElementScanner9<R, P> {
 154 
 155         protected ElementScanningIncludingTypeParameters(R defaultValue) {
 156             super(defaultValue);
 157         }
 158 
 159         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 160         public R visitType(TypeElement e, P p) {
 161             // Type parameters are not considered to be enclosed by a type
 162             scan(e.getTypeParameters(), p);
 163             return super.visitType(e, p);
 164         }
 165 
 166         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 167         public R visitExecutable(ExecutableElement e, P p) {
 168             // Type parameters are not considered to be enclosed by an executable
 169             scan(e.getTypeParameters(), p);
 170             return super.visitExecutable(e, p);
 171         }
 172     }
 173 
 174     // Could be written as a local class inside getElementsAnnotatedWith
 175     private class AnnotationSetScanner extends
 176         ElementScanningIncludingTypeParameters<Set<Element>, TypeElement> {
 177         // Insertion-order preserving set
 178         Set<Element> annotatedElements = new LinkedHashSet<>();
 179 
 180         AnnotationSetScanner(Set<Element> defaultSet) {
 181             super(defaultSet);
 182         }
 183 
 184         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 185         public Set<Element> scan(Element e, TypeElement p) {
 186             java.util.List<? extends AnnotationMirror> annotationMirrors =
 187                 eltUtils.getAllAnnotationMirrors(e);
 188             for (AnnotationMirror annotationMirror : annotationMirrors) {
 189                 if (p.equals(annotationMirror.getAnnotationType().asElement()))
 190                     annotatedElements.add(e);
 191             }
 192             e.accept(this, p);
 193             return annotatedElements;
 194         }
 195     }
 196 
 197     private class AnnotationSetMultiScanner extends
 198         ElementScanningIncludingTypeParameters<Set<Element>, Set<TypeElement>> {
 199         // Insertion-order preserving set
 200         Set<Element> annotatedElements = new LinkedHashSet<>();
 201 
 202         AnnotationSetMultiScanner(Set<Element> defaultSet) {
 203             super(defaultSet);
 204         }
 205 
 206         @Override @DefinedBy(Api.LANGUAGE_MODEL)
 207         public Set<Element> scan(Element e, Set<TypeElement> p) {
 208             java.util.List<? extends AnnotationMirror> annotationMirrors =
 209                 eltUtils.getAllAnnotationMirrors(e);
 210             for (AnnotationMirror annotationMirror : annotationMirrors) {
 211                 if (p.contains(annotationMirror.getAnnotationType().asElement()))
 212                     annotatedElements.add(e);
 213             }
 214             e.accept(this, p);
 215             return annotatedElements;
 216         }
 217     }
 218 
 219     /**
 220      * {@inheritDoc}
 221      */
 222     @DefinedBy(Api.ANNOTATION_PROCESSING)
 223     public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
 224         throwIfNotAnnotation(a);

 225         String name = a.getCanonicalName();
 226         if (name == null)
 227             return Collections.emptySet();
 228         else {
 229             TypeElement annotationType = eltUtils.getTypeElement(name);
 230             if (annotationType == null)
 231                 return Collections.emptySet();
 232             else
 233                 return getElementsAnnotatedWith(annotationType);
 234         }
 235     }
 236 
 237     @DefinedBy(Api.ANNOTATION_PROCESSING)
 238     public Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> annotations) {
 239         List<TypeElement> annotationsAsElements = new ArrayList<>(annotations.size());
 240 
 241         for (Class<? extends Annotation> annotation : annotations) {
 242             throwIfNotAnnotation(annotation);
 243             String name = annotation.getCanonicalName();
 244             if (name == null)
 245                 continue;
 246             annotationsAsElements.add(eltUtils.getTypeElement(name));
 247         }
 248 
 249         return getElementsAnnotatedWithAny(annotationsAsElements.toArray(new TypeElement[0]));
 250     }
 251 
 252     private void throwIfNotAnnotation(Class<? extends Annotation> a) {
 253         if (!a.isAnnotation())
 254             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 255     }
 256 
 257     private void throwIfNotAnnotation(TypeElement a) {
 258         if (a.getKind() != ElementKind.ANNOTATION_TYPE)
 259             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 260     }
 261 }
< prev index next >