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

Fri, 21 Dec 2012 08:45:43 -0800

author
darcy
date
Fri, 21 Dec 2012 08:45:43 -0800
changeset 1466
b52a38d4536c
parent 699
d2aaaec153e8
child 1876
1908e86ee49a
permissions
-rw-r--r--

8005282: Use @library tag with non-relative path for javac tests
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2006, 2010, 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  */
    24 /*
    25  * @test
    26  * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854
    27  * @summary Tests that getElementsAnnotatedWith works properly.
    28  * @author  Joseph D. Darcy
    29  * @library /tools/javac/lib
    30  * @build   JavacTestingAbstractProcessor
    31  * @compile TestElementsAnnotatedWith.java
    32  * @compile InheritedAnnotation.java
    33  * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
    34  * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
    35  * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
    36  * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
    37  * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
    38  * @compile Foo.java
    39  * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
    40  */
    42 import java.lang.annotation.Annotation;
    43 import java.io.*;
    44 import java.util.Collections;
    45 import java.util.Set;
    46 import java.util.HashSet;
    47 import java.util.List;
    48 import java.util.ArrayList;
    49 import java.util.Arrays;
    50 import javax.annotation.processing.*;
    51 import javax.tools.*;
    52 import javax.lang.model.SourceVersion;
    53 import javax.lang.model.element.*;
    54 import javax.lang.model.util.*;
    55 import static javax.lang.model.util.ElementFilter.*;
    57 /**
    58  * This processor verifies that the information returned by
    59  * getElementsAnnotatedWith is consistent with the expected results
    60  * stored in an AnnotatedElementInfo annotation.
    61  */
    62 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
    63 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor {
    65     public boolean process(Set<? extends TypeElement> annotations,
    66                            RoundEnvironment roundEnvironment) {
    67         TypeElement annotatedElementInfoElement =
    68             elements.getTypeElement("AnnotatedElementInfo");
    69         Set<? extends Element> resultsMeta = Collections.emptySet();
    70         Set<? extends Element> resultsBase = Collections.emptySet();
    72         if (!roundEnvironment.processingOver()) {
    73             testNonAnnotations(roundEnvironment);
    75             // Verify AnnotatedElementInfo is present on the first
    76             // specified type.
    78             TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next();
    80             AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class);
    82             boolean failed = false;
    84             if (annotatedElementInfo == null)
    85                 throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " +
    86                                                   firstType);
    87             else {
    88                 // Verify that the annotation information is as
    89                 // expected.
    91                 Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names()));
    93                 resultsMeta =
    94                     roundEnvironment.
    95                     getElementsAnnotatedWith(elements.getTypeElement(annotatedElementInfo.annotationName()));
    97                 System.err.println("Results: " + resultsMeta);
    99                 if (resultsMeta.size() != annotatedElementInfo.expectedSize()) {
   100                     failed = true;
   101                     System.err.printf("Bad number of elements; expected %d, got %d%n",
   102                                       annotatedElementInfo.expectedSize(), resultsMeta.size());
   103                 } else {
   104                     for(Element element : resultsMeta) {
   105                         String simpleName = element.getSimpleName().toString();
   106                         if (!expectedNames.contains(simpleName) ) {
   107                             failed = true;
   108                             System.err.println("Name ``" + simpleName + "'' not expected.");
   109                         }
   110                     }
   111                 }
   112             }
   114             resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName());
   116             if (!resultsMeta.equals(resultsBase)) {
   117                 failed = true;
   118                 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta +
   119                                    "\nbase: " + resultsBase);
   120             }
   122             if (failed) {
   123                 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo);
   124                 throw new RuntimeException();
   125             }
   126         } else {
   127             // If processing is over without an error, the specified
   128             // elements should be empty so an empty set should be returned.
   129             resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement);
   130             resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class);
   131             if (!resultsMeta.isEmpty())
   132                 throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta);
   133             if (!resultsBase.isEmpty())
   134                 throw new RuntimeException("Nonempty resultsBase: " + resultsBase);
   136         }
   137         return true;
   138     }
   140     private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) {
   141         try {
   142             return roundEnvironment.
   143                 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class));
   144         } catch(ClassNotFoundException cnfe) {
   145             throw new RuntimeException(cnfe);
   146         }
   147     }
   149     /**
   150      * Verify non-annotation types result in
   151      * IllegalArgumentExceptions.
   152      */
   153     private void testNonAnnotations(RoundEnvironment roundEnvironment) {
   154         try {
   155             Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class );
   156             throw new RuntimeException("Illegal argument exception not thrown");
   157         } catch(IllegalArgumentException iae) {}
   159         try {
   160             Set<? extends Element> elements =
   161                 roundEnvironment.getElementsAnnotatedWith(processingEnv.
   162                                                           getElementUtils().
   163                                                           getTypeElement("java.lang.Object") );
   164             throw new RuntimeException("Illegal argument exception not thrown");
   165         } catch(IllegalArgumentException iae) {}
   166     }
   167 }

mercurial