Tue, 24 Feb 2009 17:48:53 -0800
6498938: Faulty comparison of TypeMirror objects in getElementsAnnotatedWith implementation
Reviewed-by: jjg
1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java Tue Feb 24 17:16:18 2009 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java Tue Feb 24 17:48:53 2009 -0800 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -111,6 +111,7 @@ 1.11 */ 1.12 public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { 1.13 Set<Element> result = Collections.emptySet(); 1.14 + Types typeUtil = processingEnv.getTypeUtils(); 1.15 if (a.getKind() != ElementKind.ANNOTATION_TYPE) 1.16 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); 1.17 1.18 @@ -122,7 +123,7 @@ 1.19 throw new AssertionError("Bad implementation type for " + tm); 1.20 1.21 ElementScanner6<Set<Element>, DeclaredType> scanner = 1.22 - new AnnotationSetScanner(result); 1.23 + new AnnotationSetScanner(result, typeUtil); 1.24 1.25 for (Element element : rootElements) 1.26 result = scanner.scan(element, annotationTypeElement); 1.27 @@ -135,9 +136,11 @@ 1.28 ElementScanner6<Set<Element>, DeclaredType> { 1.29 // Insertion-order preserving set 1.30 Set<Element> annotatedElements = new LinkedHashSet<Element>(); 1.31 + Types typeUtil; 1.32 1.33 - AnnotationSetScanner(Set<Element> defaultSet) { 1.34 + AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) { 1.35 super(defaultSet); 1.36 + this.typeUtil = typeUtil; 1.37 } 1.38 1.39 @Override 1.40 @@ -145,7 +148,7 @@ 1.41 java.util.List<? extends AnnotationMirror> annotationMirrors = 1.42 processingEnv.getElementUtils().getAllAnnotationMirrors(e); 1.43 for (AnnotationMirror annotationMirror : annotationMirrors) { 1.44 - if (annotationMirror.getAnnotationType().equals(p)) 1.45 + if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p)) 1.46 annotatedElements.add(e); 1.47 } 1.48 e.accept(this, p);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/processing/environment/round/Foo.java Tue Feb 24 17:48:53 2009 -0800 2.3 @@ -0,0 +1,27 @@ 2.4 +/* 2.5 + * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 2.24 + * have any questions. 2.25 + */ 2.26 + 2.27 +@AnnotatedElementInfo(annotationName="AnnotatedElementInfo", 2.28 + expectedSize=1, 2.29 + names="Foo") 2.30 +public class Foo {}
3.1 --- a/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Tue Feb 24 17:16:18 2009 -0800 3.2 +++ b/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Tue Feb 24 17:48:53 2009 -0800 3.3 @@ -1,5 +1,5 @@ 3.4 /* 3.5 - * Copyright 2006-2007 Sun Microsystems, Inc. All Rights Reserved. 3.6 + * Copyright 2006-2009 Sun Microsystems, Inc. All Rights Reserved. 3.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.8 * 3.9 * This code is free software; you can redistribute it and/or modify it 3.10 @@ -23,7 +23,7 @@ 3.11 3.12 /* 3.13 * @test 3.14 - * @bug 6397298 6400986 6425592 6449798 6453386 6508401 3.15 + * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 3.16 * @summary Tests that getElementsAnnotatedWith works properly. 3.17 * @author Joseph D. Darcy 3.18 * @compile TestElementsAnnotatedWith.java 3.19 @@ -31,16 +31,22 @@ 3.20 * @compile -processor TestElementsAnnotatedWith -proc:only SurfaceAnnotations.java 3.21 * @compile -processor TestElementsAnnotatedWith -proc:only BuriedAnnotations.java 3.22 * @compile -processor TestElementsAnnotatedWith -proc:only Part1.java Part2.java 3.23 + * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 3.24 + * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java 3.25 + * @compile -XD-d=. Foo.java 3.26 * @compile -processor TestElementsAnnotatedWith -proc:only TestElementsAnnotatedWith.java 3.27 - * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 3.28 */ 3.29 3.30 import java.lang.annotation.Annotation; 3.31 +import java.io.*; 3.32 import java.util.Collections; 3.33 import java.util.Set; 3.34 import java.util.HashSet; 3.35 +import java.util.List; 3.36 +import java.util.ArrayList; 3.37 import java.util.Arrays; 3.38 import javax.annotation.processing.*; 3.39 +import javax.tools.*; 3.40 import javax.lang.model.SourceVersion; 3.41 import javax.lang.model.element.*; 3.42 import javax.lang.model.util.*; 3.43 @@ -120,6 +126,9 @@ 3.44 System.err.println("AnnotatedElementInfo: " + annotatedElementInfo); 3.45 throw new RuntimeException(); 3.46 } 3.47 + 3.48 + if("TestElementsAnnotatedWith".equals(firstType.getSimpleName().toString())) 3.49 + writeClassFile(); // Start another round to test class file input 3.50 } else { 3.51 // If processing is over without an error, the specified 3.52 // elements should be empty so an empty set should be returned. 3.53 @@ -161,6 +170,37 @@ 3.54 } catch(IllegalArgumentException iae) {} 3.55 } 3.56 3.57 + /* 3.58 + * Hack alert! The class file read below is generated by the 3.59 + * "@compile -XD-d=. Foo.java" directive above. This sneakily 3.60 + * overrides the output location to the current directory where a 3.61 + * subsequent @compile can read the file. This could be improved 3.62 + * if either a new directive like @process accepted class file 3.63 + * arguments (the javac command accepts such arguments but 3.64 + * @compile does not) or the test.src and test.classes properties 3.65 + * were set to be read with @compile jobs. 3.66 + */ 3.67 + private void writeClassFile() { 3.68 + try { 3.69 + Filer filer = processingEnv.getFiler(); 3.70 + JavaFileObject jfo = filer.createClassFile("Foo"); 3.71 + OutputStream os = jfo.openOutputStream(); 3.72 + // Copy the bytes over 3.73 + System.out.println((new File(".")).getAbsolutePath()); 3.74 + InputStream io = new BufferedInputStream(new FileInputStream(new File(".", "Foo.class"))); 3.75 + int datum = io.read(); 3.76 + while(datum != -1) { 3.77 + os.write(datum); 3.78 + datum = io.read(); 3.79 + } 3.80 + os.close(); 3.81 + } catch (IOException io) { 3.82 + throw new RuntimeException(io); 3.83 + } 3.84 + 3.85 + 3.86 + } 3.87 + 3.88 @Override 3.89 public SourceVersion getSupportedSourceVersion() { 3.90 return SourceVersion.latest();