Fri, 10 Jan 2014 19:02:54 +0100
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
Summary: Match the required and actual annotations using Element equivalence rather than TypeMirror equivalence, to avoid trouble with erroneous types.
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java Mon Jan 27 21:15:39 2014 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java Fri Jan 10 19:02:54 2014 +0100 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2005, 2014, Oracle and/or its affiliates. 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 @@ -26,11 +26,8 @@ 1.11 package com.sun.tools.javac.processing; 1.12 1.13 import java.lang.annotation.Annotation; 1.14 -import com.sun.tools.javac.tree.JCTree.*; 1.15 import javax.annotation.processing.*; 1.16 import javax.lang.model.element.*; 1.17 -import javax.lang.model.type.DeclaredType; 1.18 -import javax.lang.model.type.TypeMirror; 1.19 import javax.lang.model.util.*; 1.20 import java.util.*; 1.21 1.22 @@ -114,58 +111,48 @@ 1.23 */ 1.24 public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { 1.25 Set<Element> result = Collections.emptySet(); 1.26 - Types typeUtil = processingEnv.getTypeUtils(); 1.27 if (a.getKind() != ElementKind.ANNOTATION_TYPE) 1.28 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); 1.29 1.30 - DeclaredType annotationTypeElement; 1.31 - TypeMirror tm = a.asType(); 1.32 - if ( tm instanceof DeclaredType ) 1.33 - annotationTypeElement = (DeclaredType) a.asType(); 1.34 - else 1.35 - throw new AssertionError("Bad implementation type for " + tm); 1.36 - 1.37 - ElementScanner8<Set<Element>, DeclaredType> scanner = 1.38 - new AnnotationSetScanner(result, typeUtil); 1.39 + ElementScanner8<Set<Element>, TypeElement> scanner = 1.40 + new AnnotationSetScanner(result); 1.41 1.42 for (Element element : rootElements) 1.43 - result = scanner.scan(element, annotationTypeElement); 1.44 + result = scanner.scan(element, a); 1.45 1.46 return result; 1.47 } 1.48 1.49 // Could be written as a local class inside getElementsAnnotatedWith 1.50 private class AnnotationSetScanner extends 1.51 - ElementScanner8<Set<Element>, DeclaredType> { 1.52 + ElementScanner8<Set<Element>, TypeElement> { 1.53 // Insertion-order preserving set 1.54 Set<Element> annotatedElements = new LinkedHashSet<Element>(); 1.55 - Types typeUtil; 1.56 1.57 - AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) { 1.58 + AnnotationSetScanner(Set<Element> defaultSet) { 1.59 super(defaultSet); 1.60 - this.typeUtil = typeUtil; 1.61 } 1.62 1.63 @Override 1.64 - public Set<Element> visitType(TypeElement e, DeclaredType p) { 1.65 + public Set<Element> visitType(TypeElement e, TypeElement p) { 1.66 // Type parameters are not considered to be enclosed by a type 1.67 scan(e.getTypeParameters(), p); 1.68 return scan(e.getEnclosedElements(), p); 1.69 } 1.70 1.71 @Override 1.72 - public Set<Element> visitExecutable(ExecutableElement e, DeclaredType p) { 1.73 + public Set<Element> visitExecutable(ExecutableElement e, TypeElement p) { 1.74 // Type parameters are not considered to be enclosed by an executable 1.75 scan(e.getTypeParameters(), p); 1.76 return scan(e.getEnclosedElements(), p); 1.77 } 1.78 1.79 @Override 1.80 - public Set<Element> scan(Element e, DeclaredType p) { 1.81 + public Set<Element> scan(Element e, TypeElement p) { 1.82 java.util.List<? extends AnnotationMirror> annotationMirrors = 1.83 processingEnv.getElementUtils().getAllAnnotationMirrors(e); 1.84 for (AnnotationMirror annotationMirror : annotationMirrors) { 1.85 - if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p)) 1.86 + if (p.equals(annotationMirror.getAnnotationType().asElement())) 1.87 annotatedElements.add(e); 1.88 } 1.89 e.accept(this, p);
2.1 --- a/test/tools/javac/processing/environment/round/BuriedAnnotations.java Mon Jan 27 21:15:39 2014 +0000 2.2 +++ b/test/tools/javac/processing/environment/round/BuriedAnnotations.java Fri Jan 10 19:02:54 2014 +0100 2.3 @@ -1,5 +1,5 @@ 2.4 /* 2.5 - * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved. 2.6 + * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. 2.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.8 * 2.9 * This code is free software; you can redistribute it and/or modify it 2.10 @@ -22,7 +22,7 @@ 2.11 */ 2.12 2.13 /** 2.14 - * Class to hold annotations for ElementsAnnotatedWithTest. 2.15 + * Class to hold annotations for TestElementsAnnotatedWith. 2.16 */ 2.17 2.18 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/processing/environment/round/ErroneousAnnotations.java Fri Jan 10 19:02:54 2014 +0100 3.3 @@ -0,0 +1,12 @@ 3.4 +/** /nodynamiccopyright/ 3.5 + * Class to hold annotations for TestElementsAnnotatedWith. 3.6 + */ 3.7 + 3.8 +@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings", 3.9 + expectedSize=0, 3.10 + names={}) 3.11 +@Undefined 3.12 +public class ErroneousAnnotations { 3.13 + @Undefined 3.14 + private void foo() {return;} 3.15 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/processing/environment/round/ErroneousAnnotations.out Fri Jan 10 19:02:54 2014 +0100 4.3 @@ -0,0 +1,4 @@ 4.4 +ErroneousAnnotations.java:8:2: compiler.err.cant.resolve: kindname.class, Undefined, , 4.5 +ErroneousAnnotations.java:10:6: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, ErroneousAnnotations, null) 4.6 +2 errors 4.7 +Results: []
5.1 --- a/test/tools/javac/processing/environment/round/Part1.java Mon Jan 27 21:15:39 2014 +0000 5.2 +++ b/test/tools/javac/processing/environment/round/Part1.java Fri Jan 10 19:02:54 2014 +0100 5.3 @@ -1,5 +1,5 @@ 5.4 /* 5.5 - * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. 5.6 + * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. 5.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.8 * 5.9 * This code is free software; you can redistribute it and/or modify it 5.10 @@ -22,7 +22,7 @@ 5.11 */ 5.12 5.13 /** 5.14 - * Class to hold annotations for ElementsAnnotatedWithTest. 5.15 + * Class to hold annotations for TestElementsAnnotatedWith. 5.16 */ 5.17 5.18 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
6.1 --- a/test/tools/javac/processing/environment/round/Part2.java Mon Jan 27 21:15:39 2014 +0000 6.2 +++ b/test/tools/javac/processing/environment/round/Part2.java Fri Jan 10 19:02:54 2014 +0100 6.3 @@ -1,5 +1,5 @@ 6.4 /* 6.5 - * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. 6.6 + * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. 6.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.8 * 6.9 * This code is free software; you can redistribute it and/or modify it 6.10 @@ -22,7 +22,7 @@ 6.11 */ 6.12 6.13 /** 6.14 - * Class to hold annotations for ElementsAnnotatedWithTest. 6.15 + * Class to hold annotations for TestElementsAnnotatedWith. 6.16 */ 6.17 @SuppressWarnings("") 6.18 public class Part2 {
7.1 --- a/test/tools/javac/processing/environment/round/SurfaceAnnotations.java Mon Jan 27 21:15:39 2014 +0000 7.2 +++ b/test/tools/javac/processing/environment/round/SurfaceAnnotations.java Fri Jan 10 19:02:54 2014 +0100 7.3 @@ -1,5 +1,5 @@ 7.4 /* 7.5 - * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. 7.6 + * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. 7.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.8 * 7.9 * This code is free software; you can redistribute it and/or modify it 7.10 @@ -22,7 +22,7 @@ 7.11 */ 7.12 7.13 /** 7.14 - * Class to hold annotations for ElementsAnnotatedWithTest. 7.15 + * Class to hold annotations for TestElementsAnnotatedWith. 7.16 */ 7.17 7.18 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
8.1 --- a/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Mon Jan 27 21:15:39 2014 +0000 8.2 +++ b/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java Fri Jan 10 19:02:54 2014 +0100 8.3 @@ -1,5 +1,5 @@ 8.4 /* 8.5 - * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. 8.6 + * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. 8.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.8 * 8.9 * This code is free software; you can redistribute it and/or modify it 8.10 @@ -23,7 +23,7 @@ 8.11 8.12 /* 8.13 * @test 8.14 - * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8.15 + * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049 8.16 * @summary Tests that getElementsAnnotatedWith works properly. 8.17 * @author Joseph D. Darcy 8.18 * @library /tools/javac/lib 8.19 @@ -37,23 +37,18 @@ 8.20 * @compile -processor TestElementsAnnotatedWith -proc:only C2.java 8.21 * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java 8.22 * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java 8.23 + * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java 8.24 * @compile Foo.java 8.25 * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo 8.26 */ 8.27 8.28 import java.lang.annotation.Annotation; 8.29 -import java.io.*; 8.30 import java.util.Collections; 8.31 import java.util.Set; 8.32 import java.util.HashSet; 8.33 -import java.util.List; 8.34 -import java.util.ArrayList; 8.35 import java.util.Arrays; 8.36 import javax.annotation.processing.*; 8.37 -import javax.tools.*; 8.38 -import javax.lang.model.SourceVersion; 8.39 import javax.lang.model.element.*; 8.40 -import javax.lang.model.util.*; 8.41 import static javax.lang.model.util.ElementFilter.*; 8.42 8.43 /**
9.1 --- a/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java Mon Jan 27 21:15:39 2014 +0000 9.2 +++ b/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java Fri Jan 10 19:02:54 2014 +0100 9.3 @@ -1,5 +1,5 @@ 9.4 /* 9.5 - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 9.6 + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. 9.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.8 * 9.9 * This code is free software; you can redistribute it and/or modify it 9.10 @@ -22,7 +22,7 @@ 9.11 */ 9.12 9.13 /** 9.14 - * Class to hold annotations for ElementsAnnotatedWithTest. 9.15 + * Class to hold annotations for TestElementsAnnotatedWith. 9.16 */ 9.17 9.18 @AnnotatedElementInfo(annotationName="TpAnno",