8046916: Type parameter annotations don't work with multiple type parameters
authorjlahoda
Wed, 18 Jun 2014 10:44:16 +0200
changeset 25009 55c823e853f2
parent 25008 969bfaae5bce
child 25010 64c8d860c984
8046916: Type parameter annotations don't work with multiple type parameters Summary: When reading type variable's annotations out of the owner's type annotations, use the type variable's index in owner to exclude annotations belonging to other type variables. Reviewed-by: jfranck, emc
langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java
langtools/test/tools/javac/processing/model/element/TestTypeParameterAnnotations.java
--- a/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java	Tue Jun 17 17:33:01 2014 +0100
+++ b/langtools/src/share/classes/com/sun/tools/javac/code/Symbol.java	Wed Jun 18 10:44:16 2014 +0200
@@ -770,42 +770,41 @@
 
         @Override
         public List<Attribute.Compound> getAnnotationMirrors() {
-            return onlyTypeVariableAnnotations(owner.getRawTypeAttributes());
-        }
-
-        private List<Attribute.Compound> onlyTypeVariableAnnotations(
-                List<Attribute.TypeCompound> candidates) {
-            // Declaration annotations on TypeParameters are stored in type attributes
+            // Declaration annotations on type variables are stored in type attributes
+            // on the owner of the TypeVariableSymbol
+            List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
+            int index = owner.getTypeParameters().indexOf(this);
             List<Attribute.Compound> res = List.nil();
             for (Attribute.TypeCompound a : candidates) {
-                if (a.position.type == TargetType.CLASS_TYPE_PARAMETER ||
-                    a.position.type == TargetType.METHOD_TYPE_PARAMETER)
+                if (isCurrentSymbolsAnnotation(a, index))
                     res = res.prepend(a);
             }
 
-            return res = res.reverse();
+            return res.reverse();
         }
 
-
-
         // Helper to getAnnotation[s]
         @Override
         public <A extends Annotation> Attribute.Compound getAttribute(Class<A> annoType) {
-
             String name = annoType.getName();
 
             // Declaration annotations on type variables are stored in type attributes
             // on the owner of the TypeVariableSymbol
             List<Attribute.TypeCompound> candidates = owner.getRawTypeAttributes();
+            int index = owner.getTypeParameters().indexOf(this);
             for (Attribute.TypeCompound anno : candidates)
-                if (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
-                        anno.position.type == TargetType.METHOD_TYPE_PARAMETER)
-                    if (name.contentEquals(anno.type.tsym.flatName()))
-                        return anno;
+                if (isCurrentSymbolsAnnotation(anno, index) &&
+                    name.contentEquals(anno.type.tsym.flatName()))
+                    return anno;
 
             return null;
         }
-
+            //where:
+            boolean isCurrentSymbolsAnnotation(Attribute.TypeCompound anno, int index) {
+                return (anno.position.type == TargetType.CLASS_TYPE_PARAMETER ||
+                        anno.position.type == TargetType.METHOD_TYPE_PARAMETER) &&
+                       anno.position.parameter_index == index;
+            }
 
 
         @Override
--- a/langtools/test/tools/javac/processing/model/element/TestTypeParameterAnnotations.java	Tue Jun 17 17:33:01 2014 +0100
+++ b/langtools/test/tools/javac/processing/model/element/TestTypeParameterAnnotations.java	Wed Jun 18 10:44:16 2014 +0200
@@ -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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8011027
+ * @bug 8011027 8046916
  * @library /tools/javac/lib
  * @build JavacTestingAbstractProcessor TestTypeParameterAnnotations
  * @compile -processor TestTypeParameterAnnotations -proc:only TestTypeParameterAnnotations.java
@@ -33,10 +33,16 @@
 import java.lang.annotation.*;
 import javax.annotation.processing.*;
 import javax.lang.model.element.*;
-import javax.lang.model.util.*;
 import javax.tools.*;
 
