8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
authorjlahoda
Fri, 10 Jan 2014 19:02:54 +0100
changeset 22441 05b907a2f359
parent 22440 d40c30326317
child 22442 8fd30fc4e3a3
child 22449 1fd6d4bec7dd
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
langtools/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java
langtools/test/tools/javac/processing/environment/round/BuriedAnnotations.java
langtools/test/tools/javac/processing/environment/round/ErroneousAnnotations.java
langtools/test/tools/javac/processing/environment/round/ErroneousAnnotations.out
langtools/test/tools/javac/processing/environment/round/Part1.java
langtools/test/tools/javac/processing/environment/round/Part2.java
langtools/test/tools/javac/processing/environment/round/SurfaceAnnotations.java
langtools/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java
langtools/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java
--- a/langtools/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java	Fri Jan 10 12:47:15 2014 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/processing/JavacRoundEnvironment.java	Fri Jan 10 19:02:54 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,11 +26,8 @@
 package com.sun.tools.javac.processing;
 
 import java.lang.annotation.Annotation;
-import com.sun.tools.javac.tree.JCTree.*;
 import javax.annotation.processing.*;
 import javax.lang.model.element.*;
-import javax.lang.model.type.DeclaredType;
-import javax.lang.model.type.TypeMirror;
 import javax.lang.model.util.*;
 import java.util.*;
 
@@ -114,58 +111,48 @@
      */
     public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) {
         Set<Element> result = Collections.emptySet();
-        Types typeUtil = processingEnv.getTypeUtils();
         if (a.getKind() != ElementKind.ANNOTATION_TYPE)
             throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
 
-        DeclaredType annotationTypeElement;
-        TypeMirror tm = a.asType();
-        if ( tm instanceof DeclaredType )
-            annotationTypeElement = (DeclaredType) a.asType();
-        else
-            throw new AssertionError("Bad implementation type for " + tm);
-
-        ElementScanner8<Set<Element>, DeclaredType> scanner =
-            new AnnotationSetScanner(result, typeUtil);
+        ElementScanner8<Set<Element>, TypeElement> scanner =
+            new AnnotationSetScanner(result);
 
         for (Element element : rootElements)
-            result = scanner.scan(element, annotationTypeElement);
+            result = scanner.scan(element, a);
 
         return result;
     }
 
     // Could be written as a local class inside getElementsAnnotatedWith
     private class AnnotationSetScanner extends
