duke@1: /* darcy@232: * Copyright 2006-2009 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: /* duke@1: * @test darcy@232: * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 duke@1: * @summary Tests that getElementsAnnotatedWith works properly. duke@1: * @author Joseph D. Darcy duke@1: * @compile TestElementsAnnotatedWith.java duke@1: * @compile InheritedAnnotation.java duke@1: * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java duke@1: * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java duke@1: * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java darcy@232: * @compile -processor TestElementsAnnotatedWith -proc:only C2.java darcy@232: * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java darcy@232: * @compile -XD-d=. Foo.java duke@1: * @compile -processor TestElementsAnnotatedWith -proc:only TestElementsAnnotatedWith.java duke@1: */ duke@1: duke@1: import java.lang.annotation.Annotation; darcy@232: import java.io.*; duke@1: import java.util.Collections; duke@1: import java.util.Set; duke@1: import java.util.HashSet; darcy@232: import java.util.List; darcy@232: import java.util.ArrayList; duke@1: import java.util.Arrays; duke@1: import javax.annotation.processing.*; darcy@232: import javax.tools.*; duke@1: import javax.lang.model.SourceVersion; duke@1: import javax.lang.model.element.*; duke@1: import javax.lang.model.util.*; duke@1: import static javax.lang.model.util.ElementFilter.*; duke@1: duke@1: /** duke@1: * This processor verifies that the information returned by duke@1: * getElementsAnnotatedWith is consistent with the expected results duke@1: * stored in an AnnotatedElementInfo annotation. duke@1: */ duke@1: @SupportedAnnotationTypes("*") duke@1: @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={}) duke@1: public class TestElementsAnnotatedWith extends AbstractProcessor { duke@1: duke@1: public boolean process(Set annotations, duke@1: RoundEnvironment roundEnvironment) { duke@1: Elements elementUtils = processingEnv.getElementUtils(); duke@1: duke@1: TypeElement annotatedElementInfoElement = duke@1: elementUtils.getTypeElement("AnnotatedElementInfo"); duke@1: Set resultsMeta = Collections.emptySet(); duke@1: Set resultsBase = Collections.emptySet(); duke@1: duke@1: if (!roundEnvironment.processingOver()) { duke@1: testNonAnnotations(roundEnvironment); duke@1: duke@1: // Verify AnnotatedElementInfo is present on the first duke@1: // specified type. duke@1: duke@1: TypeElement firstType = typesIn(roundEnvironment.getRootElements()).iterator().next(); duke@1: duke@1: AnnotatedElementInfo annotatedElementInfo = firstType.getAnnotation(AnnotatedElementInfo.class); duke@1: duke@1: boolean failed = false; duke@1: duke@1: if (annotatedElementInfo == null) duke@1: throw new IllegalArgumentException("Missing AnnotatedElementInfo annotation on " + duke@1: firstType); duke@1: else { duke@1: // Verify that the annotation information is as duke@1: // expected. duke@1: duke@1: Set expectedNames = new HashSet(Arrays.asList(annotatedElementInfo.names())); duke@1: duke@1: resultsMeta = duke@1: roundEnvironment. duke@1: getElementsAnnotatedWith(elementUtils. duke@1: getTypeElement(annotatedElementInfo. duke@1: annotationName())) ; duke@1: duke@1: System.err.println("Results: " + resultsMeta); duke@1: duke@1: if (resultsMeta.size() != annotatedElementInfo.expectedSize()) { duke@1: failed = true; duke@1: System.err.printf("Bad number of elements; expected %d, got %d%n", duke@1: annotatedElementInfo.expectedSize(), resultsMeta.size()); duke@1: } else { duke@1: for(Element element : resultsMeta) { duke@1: String simpleName = element.getSimpleName().toString(); duke@1: if (!expectedNames.contains(simpleName) ) { duke@1: failed = true; duke@1: System.err.println("Name ``" + simpleName + "'' not expected."); duke@1: } duke@1: } duke@1: } duke@1: } duke@1: duke@1: resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName()); duke@1: duke@1: if (!resultsMeta.equals(resultsBase)) { duke@1: failed = true; duke@1: System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta + duke@1: "\nbase: " + resultsBase); duke@1: } duke@1: duke@1: if (failed) { duke@1: System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); duke@1: throw new RuntimeException(); duke@1: } darcy@232: darcy@232: if("TestElementsAnnotatedWith".equals(firstType.getSimpleName().toString())) darcy@232: writeClassFile(); // Start another round to test class file input duke@1: } else { duke@1: // If processing is over without an error, the specified duke@1: // elements should be empty so an empty set should be returned. duke@1: resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement); duke@1: resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class); duke@1: if (!resultsMeta.isEmpty()) duke@1: throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta); duke@1: if (!resultsBase.isEmpty()) duke@1: throw new RuntimeException("Nonempty resultsBase: " + resultsBase); duke@1: duke@1: } duke@1: return true; duke@1: } duke@1: duke@1: private Set computeResultsBase(RoundEnvironment roundEnvironment, String name) { duke@1: try { duke@1: return roundEnvironment. duke@1: getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class)); duke@1: } catch(ClassNotFoundException cnfe) { duke@1: throw new RuntimeException(cnfe); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Verify non-annotation types result in duke@1: * IllegalArgumentExceptions. duke@1: */ duke@1: private void testNonAnnotations(RoundEnvironment roundEnvironment) { duke@1: try { duke@1: Set elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class ); duke@1: throw new RuntimeException("Illegal argument exception not thrown"); duke@1: } catch(IllegalArgumentException iae) {} duke@1: duke@1: try { duke@1: Set elements = roundEnvironment.getElementsAnnotatedWith(processingEnv. duke@1: getElementUtils(). duke@1: getTypeElement("java.lang.Object") ); duke@1: throw new RuntimeException("Illegal argument exception not thrown"); duke@1: } catch(IllegalArgumentException iae) {} duke@1: } duke@1: darcy@232: /* darcy@232: * Hack alert! The class file read below is generated by the darcy@232: * "@compile -XD-d=. Foo.java" directive above. This sneakily darcy@232: * overrides the output location to the current directory where a darcy@232: * subsequent @compile can read the file. This could be improved darcy@232: * if either a new directive like @process accepted class file darcy@232: * arguments (the javac command accepts such arguments but darcy@232: * @compile does not) or the test.src and test.classes properties darcy@232: * were set to be read with @compile jobs. darcy@232: */ darcy@232: private void writeClassFile() { darcy@232: try { darcy@232: Filer filer = processingEnv.getFiler(); darcy@232: JavaFileObject jfo = filer.createClassFile("Foo"); darcy@232: OutputStream os = jfo.openOutputStream(); darcy@232: // Copy the bytes over darcy@232: System.out.println((new File(".")).getAbsolutePath()); darcy@232: InputStream io = new BufferedInputStream(new FileInputStream(new File(".", "Foo.class"))); darcy@232: int datum = io.read(); darcy@232: while(datum != -1) { darcy@232: os.write(datum); darcy@232: datum = io.read(); darcy@232: } darcy@232: os.close(); darcy@232: } catch (IOException io) { darcy@232: throw new RuntimeException(io); darcy@232: } darcy@232: darcy@232: darcy@232: } darcy@232: duke@1: @Override duke@1: public SourceVersion getSupportedSourceVersion() { duke@1: return SourceVersion.latest(); duke@1: } duke@1: }