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

Print this page
rev 224 : 6498938: Faulty comparison of TypeMirror objects in getElementsAnnotatedWith implementation
Reviewed-by: jjg
   1 /*
   2  * Copyright 2005-2008 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or


  94         return rootElements;
  95     }
  96 
  97     private static final String NOT_AN_ANNOTATION_TYPE =
  98         "The argument does not represent an annotation type: ";
  99 
 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 + a);
 116 
 117         DeclaredType annotationTypeElement;
 118         TypeMirror tm = a.asType();
 119         if ( tm instanceof DeclaredType )
 120             annotationTypeElement = (DeclaredType) a.asType();
 121         else
 122             throw new AssertionError("Bad implementation type for " + tm);
 123 
 124         ElementScanner6<Set<Element>, DeclaredType> scanner =
 125             new AnnotationSetScanner(result);
 126 
 127         for (Element element : rootElements)
 128             result = scanner.scan(element, annotationTypeElement);
 129 
 130         return result;
 131     }
 132 
 133     // Could be written as a local class inside getElementsAnnotatedWith
 134     private class AnnotationSetScanner extends
 135         ElementScanner6<Set<Element>, DeclaredType> {
 136         // Insertion-order preserving set
 137         Set<Element> annotatedElements = new LinkedHashSet<Element>();

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

 141         }
 142 
 143         @Override
 144         public Set<Element> scan(Element e, DeclaredType p) {
 145             java.util.List<? extends AnnotationMirror> annotationMirrors =
 146                 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
 147             for (AnnotationMirror annotationMirror : annotationMirrors) {
 148                 if (annotationMirror.getAnnotationType().equals(p))
 149                     annotatedElements.add(e);
 150             }
 151             e.accept(this, p);
 152             return annotatedElements;
 153         }
 154 
 155     }
 156 
 157     /**
 158      * {@inheritdoc}
 159      */
 160     public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
 161         if (!a.isAnnotation())
 162             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 163         String name = a.getCanonicalName();
 164         if (name == null)
 165             return Collections.emptySet();
 166         else {
 167             TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name);
 168             if (annotationType == null)
   1 /*
   2  * Copyright 2005-2009 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or


  94         return rootElements;
  95     }
  96 
  97     private static final String NOT_AN_ANNOTATION_TYPE =
  98         "The argument does not represent an annotation type: ";
  99 
 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         Types typeUtil = processingEnv.getTypeUtils();
 115         if (a.getKind() != ElementKind.ANNOTATION_TYPE)
 116             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + 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 " + tm);
 124 
 125         ElementScanner6<Set<Element>, DeclaredType> scanner =
 126             new AnnotationSetScanner(result, typeUtil);
 127 
 128         for (Element element : rootElements)
 129             result = scanner.scan(element, annotationTypeElement);
 130 
 131         return result;
 132     }
 133 
 134     // Could be written as a local class inside getElementsAnnotatedWith
 135     private class AnnotationSetScanner extends
 136         ElementScanner6<Set<Element>, DeclaredType> {
 137         // Insertion-order preserving set
 138         Set<Element> annotatedElements = new LinkedHashSet<Element>();
 139         Types typeUtil;
 140 
 141         AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) {
 142             super(defaultSet);
 143             this.typeUtil = typeUtil;
 144         }
 145 
 146         @Override
 147         public Set<Element> scan(Element e, DeclaredType p) {
 148             java.util.List<? extends AnnotationMirror> annotationMirrors =
 149                 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
 150             for (AnnotationMirror annotationMirror : annotationMirrors) {
 151                 if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p))
 152                     annotatedElements.add(e);
 153             }
 154             e.accept(this, p);
 155             return annotatedElements;
 156         }
 157 
 158     }
 159 
 160     /**
 161      * {@inheritdoc}
 162      */
 163     public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
 164         if (!a.isAnnotation())
 165             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 166         String name = a.getCanonicalName();
 167         if (name == null)
 168             return Collections.emptySet();
 169         else {
 170             TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name);
 171             if (annotationType == null)