-        ElementScanner8<Set<Element>, DeclaredType> {
+        ElementScanner8<Set<Element>, TypeElement> {
         // Insertion-order preserving set
         Set<Element> annotatedElements = new LinkedHashSet<>();
-        Types typeUtil;
 
-        AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) {
+        AnnotationSetScanner(Set<Element> defaultSet) {
             super(defaultSet);
-            this.typeUtil = typeUtil;
         }
 
         @Override
-        public Set<Element> visitType(TypeElement e, DeclaredType p) {
+        public Set<Element> visitType(TypeElement e, TypeElement p) {
             // Type parameters are not considered to be enclosed by a type
             scan(e.getTypeParameters(), p);
             return scan(e.getEnclosedElements(), p);
         }
 
         @Override
-        public Set<Element> visitExecutable(ExecutableElement e, DeclaredType p) {
+        public Set<Element> visitExecutable(ExecutableElement e, TypeElement p) {
             // Type parameters are not considered to be enclosed by an executable
             scan(e.getTypeParameters(), p);
             return scan(e.getEnclosedElements(), p);
         }
 
         @Override
-        public Set<Element> scan(Element e, DeclaredType p) {
+        public Set<Element> scan(Element e, TypeElement p) {
             java.util.List<? extends AnnotationMirror> annotationMirrors =
                 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
             for (AnnotationMirror annotationMirror : annotationMirrors) {
-                if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p))
+                if (p.equals(annotationMirror.getAnnotationType().asElement()))
                     annotatedElements.add(e);
             }
             e.accept(this, p);
--- a/langtools/test/tools/javac/processing/environment/round/BuriedAnnotations.java	Fri Jan 10 12:47:15 2014 +0100
+++ b/langtools/test/tools/javac/processing/environment/round/BuriedAnnotations.java	Fri Jan 10 19:02:54 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 
 /**
- * Class to hold annotations for ElementsAnnotatedWithTest.
+ * Class to hold annotations for TestElementsAnnotatedWith.
  */
 
 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/processing/environment/round/ErroneousAnnotations.java	Fri Jan 10 19:02:54 2014 +0100
@@ -0,0 +1,12 @@
+/** /nodynamiccopyright/
+ * Class to hold annotations for TestElementsAnnotatedWith.
+ */
+
+@AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
+                      expectedSize=0,
+                      names={})
+@Undefined
+public class ErroneousAnnotations {
+    @Undefined
+    private void foo() {return;}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/processing/environment/round/ErroneousAnnotations.out	Fri Jan 10 19:02:54 2014 +0100
@@ -0,0 +1,4 @@
+ErroneousAnnotations.java:8:2: compiler.err.cant.resolve: kindname.class, Undefined, , 
+ErroneousAnnotations.java:10:6: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, ErroneousAnnotations, null)
+2 errors
+Results: []
--- a/langtools/test/tools/javac/processing/environment/round/Part1.java	Fri Jan 10 12:47:15 2014 +0100
+++ b/langtools/test/tools/javac/processing/environment/round/Part1.java	Fri Jan 10 19:02:54 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 
 /**
- * Class to hold annotations for ElementsAnnotatedWithTest.
+ * Class to hold annotations for TestElementsAnnotatedWith.
  */
 
 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
--- a/langtools/test/tools/javac/processing/environment/round/Part2.java	Fri Jan 10 12:47:15 2014 +0100
+++ b/langtools/test/tools/javac/processing/environment/round/Part2.java	Fri Jan 10 19:02:54 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 
 /**
- * Class to hold annotations for ElementsAnnotatedWithTest.
+ * Class to hold annotations for TestElementsAnnotatedWith.
  */
 @SuppressWarnings("")
 public class Part2 {
--- a/langtools/test/tools/javac/processing/environment/round/SurfaceAnnotations.java	Fri Jan 10 12:47:15 2014 +0100
+++ b/langtools/test/tools/javac/processing/environment/round/SurfaceAnnotations.java	Fri Jan 10 19:02:54 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 
 /**
- * Class to hold annotations for ElementsAnnotatedWithTest.
+ * Class to hold annotations for TestElementsAnnotatedWith.
  */
 
 @AnnotatedElementInfo(annotationName="java.lang.SuppressWarnings",
--- a/langtools/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java	Fri Jan 10 12:47:15 2014 +0100
+++ b/langtools/test/tools/javac/processing/environment/round/TestElementsAnnotatedWith.java	Fri Jan 10 19:02:54 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854
+ * @bug 6397298 6400986 6425592 6449798 6453386 6508401 6498938 6911854 8030049
  * @summary Tests that getElementsAnnotatedWith works properly.
  * @author  Joseph D. Darcy
  * @library /tools/javac/lib
@@ -37,23 +37,18 @@
  * @compile -processor TestElementsAnnotatedWith -proc:only C2.java
  * @compile -processor TestElementsAnnotatedWith -proc:only Foo.java
  * @compile -processor TestElementsAnnotatedWith -proc:only TypeParameterAnnotations.java
+ * @compile/fail/ref=ErroneousAnnotations.out -processor TestElementsAnnotatedWith -proc:only -XDrawDiagnostics ErroneousAnnotations.java
  * @compile Foo.java
  * @compile/process -processor TestElementsAnnotatedWith -proc:only Foo
  */
 
 import java.lang.annotation.Annotation;
-import java.io.*;
 import java.util.Collections;
 import java.util.Set;
 import java.util.HashSet;
-import java.util.List;
-import java.util.ArrayList;
 import java.util.Arrays;
 import javax.annotation.processing.*;
-import javax.tools.*;
-import javax.lang.model.SourceVersion;
 import javax.lang.model.element.*;
-import javax.lang.model.util.*;
 import static javax.lang.model.util.ElementFilter.*;
 
 /**
--- a/langtools/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java	Fri Jan 10 12:47:15 2014 +0100
+++ b/langtools/test/tools/javac/processing/environment/round/TypeParameterAnnotations.java	Fri Jan 10 19:02:54 2014 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 
 /**
- * Class to hold annotations for ElementsAnnotatedWithTest.
+ * Class to hold annotations for TestElementsAnnotatedWith.
  */
 
 @AnnotatedElementInfo(annotationName="TpAnno",