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

Print this page
rev 224 : 6498938: Faulty comparison of TypeMirror objects in getElementsAnnotatedWith implementation
Reviewed-by: jjg
   1 /*
   2  * Copyright 2006-2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6397298 6400986 6425592 6449798 6453386 6508401
  27  * @summary Tests that getElementsAnnotatedWith works properly.
  28  * @author  Joseph D. Darcy
  29  * @compile TestElementsAnnotatedWith.java
  30  * @compile InheritedAnnotation.java
  31  * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
  32  * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
  33  * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java
  34  * @compile -processor TestElementsAnnotatedWith -proc:only TestElementsAnnotatedWith.java
  35  * @compile -processor TestElementsAnnotatedWith -proc:only C2.java



  36  */
  37 
  38 import java.lang.annotation.Annotation;

  39 import java.util.Collections;
  40 import java.util.Set;
  41 import java.util.HashSet;


  42 import java.util.Arrays;
  43 import javax.annotation.processing.*;

  44 import javax.lang.model.SourceVersion;
  45 import javax.lang.model.element.*;
  46 import javax.lang.model.util.*;
  47 import static javax.lang.model.util.ElementFilter.*;
  48 
  49 /**
  50  * This processor verifies that the information returned by
  51  * getElementsAnnotatedWith is consistent with the expected results
  52  * stored in an AnnotatedElementInfo annotation.
  53  */
  54 @SupportedAnnotationTypes("*")
  55 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
  56 public class TestElementsAnnotatedWith extends AbstractProcessor {
  57 
  58     public boolean process(Set<? extends TypeElement> annotations,
  59                            RoundEnvironment roundEnvironment) {
  60         Elements elementUtils = processingEnv.getElementUtils();
  61 
  62         TypeElement annotatedElementInfoElement =
  63             elementUtils.getTypeElement("AnnotatedElementInfo");


 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);


 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 = roundEnvironment.getElementsAnnotatedWith(processingEnv.
 158                                                                                         getElementUtils().
 159                                                                                         getTypeElement("java.lang.Object") );
 160             throw new RuntimeException("Illegal argument exception not thrown");
 161         } catch(IllegalArgumentException iae) {}
 162     }
 163 































 164     @Override
 165     public SourceVersion getSupportedSourceVersion() {
 166         return SourceVersion.latest();
 167     }
 168 }
   1 /*
   2  * Copyright 2006-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938
  27  * @summary Tests that getElementsAnnotatedWith works properly.
  28  * @author  Joseph D. Darcy
  29  * @compile TestElementsAnnotatedWith.java
  30  * @compile InheritedAnnotation.java
  31  * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java
  32  * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java
  33  * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java

  34  * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
  35  * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
  36  * @compile -XD-d=. Foo.java
  37  * @compile -processor TestElementsAnnotatedWith -proc:only TestElementsAnnotatedWith.java
  38  */
  39 
  40 import java.lang.annotation.Annotation;
  41 import java.io.*;
  42 import java.util.Collections;
  43 import java.util.Set;
  44 import java.util.HashSet;
  45 import java.util.List;
  46 import java.util.ArrayList;
  47 import java.util.Arrays;
  48 import javax.annotation.processing.*;
  49 import javax.tools.*;
  50 import javax.lang.model.SourceVersion;
  51 import javax.lang.model.element.*;
  52 import javax.lang.model.util.*;
  53 import static javax.lang.model.util.ElementFilter.*;
  54 
  55 /**
  56  * This processor verifies that the information returned by
  57  * getElementsAnnotatedWith is consistent with the expected results
  58  * stored in an AnnotatedElementInfo annotation.
  59  */
  60 @SupportedAnnotationTypes("*")
  61 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", expectedSize=0, names={})
  62 public class TestElementsAnnotatedWith extends AbstractProcessor {
  63 
  64     public boolean process(Set<? extends TypeElement> annotations,
  65                            RoundEnvironment roundEnvironment) {
  66         Elements elementUtils = processingEnv.getElementUtils();
  67 
  68         TypeElement annotatedElementInfoElement =
  69             elementUtils.getTypeElement("AnnotatedElementInfo");


 109                         if (!expectedNames.contains(simpleName) ) {
 110                             failed = true;
 111                             System.err.println("Name ``" + simpleName + "'' not expected.");
 112                         }
 113                     }
 114                 }
 115             }
 116 
 117             resultsBase = computeResultsBase(roundEnvironment, annotatedElementInfo.annotationName());
 118 
 119             if (!resultsMeta.equals(resultsBase)) {
 120                 failed = true;
 121                 System.err.println("Base and Meta sets unequal;\n meta: " + resultsMeta +
 122                                    "\nbase: " + resultsBase);
 123             }
 124 
 125             if (failed) {
 126                 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo);
 127                 throw new RuntimeException();
 128             }
 129 
 130             if("TestElementsAnnotatedWith".equals(firstType.getSimpleName().toString()))
 131                writeClassFile(); // Start another round to test class file input
 132         } else {
 133             // If processing is over without an error, the specified
 134             // elements should be empty so an empty set should be returned.
 135             resultsMeta = roundEnvironment.getElementsAnnotatedWith(annotatedElementInfoElement);
 136             resultsBase = roundEnvironment.getElementsAnnotatedWith(AnnotatedElementInfo.class);
 137             if (!resultsMeta.isEmpty())
 138                 throw new RuntimeException("Nonempty resultsMeta: " + resultsMeta);
 139             if (!resultsBase.isEmpty())
 140                 throw new RuntimeException("Nonempty resultsBase: " + resultsBase);
 141 
 142         }
 143         return true;
 144     }
 145 
 146     private Set<? extends Element> computeResultsBase(RoundEnvironment roundEnvironment, String name) {
 147         try {
 148             return roundEnvironment.
 149                 getElementsAnnotatedWith(Class.forName(name).asSubclass(Annotation.class));
 150         } catch(ClassNotFoundException cnfe) {
 151             throw new RuntimeException(cnfe);


 153     }
 154 
 155     /**
 156      * Verify non-annotation types result in
 157      * IllegalArgumentExceptions.
 158      */
 159     private void testNonAnnotations(RoundEnvironment roundEnvironment) {
 160         try {
 161             Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith((Class)Object.class );
 162             throw new RuntimeException("Illegal argument exception not thrown");
 163         } catch(IllegalArgumentException iae) {}
 164 
 165         try {
 166             Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(processingEnv.
 167                                                                                         getElementUtils().
 168                                                                                         getTypeElement("java.lang.Object") );
 169             throw new RuntimeException("Illegal argument exception not thrown");
 170         } catch(IllegalArgumentException iae) {}
 171     }
 172 
 173     /*
 174      * Hack alert!  The class file read below is generated by the
 175      * "@compile -XD-d=. Foo.java" directive above.  This sneakily
 176      * overrides the output location to the current directory where a
 177      * subsequent @compile can read the file.  This could be improved
 178      * if either a new directive like @process accepted class file
 179      * arguments (the javac command accepts such arguments but
 180      * @compile does not) or the test.src and test.classes properties
 181      * were set to be read with @compile jobs.
 182      */
 183     private void writeClassFile() {
 184         try {
 185             Filer filer = processingEnv.getFiler();
 186             JavaFileObject jfo = filer.createClassFile("Foo");
 187             OutputStream os = jfo.openOutputStream();
 188             // Copy the bytes over
 189             System.out.println((new File(".")).getAbsolutePath());
 190             InputStream io = new BufferedInputStream(new FileInputStream(new File(".", "Foo.class")));
 191             int datum = io.read();
 192             while(datum != -1) {
 193                 os.write(datum);
 194                 datum = io.read();
 195             }
 196             os.close();
 197         } catch (IOException io) {
 198             throw new RuntimeException(io);
 199         }
 200 
 201 
 202     }
 203 
 204     @Override
 205     public SourceVersion getSupportedSourceVersion() {
 206         return SourceVersion.latest();
 207     }
 208 }