1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,168 @@ 1.4 +/* 1.5 + * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6397298 6400986 6425592 6449798 6453386 6508401 1.30 + * @summary Tests that getElementsAnnotatedWith works properly. 1.31 + * @author Joseph D. Darcy 1.32 + * @compile TestElementsAnnotatedWith.java 1.33 + * @compile InheritedAnnotation.java 1.34 + * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java 1.35 + * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java 1.36 + * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java 1.37 + * @compile -processor TestElementsAnnotatedWith -proc:only TestElementsAnnotatedWith.java 1.38 + * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 1.39 + */ 1.40 + 1.41 +import java.lang.annotation.Annotation; 1.42 +import java.util.Collections; 1.43 +import java.util.Set; 1.44 +import java.util.HashSet; 1.45 +import java.util.Arrays; 1.46 +import javax.annotation.processing.*; 1.47 +import javax.lang.model.SourceVersion; 1.48 +import javax.lang.model.element.*; 1.49 +import javax.lang.model.util.*; 1.50 +import static javax.lang.model.util.ElementFilter.*; 1.51 + 1.52 +/** 1.53 + * This processor verifies that the information returned by 1.54 + * getElementsAnnotatedWith is consistent with the expected results 1.55 + * stored in an AnnotatedElementInfo annotation. 1.56 + */ 1.57 +@SupportedAnnotationTypes("*") 1.58 +@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) 1.59 +public class TestElementsAnnotatedWith extends AbstractProcessor { 1.60 + 1.61 + public boolean process(Set<? extends TypeElement> annotations, 1.62 + RoundEnvironment roundEnvironment) { 1.63 + Elements elementUtils = processingEnv.getElementUtils(); 1.64 + 1.65 + TypeElement annotatedElementInfoElement = 1.66 + elementUtils.getTypeElement("AnnotatedElementInfo"); 1.67 + Set<? extends Element> resultsMeta = Collections.emptySet(); 1.68 + Set<? extends Element> resultsBase = Collections.emptySet(); 1.69 + 1.70 + if (!roundEnvironment.processingOver()) { 1.71 + testNonAnnotations(roundEnvironment); 1.72 + 1.73 + // Verify AnnotatedElementInfo is present on the first 1.74 + // specified type. 1.75 + 1.76 + TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next(); 1.77 + 1.78 + AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class); 1.79 + 1.80 + boolean failed = false; 1.81 + 1.82 + if (annotatedElementInfo == null) 1.83 + throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " + 1.84 + firstType); 1.85 + else { 1.86 + // Verify that the annotation information is as 1.87 + // expected. 1.88 + 1.89 + Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names())); 1.90 + 1.91 + resultsMeta = 1.92 + roundEnvironment. 1.93 + getElementsAnnotatedWith(elementUtils. 1.94 + getTypeElement(annotatedElementInfo. 1.95 + annotationName())) ; 1.96 + 1.97 + System.err.println("Results: " + resultsMeta); 1.98 + 1.99 + if (resultsMeta.size() != annotatedElementInfo.expectedSize()) { 1.100 + failed = true; 1.101 + System.err.printf("Bad number of elements; expected %d, got %d%n", 1.102 + annotatedElementInfo.expectedSize(), resultsMeta.size()); 1.103 + } else { 1.104 + for(Element element : resultsMeta) { 1.105 + String simpleName = element.getSimpleName().toString(); 1.106 + if (!expectedNames.contains(simpleName) ) { 1.107 + failed = true; 1.108 + System.err.println("Name ``" + simpleName + "'' not expected."); 1.109 + } 1.110 + } 1.111 + } 1.112 + } 1.113 + 1.114 + resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName()); 1.115 + 1.116 + if (!resultsMeta.equals(resultsBase)) { 1.117 + failed = true; 1.118 + System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + 1.119 + "\nbase: " + resultsBase); 1.120 + } 1.121 + 1.122 + if (failed) { 1.123 + System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); 1.124 + throw new RuntimeException(); 1.125 + } 1.126 + } else { 1.127 + // If processing is over without an error, the specified 1.128 + // elements should be empty so an empty set should be returned. 1.129 + resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement); 1.130 + resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class); 1.131 + if (!resultsMeta.isEmpty()) 1.132 + throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta); 1.133 + if (!resultsBase.isEmpty()) 1.134 + throw new RuntimeException("Nonempty resultsBase: " + resultsBase); 1.135 + 1.136 + } 1.137 + return true; 1.138 + } 1.139 + 1.140 + private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) { 1.141 + try { 1.142 + return roundEnvironment. 1.143 + getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); 1.144 + } catch(ClassNotFoundException cnfe) { 1.145 + throw new RuntimeException(cnfe); 1.146 + } 1.147 + } 1.148 + 1.149 + /** 1.150 + * Verify non-annotation types result in 1.151 + * IllegalArgumentExceptions. 1.152 + */ 1.153 + private void testNonAnnotations(RoundEnvironment roundEnvironment) { 1.154 + try { 1.155 + Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class ); 1.156 + throw new RuntimeException("Illegal argument exception not thrown"); 1.157 + } catch(IllegalArgumentException iae) {} 1.158 + 1.159 + try { 1.160 + Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(processingEnv. 1.161 + getElementUtils(). 1.162 + getTypeElement("java.lang.Object") ); 1.163 + throw new RuntimeException("Illegal argument exception not thrown"); 1.164 + } catch(IllegalArgumentException iae) {} 1.165 + } 1.166 + 1.167 + @Override 1.168 + public SourceVersion getSupportedSourceVersion() { 1.169 + return SourceVersion.latest(); 1.170 + } 1.171 +}