< prev index next >

test/langtools/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java

Print this page


   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 TestElementsAnnotatedWith.java
  34  * @compile InheritedAnnotation.java
  35  * @compile TpAnno.java
  36  * @compile Anno.java
  37  * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
  38  * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
  39  * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
  40  * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
  41  * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
  42  * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
  43  * @compile -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java



  44  * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java
  45  * @compile Foo.java
  46  * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
  47  */
  48 

  49 import java.lang.annotation.Annotation;
  50 import java.util.Collections;
  51 import java.util.Set;
  52 import java.util.HashSet;
  53 import java.util.Arrays;
  54 import java.util.Objects;
  55 import javax.annotation.processing.*;
  56 import javax.lang.model.element.*;
  57 import static javax.lang.model.util.ElementFilter.*;
  58 
  59 /**
  60  * This processor verifies that the information returned by
  61  * getElementsAnnotatedWith and getElementsAnnotatedWithAny is
  62  * consistent with the expected results stored in an
  63  * AnnotatedElementInfo annotation.
  64  */
  65 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
  66 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor {
  67 
  68     public boolean process(Set<? extends TypeElement> annotations,


 131             return re.processingOver();
 132         }
 133 
 134     }
 135 
 136     /**
 137      * The method checks the following conditions:
 138      *
 139      * 1) The sets of elements found are equal for the TypeElement and
 140      * Class<? extends Annotation> methods on logically equivalent
 141      * arguments.
 142      *
 143      * 2) getElementsAnnotatedWithAny(X) is equal to
 144      * getElementsAnnotatedWith(X') where X is a set/var-args array
 145      * with one element and X' is the element.
 146      *
 147      * 3) Verify the result of getElementsAnnotatedWithAny({X, Y}) is equal to
 148      * getElementsAnnotatedWith(X) UNION getElementsAnnotatedWith(Y).
 149      */
 150     void checkSetOfAnnotatedElements(RoundEnvironment re) {
 151         TypeElement annotatedElemInfoElem =  elements.getTypeElement("AnnotatedElementInfo");


 152 
 153         // For the "Any" methods, search for both the expected
 154         // annotation and AnnotatedElementInfo and verify the return
 155         // set is the union of searching for AnnotatedElementInfo and
 156         // the other annotation
 157         Set<? extends Element> resultsMeta         = Collections.emptySet();
 158         Set<? extends Element> resultsMetaAny      = Collections.emptySet();
 159         Set<Element>           resultsMetaMulti    = new HashSet<>();
 160         Set<? extends Element> resultsMetaAnyMulti = Collections.emptySet();
 161         Set<? extends Element> resultsBase         = Collections.emptySet();
 162         Set<? extends Element> resultsBaseAny      = Collections.emptySet();
 163         Set<? extends Element> resultsBaseAnyMulti = Collections.emptySet();
 164 
 165         if (!re.processingOver()) {
 166             testNonAnnotations(re);
 167 
 168             // Verify AnnotatedElementInfo is present on the first
 169             // specified type.
 170 
 171             TypeElement firstType = typesIn(re.getRootElements()).iterator().next();
 172 
 173             AnnotatedElementInfo annotatedElemInfo =
 174                 firstType.getAnnotation(AnnotatedElementInfo.class);







 175 
 176             boolean failed = false;
 177 
 178             Objects.requireNonNull(annotatedElemInfo,
 179                                    "Missing AnnotatedElementInfo annotation on " + firstType);
 180 
 181             // Verify that the annotation information is as expected.
 182             Set<String> expectedNames =
 183                 new HashSet<>(Arrays.asList(annotatedElemInfo.names()));
 184 
 185             String annotationName = annotatedElemInfo.annotationName();
 186             TypeElement annotationTypeElem = elements.getTypeElement(annotationName);

 187 
 188             resultsMeta         = re.getElementsAnnotatedWith(annotationTypeElem);
 189             resultsMetaAny      = re.getElementsAnnotatedWithAny(annotationTypeElem);
 190             resultsMetaMulti.addAll(resultsMeta);
 191             resultsMetaMulti.addAll(re.getElementsAnnotatedWith(annotatedElemInfoElem));
 192             resultsMetaAnyMulti = re.getElementsAnnotatedWithAny(annotationTypeElem, annotatedElemInfoElem);
 193 
 194             if (!resultsMeta.isEmpty())
 195                 System.err.println("Results: " + resultsMeta);
 196 
 197             if (!resultsMeta.equals(resultsMetaAny)) {
 198                 failed = true;
 199                 System.err.printf("Inconsistent Meta with vs withAny results");
 200             }
 201 
 202             if (resultsMeta.size() != annotatedElemInfo.expectedSize()) {
 203                 failed = true;
 204                 System.err.printf("Bad number of elements; expected %d, got %d%n",
 205                                   annotatedElemInfo.expectedSize(), resultsMeta.size());
 206             } else {


   1 /*
   2  * Copyright (c) 2006, 2018, 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 8190886
  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 mod/quux/package-info.java mod/quux/Quux.java
  47  * @compile -processor TestElementsAnnotatedWith -proc:only mod/module-info.java
  48  * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java
  49  * @compile Foo.java
  50  * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
  51  */
  52 
  53 import annot.AnnotatedElementInfo;
  54 import java.lang.annotation.Annotation;
  55 import java.util.Collections;
  56 import java.util.Set;
  57 import java.util.HashSet;
  58 import java.util.Arrays;
  59 import java.util.Objects;
  60 import javax.annotation.processing.*;
  61 import javax.lang.model.element.*;
  62 import static javax.lang.model.util.ElementFilter.*;
  63 
  64 /**
  65  * This processor verifies that the information returned by
  66  * getElementsAnnotatedWith and getElementsAnnotatedWithAny is
  67  * consistent with the expected results stored in an
  68  * AnnotatedElementInfo annotation.
  69  */
  70 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
  71 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor {
  72 
  73     public boolean process(Set<? extends TypeElement> annotations,


 136             return re.processingOver();
 137         }
 138 
 139     }
 140 
 141     /**
 142      * The method checks the following conditions:
 143      *
 144      * 1) The sets of elements found are equal for the TypeElement and
 145      * Class<? extends Annotation> methods on logically equivalent
 146      * arguments.
 147      *
 148      * 2) getElementsAnnotatedWithAny(X) is equal to
 149      * getElementsAnnotatedWith(X') where X is a set/var-args array
 150      * with one element and X' is the element.
 151      *
 152      * 3) Verify the result of getElementsAnnotatedWithAny({X, Y}) is equal to
 153      * getElementsAnnotatedWith(X) UNION getElementsAnnotatedWith(Y).
 154      */
 155     void checkSetOfAnnotatedElements(RoundEnvironment re) {
 156         ModuleElement UNNAMED_MODULE = elements.getModuleElement("");
 157         TypeElement annotatedElemInfoElem =
 158             elements.getTypeElement(UNNAMED_MODULE, "annot.AnnotatedElementInfo");
 159 
 160         // For the "Any" methods, search for both the expected
 161         // annotation and AnnotatedElementInfo and verify the return
 162         // set is the union of searching for AnnotatedElementInfo and
 163         // the other annotation
 164         Set<? extends Element> resultsMeta         = Collections.emptySet();
 165         Set<? extends Element> resultsMetaAny      = Collections.emptySet();
 166         Set<Element>           resultsMetaMulti    = new HashSet<>();
 167         Set<? extends Element> resultsMetaAnyMulti = Collections.emptySet();
 168         Set<? extends Element> resultsBase         = Collections.emptySet();
 169         Set<? extends Element> resultsBaseAny      = Collections.emptySet();
 170         Set<? extends Element> resultsBaseAnyMulti = Collections.emptySet();
 171 
 172         if (!re.processingOver()) {
 173             testNonAnnotations(re);
 174 
 175             // Verify AnnotatedElementInfo is present on the first
 176             // specified type.
 177 
 178             Element firstElement = re.getRootElements().iterator().next();
 179 
 180             AnnotatedElementInfo annotatedElemInfo =
 181                 firstElement.getAnnotation(AnnotatedElementInfo.class);
 182 
 183             // DEBUGGING
 184             if (firstElement.getKind() == ElementKind.MODULE) {
 185                 elements.printElements(new java.io.OutputStreamWriter(System.out) , firstElement);
 186                 System.out.println("AnnotatedElementInfo module: " + AnnotatedElementInfo.class.getModule());
 187             }
 188 
 189 
 190             boolean failed = false;
 191 
 192             Objects.requireNonNull(annotatedElemInfo,
 193                                    "Missing AnnotatedElementInfo annotation on " + firstElement);
 194 
 195             // Verify that the annotation information is as expected.
 196             Set<String> expectedNames =
 197                 new HashSet<>(Arrays.asList(annotatedElemInfo.names()));
 198 
 199             String annotationName = annotatedElemInfo.annotationName();
 200             TypeElement annotationTypeElem = elements.getTypeElement(UNNAMED_MODULE,
 201                                                                      annotationName);
 202 
 203             resultsMeta         = re.getElementsAnnotatedWith(annotationTypeElem);
 204             resultsMetaAny      = re.getElementsAnnotatedWithAny(annotationTypeElem);
 205             resultsMetaMulti.addAll(resultsMeta);
 206             resultsMetaMulti.addAll(re.getElementsAnnotatedWith(annotatedElemInfoElem));
 207             resultsMetaAnyMulti = re.getElementsAnnotatedWithAny(annotationTypeElem, annotatedElemInfoElem);
 208 
 209             if (!resultsMeta.isEmpty())
 210                 System.err.println("Results: " + resultsMeta);
 211 
 212             if (!resultsMeta.equals(resultsMetaAny)) {
 213                 failed = true;
 214                 System.err.printf("Inconsistent Meta with vs withAny results");
 215             }
 216 
 217             if (resultsMeta.size() != annotatedElemInfo.expectedSize()) {
 218                 failed = true;
 219                 System.err.printf("Bad number of elements; expected %d, got %d%n",
 220                                   annotatedElemInfo.expectedSize(), resultsMeta.size());
 221             } else {


< prev index next >