Tue, 24 Dec 2013 09:17:37 -0800
8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com
1 /*
2 * Copyright (c) 2006, 2013, 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 TpAnno.java
34 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
35 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
36 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
37 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
38 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
39 * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
40 * @compile Foo.java
41 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
42 */
44 import java.lang.annotation.Annotation;
45 import java.io.*;
46 import java.util.Collections;
47 import java.util.Set;
48 import java.util.HashSet;
49 import java.util.List;
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import javax.annotation.processing.*;
53 import javax.tools.*;
54 import javax.lang.model.SourceVersion;
55 import javax.lang.model.element.*;
56 import javax.lang.model.util.*;
57 import static javax.lang.model.util.ElementFilter.*;
59 /**
60 * This processor verifies that the information returned by
61 * getElementsAnnotatedWith is consistent with the expected results
62 * stored in an AnnotatedElementInfo annotation.
63 */
64 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
65 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor {
67 public boolean process(Set<? extends TypeElement> annotations,
68 RoundEnvironment roundEnvironment) {
69 TypeElement annotatedElementInfoElement =
70 elements.getTypeElement("AnnotatedElementInfo");
71 Set<? extends Element> resultsMeta = Collections.emptySet();
72 Set<? extends Element> resultsBase = Collections.emptySet();
74 if (!roundEnvironment.processingOver()) {
75 testNonAnnotations(roundEnvironment);
77 // Verify AnnotatedElementInfo is present on the first
78 // specified type.
80 TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next();
82 AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class);
84 boolean failed = false;
86 if (annotatedElementInfo == null)
87 throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " +
88 firstType);
89 else {
90 // Verify that the annotation information is as
91 // expected.
93 Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names()));
95 resultsMeta =
96 roundEnvironment.
97 getElementsAnnotatedWith(elements.getTypeElement(annotatedElementInfo.annotationName()));
99 System.err.println("Results: " + resultsMeta);
101 if (resultsMeta.size() != annotatedElementInfo.expectedSize()) {
102 failed = true;
103 System.err.printf("Bad number of elements; expected %d, got %d%n",
104 annotatedElementInfo.expectedSize(), resultsMeta.size());
105 } else {
106 for(Element element : resultsMeta) {
107 String simpleName = element.getSimpleName().toString();
108 if (!expectedNames.contains(simpleName) ) {
109 failed = true;
110 System.err.println("Name ``" + simpleName + "'' not expected.");
111 }
112 }
113 }
114 }
116 resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName());
118 if (!resultsMeta.equals(resultsBase)) {
119 failed = true;
120 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta +
121 "\nbase: " + resultsBase);
122 }
124 if (failed) {
125 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo);
126 throw new RuntimeException();
127 }
128 } else {
129 // If processing is over without an error, the specified
130 // elements should be empty so an empty set should be returned.
131 resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement);
132 resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class);
133 if (!resultsMeta.isEmpty())
134 throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta);
135 if (!resultsBase.isEmpty())
136 throw new RuntimeException("Nonempty resultsBase: " + resultsBase);
138 }
139 return true;
140 }
142 private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) {
143 try {
144 return roundEnvironment.
145 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class));
146 } catch(ClassNotFoundException cnfe) {
147 throw new RuntimeException(cnfe);
148 }
149 }
151 /**
152 * Verify non-annotation types result in
153 * IllegalArgumentExceptions.
154 */
155 private void testNonAnnotations(RoundEnvironment roundEnvironment) {
156 try {
157 Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class );
158 throw new RuntimeException("Illegal argument exception not thrown");
159 } catch(IllegalArgumentException iae) {}
161 try {
162 Set<? extends Element> elements =
163 roundEnvironment.getElementsAnnotatedWith(processingEnv.
164 getElementUtils().
165 getTypeElement("java.lang.Object") );
166 throw new RuntimeException("Illegal argument exception not thrown");
167 } catch(IllegalArgumentException iae) {}
168 }
169 }