1 /* 2 * Copyright (c) 2006, 2014, 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 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/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java 41 * @compile Foo.java 42 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo 43 */ 44 45 import java.lang.annotation.Annotation; 46 import java.util.Collections; 47 import java.util.Set; 48 import java.util.HashSet; 49 import java.util.Arrays; 50 import javax.annotation.processing.*; 51 import javax.lang.model.element.*; 52 import static javax.lang.model.util.ElementFilter.*; 53 54 /** 55 * This processor verifies that the information returned by 56 * getElementsAnnotatedWith is consistent with the expected results 57 * stored in an AnnotatedElementInfo annotation. 58 */ 59 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) 60 public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor { 61 62 public boolean process(Set<? extends TypeElement> annotations, 63 RoundEnvironment roundEnvironment) { 64 TypeElement annotatedElementInfoElement = 65 elements.getTypeElement("AnnotatedElementInfo"); 66 Set<? extends Element> resultsMeta = Collections.emptySet(); 67 Set<? extends Element> resultsBase = Collections.emptySet(); 68 69 if (!roundEnvironment.processingOver()) { 70 testNonAnnotations(roundEnvironment); 71 72 // Verify AnnotatedElementInfo is present on the first 73 // specified type. 74 75 TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next(); 76 77 AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class); 78 79 boolean failed = false; 80 81 if (annotatedElementInfo == null) 82 throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " + 83 firstType); 84 else { 85 // Verify that the annotation information is as 86 // expected. 87 88 Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names())); 89 90 resultsMeta = 91 roundEnvironment. 92 getElementsAnnotatedWith(elements.getTypeElement(annotatedElementInfo.annotationName())); 93 94 System.err.println("Results: " + resultsMeta); 95 96 if (resultsMeta.size() != annotatedElementInfo.expectedSize()) { 97 failed = true; 98 System.err.printf("Bad number of elements; expected %d, got %d%n", 99 annotatedElementInfo.expectedSize(), resultsMeta.size()); 100 } else { 101 for(Element element : resultsMeta) { 102 String simpleName = element.getSimpleName().toString(); 103 if (!expectedNames.contains(simpleName) ) { 104 failed = true; 105 System.err.println("Name ``" + simpleName + "'' not expected."); 106 } 107 } 108 } 109 } 110 111 resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName()); 112 113 if (!resultsMeta.equals(resultsBase)) { 114 failed = true; 115 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + 116 "\nbase: " + resultsBase); 117 } 118 119 if (failed) { 120 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); 121 throw new RuntimeException(); 122 } 123 } else { 124 // If processing is over without an error, the specified 125 // elements should be empty so an empty set should be returned. 126 resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement); 127 resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class); 128 if (!resultsMeta.isEmpty()) 129 throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta); 130 if (!resultsBase.isEmpty()) 131 throw new RuntimeException("Nonempty resultsBase: " + resultsBase); 132 133 } 134 return true; 135 } 136 137 private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) { 138 try { 139 return roundEnvironment. 140 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); 141 } catch(ClassNotFoundException cnfe) { 142 throw new RuntimeException(cnfe); 143 } 144 } 145 146 /** 147 * Verify non-annotation types result in 148 * IllegalArgumentExceptions. 149 */ 150 private void testNonAnnotations(RoundEnvironment roundEnvironment) { 151 try { 152 Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class ); 153 throw new RuntimeException("Illegal argument exception not thrown"); 154 } catch(IllegalArgumentException iae) {} 155 156 try { 157 Set<? extends Element> elements = 158 roundEnvironment.getElementsAnnotatedWith(processingEnv. 159 getElementUtils(). 160 getTypeElement("java.lang.Object") ); 161 throw new RuntimeException("Illegal argument exception not thrown"); 162 } catch(IllegalArgumentException iae) {} 163 } 164 }