changeset 52777:7d3391e9df19

8206325: AssertionError in TypeSymbol.getAnnotationTypeMetadata Reviewed-by: mcimadamore
author sadayapalam
date Fri, 30 Nov 2018 10:37:48 +0530
parents c05ba185a1d3
children dbbf46b13d52
files src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java test/langtools/tools/javac/annotations/AtNonAnnotationTypeTest.java test/langtools/tools/javac/annotations/AtNonAnnotationTypeTest.out
diffstat 5 files changed, 32 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Thu Nov 29 18:57:18 2018 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java	Fri Nov 30 10:37:48 2018 +0530
@@ -169,9 +169,12 @@
 
     /**
      * Determine whether an annotation is a declaration annotation,
-     * a type annotation, or both.
+     * a type annotation, or both (or none, i.e a non-annotation masquerading as one).
      */
     public AnnotationType annotationTargetType(Attribute.Compound a, Symbol s) {
+        if (!a.type.tsym.isAnnotationType()) {
+            return AnnotationType.NONE;
+        }
         List<Attribute> targets = annotationTargets(a.type.tsym);
         return (targets == null) ?
                 AnnotationType.DECLARATION :
@@ -319,6 +322,8 @@
                         onlyTypeAnnos.append(ta);
                         break;
                     }
+                    case NONE: // Error signaled already, just drop the non-annotation.
+                        break;
                 }
             }
 
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java	Thu Nov 29 18:57:18 2018 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java	Fri Nov 30 10:37:48 2018 +0530
@@ -344,17 +344,19 @@
 
             Assert.checkNonNull(c, "Failed to create annotation");
 
-            if (annotated.containsKey(a.type.tsym)) {
-                if (!allowRepeatedAnnos) {
-                    log.error(DiagnosticFlag.SOURCE_LEVEL, a.pos(), Feature.REPEATED_ANNOTATIONS.error(sourceName));
+            if (a.type.tsym.isAnnotationType()) {
+                if (annotated.containsKey(a.type.tsym)) {
+                    if (!allowRepeatedAnnos) {
+                        log.error(DiagnosticFlag.SOURCE_LEVEL, a.pos(), Feature.REPEATED_ANNOTATIONS.error(sourceName));
+                    }
+                    ListBuffer<T> l = annotated.get(a.type.tsym);
+                    l = l.append(c);
+                    annotated.put(a.type.tsym, l);
+                    pos.put(c, a.pos());
+                } else {
+                    annotated.put(a.type.tsym, ListBuffer.of(c));
+                    pos.put(c, a.pos());
                 }
-                ListBuffer<T> l = annotated.get(a.type.tsym);
-                l = l.append(c);
-                annotated.put(a.type.tsym, l);
-                pos.put(c, a.pos());
-            } else {
-                annotated.put(a.type.tsym, ListBuffer.of(c));
-                pos.put(c, a.pos());
             }
 
             // Note: @Deprecated has no effect on local variables and parameters
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Thu Nov 29 18:57:18 2018 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java	Fri Nov 30 10:37:48 2018 +0530
@@ -2749,7 +2749,7 @@
         class AnnotationValidator extends TreeScanner {
             @Override
             public void visitAnnotation(JCAnnotation tree) {
-                if (!tree.type.isErroneous()) {
+                if (!tree.type.isErroneous() && tree.type.tsym.isAnnotationType()) {
                     super.visitAnnotation(tree);
                     validateAnnotation(tree);
                 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/annotations/AtNonAnnotationTypeTest.java	Fri Nov 30 10:37:48 2018 +0530
@@ -0,0 +1,11 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8206325
+ * @summary AssertionError in TypeSymbol.getAnnotationTypeMetadata
+ * @compile/fail/ref=AtNonAnnotationTypeTest.out -XDrawDiagnostics -XDdev AtNonAnnotationTypeTest.java
+ */
+
+import java.lang.annotation.Annotation;
+class AtNonAnnotationTypeTest<Override extends Annotation> {
+  AtNonAnnotationTypeTest(@Override String foo) {}
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/annotations/AtNonAnnotationTypeTest.out	Fri Nov 30 10:37:48 2018 +0530
@@ -0,0 +1,2 @@
+AtNonAnnotationTypeTest.java:10:28: compiler.err.not.annotation.type: Override
+1 error
\ No newline at end of file