1 /*
   2  * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.processing;
  27 
  28 import java.lang.annotation.Annotation;
  29 import com.sun.tools.javac.tree.JCTree.*;
  30 import javax.annotation.processing.*;
  31 import javax.lang.model.element.*;
  32 import javax.lang.model.type.DeclaredType;
  33 import javax.lang.model.type.TypeMirror;
  34 import javax.lang.model.util.*;
  35 import java.util.*;
  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     public boolean processingOver() {
  76         return processingOver;
  77     }
  78 
  79     /**
  80      * Returns {@code true} if an error was raised in the prior round
  81      * of processing; returns {@code false} otherwise.
  82      *
  83      * @return {@code true} if an error was raised in the prior round
  84      * of processing; returns {@code false} otherwise.
  85      */
  86     public boolean errorRaised() {
  87         return errorRaised;
  88     }
  89 
  90     /**
  91      * Returns the type elements specified by the prior round.
  92      *
  93      * @return the types elements specified by the prior round, or an
  94      * empty set if there were none
  95      */
  96     public Set<? extends Element> getRootElements() {
  97         return rootElements;
  98     }
  99 
 100     private static final String NOT_AN_ANNOTATION_TYPE =
 101         "The argument does not represent an annotation type: ";
 102 
 103     /**
 104      * Returns the elements annotated with the given annotation type.
 105      * Only type elements <i>included</i> in this round of annotation
 106      * processing, or declarations of members, parameters, or type
 107      * parameters declared within those, are returned.  Included type
 108      * elements are {@linkplain #getRootElements specified
 109      * types} and any types nested within them.
 110      *
 111      * @param a  annotation type being requested
 112      * @return the elements annotated with the given annotation type,
 113      * or an empty set if there are none
 114      */
 115     public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
 116         Set<Element> result = Collections.emptySet();
 117         Types typeUtil = processingEnv.getTypeUtils();
 118         if (a.getKind() != ElementKind.ANNOTATION_TYPE)
 119             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 120 
 121         DeclaredType annotationTypeElement;
 122         TypeMirror tm = a.asType();
 123         if ( tm instanceof DeclaredType )
 124             annotationTypeElement = (DeclaredType) a.asType();
 125         else
 126             throw new AssertionError("Bad implementation type for " + tm);
 127 
 128         ElementScanner8<Set<Element>, DeclaredType> scanner =
 129             new AnnotationSetScanner(result, typeUtil);
 130 
 131         for (Element element : rootElements)
 132             result = scanner.scan(element, annotationTypeElement);
 133 
 134         return result;
 135     }
 136 
 137     // Could be written as a local class inside getElementsAnnotatedWith
 138     private class AnnotationSetScanner extends
 139         ElementScanner8<Set<Element>, DeclaredType> {
 140         // Insertion-order preserving set
 141         Set<Element> annotatedElements = new LinkedHashSet<Element>();
 142         Types typeUtil;
 143 
 144         AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) {
 145             super(defaultSet);
 146             this.typeUtil = typeUtil;
 147         }
 148 
 149         @Override
 150         public Set<Element> scan(Element e, DeclaredType p) {
 151             java.util.List<? extends AnnotationMirror> annotationMirrors =
 152                 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
 153             for (AnnotationMirror annotationMirror : annotationMirrors) {
 154                 if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p))
 155                     annotatedElements.add(e);
 156             }
 157             e.accept(this, p);
 158             return annotatedElements;
 159         }
 160 
 161     }
 162 
 163     /**
 164      * {@inheritdoc}
 165      */
 166     public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
 167         if (!a.isAnnotation())
 168             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 169         String name = a.getCanonicalName();
 170         if (name == null)
 171             return Collections.emptySet();
 172         else {
 173             TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name);
 174             if (annotationType == null)
 175                 return Collections.emptySet();
 176             else
 177                 return getElementsAnnotatedWith(annotationType);
 178         }
 179     }
 180 }