-
Bug
-
Resolution: Fixed
-
P4
-
8
-
b93
-
Verified
Described at http://mail.openjdk.java.net/pipermail/jigsaw-dev/2015-September/004478.html
Take a simple annotation type, Foo.java:
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface Foo {
java.math.RoundingMode rounding();
}
Write an @Foo annotation in the constructor of a simple class, Test.java:
public class Test {
Test() {
@Foo(rounding=java.math.RoundingMode.UNNECESSARY)
int x = 0;
}
}
Great, javap reveals a RuntimeVisibleTypeAnnotations attribute in the Code attribute of Test::<init>.
Now, remove the "Test()" header so that the class just has an instance initializer:
public class Test {
{
@Foo(rounding=java.math.RoundingMode.UNNECESSARY)
int x = 0;
}
}
No RuntimeVisibleTypeAnnotations attribute appears. Same for a static initializer.
The problem appears connected with the local variable declaration itself. That is, there's no attribute from this:
{
@Foo(rounding=java.math.RoundingMode.UNNECESSARY) List x = new ArrayList();
}
but there is an attribute for this:
{
List x = new @Foo(rounding=java.math.RoundingMode.UNNECESSARY) ArrayList();
}
It also appears that type annotations on types in a lambda body -- () -> new ArrayList<@Foo String>() -- are not preserved in an attribute of the synthetic method generated for the lambda expression. While the names of the lambda expression's formal parameters should not be preserved on the synthetic method (when javac -parameters is used), appropriate annotations on the types of the lambda expression's formal parameters or on types in the lambda expression's body should be preserved.
Take a simple annotation type, Foo.java:
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface Foo {
java.math.RoundingMode rounding();
}
Write an @Foo annotation in the constructor of a simple class, Test.java:
public class Test {
Test() {
@Foo(rounding=java.math.RoundingMode.UNNECESSARY)
int x = 0;
}
}
Great, javap reveals a RuntimeVisibleTypeAnnotations attribute in the Code attribute of Test::<init>.
Now, remove the "Test()" header so that the class just has an instance initializer:
public class Test {
{
@Foo(rounding=java.math.RoundingMode.UNNECESSARY)
int x = 0;
}
}
No RuntimeVisibleTypeAnnotations attribute appears. Same for a static initializer.
The problem appears connected with the local variable declaration itself. That is, there's no attribute from this:
{
@Foo(rounding=java.math.RoundingMode.UNNECESSARY) List x = new ArrayList();
}
but there is an attribute for this:
{
List x = new @Foo(rounding=java.math.RoundingMode.UNNECESSARY) ArrayList();
}
It also appears that type annotations on types in a lambda body -- () -> new ArrayList<@Foo String>() -- are not preserved in an attribute of the synthetic method generated for the lambda expression. While the names of the lambda expression's formal parameters should not be preserved on the synthetic method (when javac -parameters is used), appropriate annotations on the types of the lambda expression's formal parameters or on types in the lambda expression's body should be preserved.
- relates to
-
JDK-8207017 Type annotations on anonymous classes in initializer blocks not written to class file
-
- Resolved
-