1 /*
   2  * Copyright (c) 2006, 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049 8038080 8032230
  27  * @summary Tests that getElementsAnnotatedWith works properly.
  28  * @author  Joseph D. Darcy
  29  * @library /tools/javac/lib
  30  * @modules java.compiler
  31  *          jdk.compiler
  32  * @build   JavacTestingAbstractProcessor
  33  * @compile annot/AnnotatedElementInfo.java annot/MarkerAnnot.java
  34  * @compile TestElementsAnnotatedWith.java
  35  * @compile InheritedAnnotation.java
  36  * @compile TpAnno.java
  37  * @compile Anno.java
  38  * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
  39  * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
  40  * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
  41  * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
  42  * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
  43  * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
  44  * @compile -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java
  45  * @compile -processor TestElementsAnnotatedWith -proc:only pkg/package-info.java
  46  * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java
  47  * @compile Foo.java
  48  * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
  49  */
  50 
  51 import annot.AnnotatedElementInfo;
  52 import java.lang.annotation.Annotation;
  53 import java.util.Collections;
  54 import java.util.Set;
  55 import java.util.HashSet;
  56 import java.util.Arrays;
  57 import java.util.Objects;
  58 import javax.annotation.processing.*;
  59 import javax.lang.model.element.*;
  60 import static javax.lang.model.util.ElementFilter.*;
  61 
  62 /**
  63  * This processor verifies that the information returned by
  64  * getElementsAnnotatedWith and getElementsAnnotatedWithAny is
  65  * consistent with the expected results stored in an
  66  * AnnotatedElementInfo annotation.
  67  */
  68 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
  69 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor {
  70 
  71     public boolean process(Set<? extends TypeElement> annotations,
  72                            RoundEnvironment roundEnv) {
  73         // First check sets of annotated elements using the round
  74         // environment from the annotation processing tool framework.
  75         checkSetOfAnnotatedElements(roundEnv);
  76 
  77         // Next check sets of annotated elements using a round
  78         // environment which uses the default implementations of the
  79         // getElementsAnnotatedWithAny methods from the interface.
  80         checkSetOfAnnotatedElements(new TestingRoundEnvironment(roundEnv));
  81         return true;
  82     }
  83 
  84     /**
  85      * To allow testing of the executable code of the default methods
  86      * for the two overloaded getElementsAnnotatedWithAny methods
  87      * defined in the RoundEnvironment interface, this class delegates
  88      * the non-default methods of RoundEnvironment to a given
  89      * RoundEnvironment object and then explicitly calls the default
  90      * methods of the interface instead of relying on the object's
  91      * implementation of those methods.
  92      */
  93     private class TestingRoundEnvironment implements RoundEnvironment {
  94         private RoundEnvironment re;
  95 
  96         public TestingRoundEnvironment(RoundEnvironment re) {
  97             this.re = re;
  98         }
  99 
 100         @Override
 101         public boolean errorRaised() {
 102             return re.errorRaised();
 103         }
 104 
 105         @Override
 106         public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
 107             return re.getElementsAnnotatedWith(a);
 108         }
 109 
 110         @Override
 111         public Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> a) {
 112             // Default method defined in the interface
 113             return RoundEnvironment.super.getElementsAnnotatedWithAny(a);
 114         }
 115 
 116         @Override
 117         public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
 118             return re.getElementsAnnotatedWith(a);
 119         }
 120 
 121         @Override
 122         public Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... a) {
 123             // Default method defined in the interface
 124             return RoundEnvironment.super.getElementsAnnotatedWithAny(a);
 125         }
 126 
 127         @Override
 128         public Set<? extends Element> getRootElements() {
 129             return re.getRootElements();
 130         }
 131 
 132         @Override
 133         public boolean processingOver() {
 134             return re.processingOver();
 135         }
 136 
 137     }
 138 
 139     /**
 140      * The method checks the following conditions:
 141      *
 142      * 1) The sets of elements found are equal for the TypeElement and
 143      * Class<? extends Annotation> methods on logically equivalent
 144      * arguments.
 145      *
 146      * 2) getElementsAnnotatedWithAny(X) is equal to
 147      * getElementsAnnotatedWith(X') where X is a set/var-args array
 148      * with one element and X' is the element.
 149      *
 150      * 3) Verify the result of getElementsAnnotatedWithAny({X, Y}) is equal to
 151      * getElementsAnnotatedWith(X) UNION getElementsAnnotatedWith(Y).
 152      */
 153     void checkSetOfAnnotatedElements(RoundEnvironment re) {
 154         TypeElement annotatedElemInfoElem =  elements.getTypeElement("annot.AnnotatedElementInfo");
 155 
 156         // For the "Any" methods, search for both the expected
 157         // annotation and AnnotatedElementInfo and verify the return
 158         // set is the union of searching for AnnotatedElementInfo and
 159         // the other annotation
 160         Set<? extends Element> resultsMeta         = Collections.emptySet();
 161         Set<? extends Element> resultsMetaAny      = Collections.emptySet();
 162         Set<Element>           resultsMetaMulti    = new HashSet<>();
 163         Set<? extends Element> resultsMetaAnyMulti = Collections.emptySet();
 164         Set<? extends Element> resultsBase         = Collections.emptySet();
 165         Set<? extends Element> resultsBaseAny      = Collections.emptySet();
 166         Set<? extends Element> resultsBaseAnyMulti = Collections.emptySet();
 167 
 168         if (!re.processingOver()) {
 169             testNonAnnotations(re);
 170 
 171             // Verify AnnotatedElementInfo is present on the first
 172             // specified type.
 173 
 174             Element firstElement = re.getRootElements().iterator().next();
 175 
 176             AnnotatedElementInfo annotatedElemInfo =
 177                 firstElement.getAnnotation(AnnotatedElementInfo.class);
 178 
 179             boolean failed = false;
 180 
 181             Objects.requireNonNull(annotatedElemInfo,
 182                                    "Missing AnnotatedElementInfo annotation on " + firstElement);
 183 
 184             // Verify that the annotation information is as expected.
 185             Set<String> expectedNames =
 186                 new HashSet<>(Arrays.asList(annotatedElemInfo.names()));
 187 
 188             String annotationName = annotatedElemInfo.annotationName();
 189             TypeElement annotationTypeElem = elements.getTypeElement(annotationName);
 190 
 191             resultsMeta         = re.getElementsAnnotatedWith(annotationTypeElem);
 192             resultsMetaAny      = re.getElementsAnnotatedWithAny(annotationTypeElem);
 193             resultsMetaMulti.addAll(resultsMeta);
 194             resultsMetaMulti.addAll(re.getElementsAnnotatedWith(annotatedElemInfoElem));
 195             resultsMetaAnyMulti = re.getElementsAnnotatedWithAny(annotationTypeElem, annotatedElemInfoElem);
 196 
 197             if (!resultsMeta.isEmpty())
 198                 System.err.println("Results: " + resultsMeta);
 199 
 200             if (!resultsMeta.equals(resultsMetaAny)) {
 201                 failed = true;
 202                 System.err.printf("Inconsistent Meta with vs withAny results");
 203             }
 204 
 205             if (resultsMeta.size() != annotatedElemInfo.expectedSize()) {
 206                 failed = true;
 207                 System.err.printf("Bad number of elements; expected %d, got %d%n",
 208                                   annotatedElemInfo.expectedSize(), resultsMeta.size());
 209             } else {
 210                 for(Element element : resultsMeta) {
 211                     String simpleName = element.getSimpleName().toString();
 212                     if (!expectedNames.contains(simpleName) ) {
 213                         failed = true;
 214                         System.err.println("Name ``" + simpleName + "'' not expected.");
 215                     }
 216                 }
 217             }
 218 
 219             resultsBase    = computeResultsBase(re, annotationName);
 220             resultsBaseAny = computeResultsBaseAny(re, annotationName);
 221             try {
 222                 Set<Class<? extends Annotation>> tmp = new HashSet<>();
 223                 tmp.add(AnnotatedElementInfo.class);
 224                 tmp.add(Class.forName(annotationName).asSubclass(Annotation.class));
 225                 resultsBaseAnyMulti = re.getElementsAnnotatedWithAny(tmp);
 226             } catch (ClassNotFoundException e) {
 227                 throw new RuntimeException(e);
 228             }
 229 
 230             if (!resultsBase.equals(resultsBaseAny)) {
 231                 failed = true;
 232                 System.err.printf("Inconsistent Base with vs withAny results");
 233             }
 234 
 235             if (!resultsMeta.equals(resultsBase)) {
 236                 failed = true;
 237                 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta +
 238                                    "\nbase: " + resultsBase);
 239             }
 240 
 241             if (!resultsMetaAnyMulti.equals(resultsMetaMulti)) {
 242                 failed = true;
 243                 System.err.println("MetaMultAny and MetaMulti sets unequal;\n meta: " + resultsMeta +
 244                                    "\nbase: " + resultsBase);
 245             }
 246 
 247             if (!resultsBaseAnyMulti.equals(resultsMetaAnyMulti)) {
 248                 failed = true;
 249                 System.err.println("BaseMulti and MetaMulti sets unequal;\n meta: " + resultsMeta +
 250                                    "\nbase: " + resultsBase);
 251             }
 252 
 253             if (failed) {
 254                 System.err.println("AnnotatedElementInfo: " + annotatedElemInfo);
 255                 throw new RuntimeException();
 256             }
 257         } else {
 258             // If processing is over without an error, the specified
 259             // elements should be empty so an empty set should be
 260             // returned.
 261             throwOnNonEmpty(re.getElementsAnnotatedWith(annotatedElemInfoElem), "resultsMeta");
 262             throwOnNonEmpty(re.getElementsAnnotatedWithAny(annotatedElemInfoElem), "resultsMetaAny");
 263             throwOnNonEmpty(re.getElementsAnnotatedWith(AnnotatedElementInfo.class),    "resultsBase");
 264             throwOnNonEmpty(re.getElementsAnnotatedWithAny(Set.of(AnnotatedElementInfo.class)), "resultsBaseAny");
 265         }
 266     }
 267 
 268     private void throwOnNonEmpty(Set<? extends Element> results, String message) {
 269         if (!results.isEmpty()) {
 270                 throw new RuntimeException("Nonempty " + message +  "\t"  + results);
 271         }
 272     }
 273 
 274     private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnv, String name) {
 275         try {
 276             return roundEnv.
 277                 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class));
 278         } catch (ClassNotFoundException cnfe) {
 279             throw new RuntimeException(cnfe);
 280         }
 281     }
 282 
 283     private Set<? extends Element> computeResultsBaseAny(RoundEnvironment roundEnv, String name) {
 284         try {
 285             return roundEnv.
 286                 getElementsAnnotatedWithAny(Set.of(Class.forName(name).asSubclass(Annotation.class)));
 287         } catch (ClassNotFoundException cnfe) {
 288             throw new RuntimeException(cnfe);
 289         }
 290     }
 291 
 292     /**
 293      * Verify non-annotation types result in
 294      * IllegalArgumentExceptions.
 295      */
 296     private void testNonAnnotations(RoundEnvironment roundEnv) {
 297         Class objectClass = (Class)Object.class;
 298         Set<? extends Element> elements;
 299         try {
 300             elements = roundEnv.getElementsAnnotatedWith(objectClass);
 301             throw new RuntimeException("Illegal argument exception not thrown");
 302         } catch (IllegalArgumentException iae) {}
 303 
 304         try {
 305             elements = roundEnv.getElementsAnnotatedWithAny(Set.of(objectClass));
 306             throw new RuntimeException("Illegal argument exception not thrown");
 307         } catch (IllegalArgumentException iae) {}
 308 
 309         TypeElement objectElement = processingEnv.getElementUtils().getTypeElement("java.lang.Object");
 310         try {
 311             elements = roundEnv.getElementsAnnotatedWith(objectElement);
 312             throw new RuntimeException("Illegal argument exception not thrown");
 313         } catch (IllegalArgumentException iae) {}
 314 
 315         try {
 316             elements = roundEnv.getElementsAnnotatedWithAny(objectElement);
 317             throw new RuntimeException("Illegal argument exception not thrown");
 318         } catch (IllegalArgumentException iae) {}
 319     }
 320 }