-public class TestTypeParameterAnnotations<@Foo @Bar @Baz T> extends JavacTestingAbstractProcessor {
+@ExpectedTypeParameterAnnotations(typeParameterName="T1",
+                                  annotations={"Foo1", "Bar1", "Baz1"})
+@ExpectedTypeParameterAnnotations(typeParameterName="T2", annotations={})
+@ExpectedTypeParameterAnnotations(typeParameterName="T3",
+                                  annotations={"Foo2", "Bar2", "Baz2"})
+@ExpectedTypeParameterAnnotations(typeParameterName="T4", annotations={})
+public class TestTypeParameterAnnotations<@Foo1 @Bar1 @Baz1 T1, T2, @Foo2 @Bar2 @Baz2 T3, T4> extends
+        JavacTestingAbstractProcessor {
     int round = 0;
 
     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
@@ -74,82 +80,69 @@
     int check(Element e, List<? extends TypeParameterElement> typarams) {
         if (typarams.isEmpty())
             return 0;
-        if (typarams.size() != 1)
-            return 0;
 
-        for (TypeParameterElement tpe: typarams) {
-            boolean b1 = checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors());
-            boolean b2 = checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe));
-            boolean b3 = checkGetAnnotation(tpe);
-            boolean b4 = checkGetAnnotations(tpe);
-            return b1 && b2 && b3 && b4 ? 1 : 0;
-        }
-        return 0;
-    }
-
-    boolean checkAnnotationMirrors(TypeParameterElement tpe, List<? extends AnnotationMirror> l) {
-        if (l.size() != 3) {
-            error("To few annotations, got " + l.size() +
-                    ", should be 3", tpe);
-            return false;
+        for (TypeParameterElement tpe : typarams) {
+            ExpectedTypeParameterAnnotations expected = null;
+            for (ExpectedTypeParameterAnnotations a : e.getAnnotationsByType(ExpectedTypeParameterAnnotations.class)) {
+                if (tpe.getSimpleName().contentEquals(a.typeParameterName())) {
+                    expected = a;
+                    break;
+                }
+            }
+            if (expected == null) {
+                throw new IllegalStateException("Does not have expected values annotation.");
+            }
+            checkAnnotationMirrors(tpe, tpe.getAnnotationMirrors(), expected);
+            checkAnnotationMirrors(tpe, elements.getAllAnnotationMirrors(tpe), expected);
+            checkGetAnnotation(tpe, expected);
+            checkGetAnnotations(tpe, expected);
         }
 
-        AnnotationMirror m = l.get(0);
-        if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Foo"))) {
-            error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement());
-            return false;
-        }
-        m = l.get(1);
-        if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Bar"))) {
-            error("Wrong type of annotation, was expecting @Bar", m.getAnnotationType().asElement());
-            return false;
-        }
-        m = l.get(2);
-        if (!m.getAnnotationType().asElement().equals(elements.getTypeElement("Baz"))) {
-            error("Wrong type of annotation, was expecting @Baz", m.getAnnotationType().asElement());
-            return false;
-        }
-        return true;
+        return typarams.size();
     }
 
