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

Wed, 27 May 2009 22:34:43 -0700

author
darcy
date
Wed, 27 May 2009 22:34:43 -0700
changeset 289
84061bd68019
parent 232
1fbc1cc6e260
child 395
5a72ba18c471
permissions
-rw-r--r--

6843761: Update langtools tests to remove unncessary -source and -target options
Reviewed-by: jjg

duke@1 1 /*
darcy@232 2 * Copyright 2006-2009 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
duke@1 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 21 * have any questions.
duke@1 22 */
duke@1 23
duke@1 24 /*
duke@1 25 * @test
darcy@232 26 * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938
duke@1 27 * @summary Tests that getElementsAnnotatedWith works properly.
duke@1 28 * @author Joseph D. Darcy
duke@1 29 * @compile TestElementsAnnotatedWith.java
duke@1 30 * @compile InheritedAnnotation.java
duke@1 31 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
duke@1 32 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
duke@1 33 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
darcy@232 34 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
darcy@232 35 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
darcy@232 36 * @compile -XD-d=. Foo.java
duke@1 37 * @compile -processor TestElementsAnnotatedWith -proc:only TestElementsAnnotatedWith.java
duke@1 38 */
duke@1 39
duke@1 40 import java.lang.annotation.Annotation;
darcy@232 41 import java.io.*;
duke@1 42 import java.util.Collections;
duke@1 43 import java.util.Set;
duke@1 44 import java.util.HashSet;
darcy@232 45 import java.util.List;
darcy@232 46 import java.util.ArrayList;
duke@1 47 import java.util.Arrays;
duke@1 48 import javax.annotation.processing.*;
darcy@232 49 import javax.tools.*;
duke@1 50 import javax.lang.model.SourceVersion;
duke@1 51 import javax.lang.model.element.*;
duke@1 52 import javax.lang.model.util.*;
duke@1 53 import static javax.lang.model.util.ElementFilter.*;
duke@1 54
duke@1 55 /**
duke@1 56 * This processor verifies that the information returned by
duke@1 57 * getElementsAnnotatedWith is consistent with the expected results
duke@1 58 * stored in an AnnotatedElementInfo annotation.
duke@1 59 */
duke@1 60 @SupportedAnnotationTypes("*")
duke@1 61 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
duke@1 62 public class TestElementsAnnotatedWith extends AbstractProcessor {
duke@1 63
duke@1 64 public boolean process(Set<? extends TypeElement> annotations,
duke@1 65 RoundEnvironment roundEnvironment) {
duke@1 66 Elements elementUtils = processingEnv.getElementUtils();
duke@1 67
duke@1 68 TypeElement annotatedElementInfoElement =
duke@1 69 elementUtils.getTypeElement("AnnotatedElementInfo");
duke@1 70 Set<? extends Element> resultsMeta = Collections.emptySet();
duke@1 71 Set<? extends Element> resultsBase = Collections.emptySet();
duke@1 72
duke@1 73 if (!roundEnvironment.processingOver()) {
duke@1 74 testNonAnnotations(roundEnvironment);
duke@1 75
duke@1 76 // Verify AnnotatedElementInfo is present on the first
duke@1 77 // specified type.
duke@1 78
duke@1 79 TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next();
duke@1 80
duke@1 81 AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class);
duke@1 82
duke@1 83 boolean failed = false;
duke@1 84
duke@1 85 if (annotatedElementInfo == null)
duke@1 86 throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " +
duke@1 87 firstType);
duke@1 88 else {
duke@1 89 // Verify that the annotation information is as
duke@1 90 // expected.
duke@1 91
duke@1 92 Set<String> expectedNames = new HashSet<String>(Arrays.asList(annotatedElementInfo.names()));
duke@1 93
duke@1 94 resultsMeta =
duke@1 95 roundEnvironment.
duke@1 96 getElementsAnnotatedWith(elementUtils.
duke@1 97 getTypeElement(annotatedElementInfo.
duke@1 98 annotationName())) ;
duke@1 99
duke@1 100 System.err.println("Results: " + resultsMeta);
duke@1 101
duke@1 102 if (resultsMeta.size() != annotatedElementInfo.expectedSize()) {
duke@1 103 failed = true;
duke@1 104 System.err.printf("Bad number of elements; expected %d, got %d%n",
duke@1 105 annotatedElementInfo.expectedSize(), resultsMeta.size());
duke@1 106 } else {
duke@1 107 for(Element element : resultsMeta) {
duke@1 108 String simpleName = element.getSimpleName().toString();
duke@1 109 if (!expectedNames.contains(simpleName) ) {
duke@1 110 failed = true;
duke@1 111 System.err.println("Name ``" + simpleName + "'' not expected.");
duke@1 112 }
duke@1 113 }
duke@1 114 }
duke@1 115 }
duke@1 116
duke@1 117 resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName());
duke@1 118
duke@1 119 if (!resultsMeta.equals(resultsBase)) {
duke@1 120 failed = true;
duke@1 121 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta +
duke@1 122 "\nbase: " + resultsBase);
duke@1 123 }
duke@1 124
duke@1 125 if (failed) {
duke@1 126 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo);
duke@1 127 throw new RuntimeException();
duke@1 128 }
darcy@232 129
darcy@232 130 if("TestElementsAnnotatedWith".equals(firstType.getSimpleName().toString()))
darcy@232 131 writeClassFile(); // Start another round to test class file input
duke@1 132 } else {
duke@1 133 // If processing is over without an error, the specified
duke@1 134 // elements should be empty so an empty set should be returned.
duke@1 135 resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement);
duke@1 136 resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class);
duke@1 137 if (!resultsMeta.isEmpty())
duke@1 138 throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta);
duke@1 139 if (!resultsBase.isEmpty())
duke@1 140 throw new RuntimeException("Nonempty resultsBase: " + resultsBase);
duke@1 141
duke@1 142 }
duke@1 143 return true;
duke@1 144 }
duke@1 145
duke@1 146 private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) {
duke@1 147 try {
duke@1 148 return roundEnvironment.
duke@1 149 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class));
duke@1 150 } catch(ClassNotFoundException cnfe) {
duke@1 151 throw new RuntimeException(cnfe);
duke@1 152 }
duke@1 153 }
duke@1 154
duke@1 155 /**
duke@1 156 * Verify non-annotation types result in
duke@1 157 * IllegalArgumentExceptions.
duke@1 158 */
duke@1 159 private void testNonAnnotations(RoundEnvironment roundEnvironment) {
duke@1 160 try {
duke@1 161 Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class );
duke@1 162 throw new RuntimeException("Illegal argument exception not thrown");
duke@1 163 } catch(IllegalArgumentException iae) {}
duke@1 164
duke@1 165 try {
duke@1 166 Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(processingEnv.
duke@1 167 getElementUtils().
duke@1 168 getTypeElement("java.lang.Object") );
duke@1 169 throw new RuntimeException("Illegal argument exception not thrown");
duke@1 170 } catch(IllegalArgumentException iae) {}
duke@1 171 }
duke@1 172
darcy@232 173 /*
darcy@232 174 * Hack alert! The class file read below is generated by the
darcy@232 175 * "@compile -XD-d=. Foo.java" directive above. This sneakily
darcy@232 176 * overrides the output location to the current directory where a
darcy@232 177 * subsequent @compile can read the file. This could be improved
darcy@232 178 * if either a new directive like @process accepted class file
darcy@232 179 * arguments (the javac command accepts such arguments but
darcy@232 180 * @compile does not) or the test.src and test.classes properties
darcy@232 181 * were set to be read with @compile jobs.
darcy@232 182 */
darcy@232 183 private void writeClassFile() {
darcy@232 184 try {
darcy@232 185 Filer filer = processingEnv.getFiler();
darcy@232 186 JavaFileObject jfo = filer.createClassFile("Foo");
darcy@232 187 OutputStream os = jfo.openOutputStream();
darcy@232 188 // Copy the bytes over
darcy@232 189 System.out.println((new File(".")).getAbsolutePath());
darcy@232 190 InputStream io = new BufferedInputStream(new FileInputStream(new File(".", "Foo.class")));
darcy@232 191 int datum = io.read();
darcy@232 192 while(datum != -1) {
darcy@232 193 os.write(datum);
darcy@232 194 datum = io.read();
darcy@232 195 }
darcy@232 196 os.close();
darcy@232 197 } catch (IOException io) {
darcy@232 198 throw new RuntimeException(io);
darcy@232 199 }
darcy@232 200
darcy@232 201
darcy@232 202 }
darcy@232 203
duke@1 204 @Override
duke@1 205 public SourceVersion getSupportedSourceVersion() {
duke@1 206 return SourceVersion.latest();
duke@1 207 }
duke@1 208 }

mercurial