1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,167 @@ 1.4 +/* 1.5 + * Copyright (c) 2006, 2014, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049 8038080 1.30 + * @summary Tests that getElementsAnnotatedWith works properly. 1.31 + * @author Joseph D. Darcy 1.32 + * @library /tools/javac/lib 1.33 + * @build JavacTestingAbstractProcessor 1.34 + * @compile TestElementsAnnotatedWith.java 1.35 + * @compile InheritedAnnotation.java 1.36 + * @compile TpAnno.java 1.37 + * @compile Anno.java 1.38 + * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java 1.39 + * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java 1.40 + * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java 1.41 + * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 1.42 + * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java 1.43 + * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java 1.44 + * @compile -processor TestElementsAnnotatedWith -proc:only ParameterAnnotations.java 1.45 + * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java 1.46 + * @compile Foo.java 1.47 + * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo 1.48 + */ 1.49 + 1.50 +import java.lang.annotation.Annotation; 1.51 +import java.util.Collections; 1.52 +import java.util.Set; 1.53 +import java.util.HashSet; 1.54 +import java.util.Arrays; 1.55 +import javax.annotation.processing.*; 1.56 +import javax.lang.model.element.*; 1.57 +import static javax.lang.model.util.ElementFilter.*; 1.58 + 1.59 +/** 1.60 + * This processor verifies that the information returned by 1.61 + * getElementsAnnotatedWith is consistent with the expected results 1.62 + * stored in an AnnotatedElementInfo annotation. 1.63 + */ 1.64 +@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) 1.65 +public class TestElementsAnnotatedWith extends JavacTestingAbstractProcessor { 1.66 + 1.67 + public boolean process(Set<? extends TypeElement> annotations, 1.68 + RoundEnvironment roundEnvironment) { 1.69 + TypeElement annotatedElementInfoElement = 1.70 + elements.getTypeElement("AnnotatedElementInfo"); 1.71 + Set<? extends Element> resultsMeta = Collections.emptySet(); 1.72 + Set<? extends Element> resultsBase = Collections.emptySet(); 1.73 + 1.74 + if (!roundEnvironment.processingOver()) { 1.75 + testNonAnnotations(roundEnvironment); 1.76 + 1.77 + // Verify AnnotatedElementInfo is present on the first 1.78 + // specified type. 1.79 + 1.80 + TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next(); 1.81 + 1.82 + AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class); 1.83 + 1.84 + boolean failed = false; 1.85 + 1.86 + if (annotatedElementInfo == null) 1.87 + throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " + 1.88 + firstType); 1.89 + else { 1.90 + // Verify that the annotation information is as 1.91 + // expected. 1.92 + 1.93 + Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names())); 1.94 + 1.95 + resultsMeta = 1.96 + roundEnvironment. 1.97 + getElementsAnnotatedWith(elements.getTypeElement(annotatedElementInfo.annotationName())); 1.98 + 1.99 + if (!resultsMeta.isEmpty()) 1.100 + System.err.println("Results: " + resultsMeta); 1.101 + 1.102 + if (resultsMeta.size() != annotatedElementInfo.expectedSize()) { 1.103 + failed = true; 1.104 + System.err.printf("Bad number of elements; expected %d, got %d%n", 1.105 + annotatedElementInfo.expectedSize(), resultsMeta.size()); 1.106 + } else { 1.107 + for(Element element : resultsMeta) { 1.108 + String simpleName = element.getSimpleName().toString(); 1.109 + if (!expectedNames.contains(simpleName) ) { 1.110 + failed = true; 1.111 + System.err.println("Name ``" + simpleName + "'' not expected."); 1.112 + } 1.113 + } 1.114 + } 1.115 + } 1.116 + 1.117 + resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName()); 1.118 + 1.119 + if (!resultsMeta.equals(resultsBase)) { 1.120 + failed = true; 1.121 + System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + 1.122 + "\nbase: " + resultsBase); 1.123 + } 1.124 + 1.125 + if (failed) { 1.126 + System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); 1.127 + throw new RuntimeException(); 1.128 + } 1.129 + } else { 1.130 + // If processing is over without an error, the specified 1.131 + // elements should be empty so an empty set should be returned. 1.132 + resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement); 1.133 + resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class); 1.134 + if (!resultsMeta.isEmpty()) 1.135 + throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta); 1.136 + if (!resultsBase.isEmpty()) 1.137 + throw new RuntimeException("Nonempty resultsBase: " + resultsBase); 1.138 + 1.139 + } 1.140 + return true; 1.141 + } 1.142 + 1.143 + private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) { 1.144 + try { 1.145 + return roundEnvironment. 1.146 + getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); 1.147 + } catch(ClassNotFoundException cnfe) { 1.148 + throw new RuntimeException(cnfe); 1.149 + } 1.150 + } 1.151 + 1.152 + /** 1.153 + * Verify non-annotation types result in 1.154 + * IllegalArgumentExceptions. 1.155 + */ 1.156 + private void testNonAnnotations(RoundEnvironment roundEnvironment) { 1.157 + try { 1.158 + Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class ); 1.159 + throw new RuntimeException("Illegal argument exception not thrown"); 1.160 + } catch(IllegalArgumentException iae) {} 1.161 + 1.162 + try { 1.163 + Set<? extends Element> elements = 1.164 + roundEnvironment.getElementsAnnotatedWith(processingEnv. 1.165 + getElementUtils(). 1.166 + getTypeElement("java.lang.Object") ); 1.167 + throw new RuntimeException("Illegal argument exception not thrown"); 1.168 + } catch(IllegalArgumentException iae) {} 1.169 + } 1.170 +}