-    boolean checkGetAnnotation(TypeParameterElement tpe) {
-        Foo f = tpe.getAnnotation(Foo.class);
-        if (f == null)
-            error("Expecting @Foo to be present in getAnnotation()", tpe);
+    void checkAnnotationMirrors(TypeParameterElement tpe, List<? extends AnnotationMirror> l, ExpectedTypeParameterAnnotations expected) {
+        String[] expectedAnnotations = expected.annotations();
 
-        Bar b = tpe.getAnnotation(Bar.class);
-        if (b == null)
-            error("Expecting @Bar to be present in getAnnotation()", tpe);
+        if (l.size() != expectedAnnotations.length) {
+            error("Incorrect number of annotations, got " + l.size() +
+                    ", should be " + expectedAnnotations.length, tpe);
+            return ;
+        }
 
-        Baz z = tpe.getAnnotation(Baz.class);
-        if (z == null)
-            error("Expecting @Baz to be present in getAnnotation()", tpe);
-
-        return f != null &&
-            b != null &&
-            z != null;
+        for (int i = 0; i < expectedAnnotations.length; i++) {
+            AnnotationMirror m = l.get(i);
+            if (!m.getAnnotationType().asElement().equals(elements.getTypeElement(expectedAnnotations[i]))) {
+                error("Wrong type of annotation, was expecting @Foo", m.getAnnotationType().asElement());
+                return ;
+            }
+        }
     }
 
-    boolean checkGetAnnotations(TypeParameterElement tpe) {
-        Foo[] f = tpe.getAnnotationsByType(Foo.class);
-        if (f.length != 1) {
-            error("Expecting 1 @Foo to be present in getAnnotationsByType()", tpe);
-            return false;
-        }
+    void checkGetAnnotation(TypeParameterElement tpe, ExpectedTypeParameterAnnotations expected) {
+        List<String> expectedAnnotations = Arrays.asList(expected.annotations());
+
+        for (Class<? extends Annotation> c : ALL_ANNOTATIONS) {
+            Object a = tpe.getAnnotation(c);
 
-        Bar[] b = tpe.getAnnotationsByType(Bar.class);
-        if (b.length != 1) {
-            error("Expecting 1 @Bar to be present in getAnnotationsByType()", tpe);
-            return false;
+            if (a != null ^ expectedAnnotations.indexOf(c.getName()) != (-1)) {
+                error("Unexpected behavior for " + c.getName(), tpe);
+                return ;
+            }
         }
+    }
 
-        Baz[] z = tpe.getAnnotationsByType(Baz.class);
-        if (z.length != 1) {
-            error("Expecting 1 @Baz to be present in getAnnotationsByType()", tpe);
-            return false;
+    void checkGetAnnotations(TypeParameterElement tpe, ExpectedTypeParameterAnnotations expected) {
+        List<String> expectedAnnotations = Arrays.asList(expected.annotations());
+
+        for (Class<? extends Annotation> c : ALL_ANNOTATIONS) {
+            Object[] a = tpe.getAnnotationsByType(c);
+
+            if (a.length > 0 ^ expectedAnnotations.indexOf(c.getName()) != (-1)) {
+                error("Unexpected behavior for " + c.getName(), tpe);
+                return ;
+            }
         }
-
-        return true;
     }
 
     void note(String msg) {
@@ -168,23 +161,71 @@
         messager.printMessage(Diagnostic.Kind.ERROR, msg);
     }
 
+    Class<? extends Annotation>[] ALL_ANNOTATIONS = new Class[] {
+        Foo1.class, Bar1.class, Baz1.class,
+        Foo2.class, Bar2.class, Baz2.class,
+    };
+
     // additional generic elements to test
-    <@Foo @Bar @Baz X> X m(X x) { return x; }
+    @ExpectedTypeParameterAnnotations(typeParameterName="W",
+                                      annotations={"Foo1", "Bar1", "Baz1"})
+    @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={})
+    @ExpectedTypeParameterAnnotations(typeParameterName="Y",
+                                      annotations={"Foo2", "Bar2", "Baz2"})
+    @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={})
+    <@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> X m(X x) { return x; }
 
-    interface Intf<@Foo @Bar @Baz X> { X m() ; }
+    @ExpectedTypeParameterAnnotations(typeParameterName="W",
+                                      annotations={"Foo1", "Bar1", "Baz1"})
+    @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={})
+    @ExpectedTypeParameterAnnotations(typeParameterName="Y",
+                                      annotations={"Foo2", "Bar2", "Baz2"})
+    @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={})
+    interface Intf<@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> { X m() ; }
 
-    class Class<@Foo @Bar @Baz X> {
-        <@Foo @Bar @Baz Y> Class() { }
+    @ExpectedTypeParameterAnnotations(typeParameterName="W",
+                                      annotations={"Foo1", "Bar1", "Baz1"})
+    @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={})
+    @ExpectedTypeParameterAnnotations(typeParameterName="Y",
+                                      annotations={"Foo2", "Bar2", "Baz2"})
+    @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={})
+    class Clazz<@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> {
+        @ExpectedTypeParameterAnnotations(typeParameterName="W",
+                                          annotations={"Foo1", "Bar1", "Baz1"})
+        @ExpectedTypeParameterAnnotations(typeParameterName="X", annotations={})
+        @ExpectedTypeParameterAnnotations(typeParameterName="Y",
+                                          annotations={"Foo2", "Bar2", "Baz2"})
+        @ExpectedTypeParameterAnnotations(typeParameterName="Z", annotations={})
+        <@Foo1 @Bar1 @Baz1 W, X, @Foo2 @Bar2 @Baz2 Y, Z> Clazz() { }
     }
 
-    final int expect = 5;  // top level class, plus preceding examples
+    final int expect = 5 * 4;  // top level class, plus preceding examples, 4 type variables each
 }
 
 @Target(ElementType.TYPE_PARAMETER)
-@interface Foo {}
+@interface Foo1 {}
+
+@Target(ElementType.TYPE_PARAMETER)
+@interface Bar1 {}
+
+@Target(ElementType.TYPE_PARAMETER)
+@interface Baz1 {}
+
+@Target(ElementType.TYPE_PARAMETER)
+@interface Foo2 {}
 
 @Target(ElementType.TYPE_PARAMETER)
-@interface Bar {}
+@interface Bar2 {}
 
 @Target(ElementType.TYPE_PARAMETER)
-@interface Baz {}
+@interface Baz2 {}
+
+@Repeatable(ExpectedTypeParameterAnnotationsCollection.class)
+@interface ExpectedTypeParameterAnnotations {
+    public String typeParameterName();
+    public String[] annotations();
+}
+
+@interface ExpectedTypeParameterAnnotationsCollection {
+    public ExpectedTypeParameterAnnotations[] value();
+}