-
Bug
-
Resolution: Fixed
-
P3
-
8
-
b20
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8048487 | 8u25 | Jan Lahoda | P3 | Resolved | Fixed | b05 |
JDK-8047176 | 8u20 | Jan Lahoda | P3 | Resolved | Fixed | b20 |
JDK-8052634 | emb-8u26 | Jan Lahoda | P3 | Resolved | Fixed | b17 |
When using javax.lang.model (as well as internal javac classes), if a class declares multiple type parameters, any annotation on one of the type parameters appears as if it were applied to all of them.
A test:
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class TypeParameterAnnotationsTest
<S,
@TypeParameterAnnotationsTest.Foo("T") T,
@TypeParameterAnnotationsTest.Bar("U") U,
V>
extends AbstractProcessor {
@Target(ElementType.TYPE_PARAMETER) @interface Foo { String value(); }
@Target(ElementType.TYPE_PARAMETER) @interface Bar { String value(); }
public boolean process(Set<? extends TypeElement> annots,
RoundEnvironment env) {
for (Element root : env.getRootElements()) {
if (root.getSimpleName().contentEquals("TypeParameterAnnotationsTest")) {
for (TypeParameterElement var : ((TypeElement) root).getTypeParameters()) {
System.out.println("var: " + var);
System.out.println("annotations: " + var.getAnnotationMirrors());
System.out.println("Foo annotation: " + var.getAnnotation(Foo.class));
System.out.println("Bar annotation: " + var.getAnnotation(Bar.class));
}
}
}
return true;
}
}
To observe the results:
javac TypeParameterAnnotationsTest.java
javac -processor TypeParameterAnnotationsTest TypeParameterAnnotationsTest.java
Output:
var: S
annotations: @TypeParameterAnnotationsTest.Foo("T"),@TypeParameterAnnotationsTest.Bar("U")
Foo annotation: @TypeParameterAnnotationsTest$Foo(value=T)
Bar annotation: @TypeParameterAnnotationsTest$Bar(value=U)
var: T
annotations: @TypeParameterAnnotationsTest.Foo("T"),@TypeParameterAnnotationsTest.Bar("U")
Foo annotation: @TypeParameterAnnotationsTest$Foo(value=T)
Bar annotation: @TypeParameterAnnotationsTest$Bar(value=U)
var: U
annotations: @TypeParameterAnnotationsTest.Foo("T"),@TypeParameterAnnotationsTest.Bar("U")
Foo annotation: @TypeParameterAnnotationsTest$Foo(value=T)
Bar annotation: @TypeParameterAnnotationsTest$Bar(value=U)
var: V
annotations: @TypeParameterAnnotationsTest.Foo("T"),@TypeParameterAnnotationsTest.Bar("U")
Foo annotation: @TypeParameterAnnotationsTest$Foo(value=T)
Bar annotation: @TypeParameterAnnotationsTest$Bar(value=U)
I confirmed that reflection does not share this problem: java.lang.reflect.TypeVariable.getAnnotations() returns correctly results.
A test:
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class TypeParameterAnnotationsTest
<S,
@TypeParameterAnnotationsTest.Foo("T") T,
@TypeParameterAnnotationsTest.Bar("U") U,
V>
extends AbstractProcessor {
@Target(ElementType.TYPE_PARAMETER) @interface Foo { String value(); }
@Target(ElementType.TYPE_PARAMETER) @interface Bar { String value(); }
public boolean process(Set<? extends TypeElement> annots,
RoundEnvironment env) {
for (Element root : env.getRootElements()) {
if (root.getSimpleName().contentEquals("TypeParameterAnnotationsTest")) {
for (TypeParameterElement var : ((TypeElement) root).getTypeParameters()) {
System.out.println("var: " + var);
System.out.println("annotations: " + var.getAnnotationMirrors());
System.out.println("Foo annotation: " + var.getAnnotation(Foo.class));
System.out.println("Bar annotation: " + var.getAnnotation(Bar.class));
}
}
}
return true;
}
}
To observe the results:
javac TypeParameterAnnotationsTest.java
javac -processor TypeParameterAnnotationsTest TypeParameterAnnotationsTest.java
Output:
var: S
annotations: @TypeParameterAnnotationsTest.Foo("T"),@TypeParameterAnnotationsTest.Bar("U")
Foo annotation: @TypeParameterAnnotationsTest$Foo(value=T)
Bar annotation: @TypeParameterAnnotationsTest$Bar(value=U)
var: T
annotations: @TypeParameterAnnotationsTest.Foo("T"),@TypeParameterAnnotationsTest.Bar("U")
Foo annotation: @TypeParameterAnnotationsTest$Foo(value=T)
Bar annotation: @TypeParameterAnnotationsTest$Bar(value=U)
var: U
annotations: @TypeParameterAnnotationsTest.Foo("T"),@TypeParameterAnnotationsTest.Bar("U")
Foo annotation: @TypeParameterAnnotationsTest$Foo(value=T)
Bar annotation: @TypeParameterAnnotationsTest$Bar(value=U)
var: V
annotations: @TypeParameterAnnotationsTest.Foo("T"),@TypeParameterAnnotationsTest.Bar("U")
Foo annotation: @TypeParameterAnnotationsTest$Foo(value=T)
Bar annotation: @TypeParameterAnnotationsTest$Bar(value=U)
I confirmed that reflection does not share this problem: java.lang.reflect.TypeVariable.getAnnotations() returns correctly results.
- backported by
-
JDK-8047176 Type parameter annotations don't work with multiple type parameters
- Resolved
-
JDK-8048487 Type parameter annotations don't work with multiple type parameters
- Resolved
-
JDK-8052634 Type parameter annotations don't work with multiple type parameters
- Resolved
- clones
-
JDK-8042060 Type parameter annotations don't work with multiple type parameters
- Closed