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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049 8038080
aoqi@0 27 * @summary Tests that getElementsAnnotatedWith works properly.
aoqi@0 28 * @author Joseph D. Darcy
aoqi@0 29 * @library /tools/javac/lib
aoqi@0 30 * @build JavacTestingAbstractProcessor
aoqi@0 31 * @compile TestElementsAnnotatedWith.java
aoqi@0 32 * @compile InheritedAnnotation.java
aoqi@0 33 * @compile TpAnno.java
aoqi@0 34 * @compile Anno.java
aoqi@0 35 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
aoqi@0 36 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
aoqi@0 37 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
aoqi@0 38 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
aoqi@0 39 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
aoqi@0 40 * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
aoqi@0 41 * @compile -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java
aoqi@0 42 * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java
aoqi@0 43 * @compile Foo.java
aoqi@0 44 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
aoqi@0 45 */
aoqi@0 46
aoqi@0 47 import java.lang.annotation.Annotation;
aoqi@0 48 import java.util.Collections;
aoqi@0 49 import java.util.Set;
aoqi@0 50 import java.util.HashSet;
aoqi@0 51 import java.util.Arrays;
aoqi@0 52 import javax.annotation.processing.*;
aoqi@0 53 import javax.lang.model.element.*;
aoqi@0 54 import static javax.lang.model.util.ElementFilter.*;
aoqi@0 55
aoqi@0 56 /**
aoqi@0 57 * This processor verifies that the information returned by
aoqi@0 58 * getElementsAnnotatedWith is consistent with the expected results
aoqi@0 59 * stored in an AnnotatedElementInfo annotation.
aoqi@0 60 */
aoqi@0 61 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
aoqi@0 62 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor {
aoqi@0 63
aoqi@0 64 public boolean process(Set<? extends TypeElement> annotations,
aoqi@0 65 RoundEnvironment roundEnvironment) {
aoqi@0 66 TypeElement annotatedElementInfoElement =
aoqi@0 67 elements.getTypeElement("AnnotatedElementInfo");
aoqi@0 68 Set<? extends Element> resultsMeta = Collections.emptySet();
aoqi@0 69 Set<? extends Element> resultsBase = Collections.emptySet();
aoqi@0 70
aoqi@0 71 if (!roundEnvironment.processingOver()) {
aoqi@0 72 testNonAnnotations(roundEnvironment);
aoqi@0 73
aoqi@0 74 // Verify AnnotatedElementInfo is present on the first
aoqi@0 75 // specified type.
aoqi@0 76
aoqi@0 77 TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next();
aoqi@0 78
aoqi@0 79 AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class);
aoqi@0 80
aoqi@0 81 boolean failed = false;
aoqi@0 82
aoqi@0 83 if (annotatedElementInfo == null)
aoqi@0 84 throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " +
aoqi@0 85 firstType);
aoqi@0 86 else {
aoqi@0 87 // Verify that the annotation information is as
aoqi@0 88 // expected.
aoqi@0 89
aoqi@0 90 Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names()));
aoqi@0 91
aoqi@0 92 resultsMeta =
aoqi@0 93 roundEnvironment.
aoqi@0 94 getElementsAnnotatedWith(elements.getTypeElement(annotatedElementInfo.annotationName()));
aoqi@0 95
aoqi@0 96 if (!resultsMeta.isEmpty())
aoqi@0 97 System.err.println("Results: " + resultsMeta);
aoqi@0 98
aoqi@0 99 if (resultsMeta.size() != annotatedElementInfo.expectedSize()) {
aoqi@0 100 failed = true;
aoqi@0 101 System.err.printf("Bad number of elements; expected %d, got %d%n",
aoqi@0 102 annotatedElementInfo.expectedSize(), resultsMeta.size());
aoqi@0 103 } else {
aoqi@0 104 for(Element element : resultsMeta) {
aoqi@0 105 String simpleName = element.getSimpleName().toString();
aoqi@0 106 if (!expectedNames.contains(simpleName) ) {
aoqi@0 107 failed = true;
aoqi@0 108 System.err.println("Name ``" + simpleName + "'' not expected.");
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111 }
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName());
aoqi@0 115
aoqi@0 116 if (!resultsMeta.equals(resultsBase)) {
aoqi@0 117 failed = true;
aoqi@0 118 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta +
aoqi@0 119 "\nbase: " + resultsBase);
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 if (failed) {
aoqi@0 123 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo);
aoqi@0 124 throw new RuntimeException();
aoqi@0 125 }
aoqi@0 126 } else {
aoqi@0 127 // If processing is over without an error, the specified
aoqi@0 128 // elements should be empty so an empty set should be returned.
aoqi@0 129 resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement);
aoqi@0 130 resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class);
aoqi@0 131 if (!resultsMeta.isEmpty())
aoqi@0 132 throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta);
aoqi@0 133 if (!resultsBase.isEmpty())
aoqi@0 134 throw new RuntimeException("Nonempty resultsBase: " + resultsBase);
aoqi@0 135
aoqi@0 136 }
aoqi@0 137 return true;
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) {
aoqi@0 141 try {
aoqi@0 142 return roundEnvironment.
aoqi@0 143 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class));
aoqi@0 144 } catch(ClassNotFoundException cnfe) {
aoqi@0 145 throw new RuntimeException(cnfe);
aoqi@0 146 }
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 /**
aoqi@0 150 * Verify non-annotation types result in
aoqi@0 151 * IllegalArgumentExceptions.
aoqi@0 152 */
aoqi@0 153 private void testNonAnnotations(RoundEnvironment roundEnvironment) {
aoqi@0 154 try {
aoqi@0 155 Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class );
aoqi@0 156 throw new RuntimeException("Illegal argument exception not thrown");
aoqi@0 157 } catch(IllegalArgumentException iae) {}
aoqi@0 158
aoqi@0 159 try {
aoqi@0 160 Set<? extends Element> elements =
aoqi@0 161 roundEnvironment.getElementsAnnotatedWith(processingEnv.
aoqi@0 162 getElementUtils().
aoqi@0 163 getTypeElement("java.lang.Object") );
aoqi@0 164 throw new RuntimeException("Illegal argument exception not thrown");
aoqi@0 165 } catch(IllegalArgumentException iae) {}
aoqi@0 166 }
aoqi@0 167 }

mercurial