Details
-
Sub-task
-
Resolution: Fixed
-
P4
-
8
Description
Formal parameters of a lambda expression have no specified translation to class file artifacts. Consequently, it is difficult to see how to preserve declaration annotations on these formal parameters in the class file. (A similar issue arises for the names of the formal parameters, when javac -parameters is used.)
In consultation with Brian Goetz, it was agreed that declaration annotations on formal parameters of lambda expressions should be exempted from the preserve-in-class-file semantics of RetentionPolicy.{CLASS,RUNTIME}, in line with the treatment of declaration annotations on local variables:
"- If m has an element whose value is java.lang.annotation.RetentionPolicy.CLASS or java.lang.annotation.RetentionPolicy.RUNTIME, then a Java compiler must ensure that a is represented in the binary representation of the class or interface in which a appears, unless ***a*** annotates a local variable declaration ***or the formal parameter declaration of a lambda expression***.
***Annotations on the declaration of a local variable, or on the declaration of a formal parameter of a lambda expression, are never retained in the binary representation. In contrast, annotations on the type of a local variable declaration, and on the type of a formal parameter of a lambda expression, are retained in the binary representation if their annotation types specify a suitable retention policy. Note that it is not illegal for an annotation type to be meta-annotated with @Target(ElementType.LOCAL_VARIABLE) and @Retention(RetentionPolicy.CLASS) / @Retention(RetentionPolicy.RUNTIME).***"
(Notice the text is "unless ***a*** annotates a local variable declaration or ..." -- JLS3 erroneously said "unless ***m*** annotates a local variable declaration" and the error persisted through JLS7 and JLS8.)
In consultation with Brian Goetz, it was agreed that declaration annotations on formal parameters of lambda expressions should be exempted from the preserve-in-class-file semantics of RetentionPolicy.{CLASS,RUNTIME}, in line with the treatment of declaration annotations on local variables:
"- If m has an element whose value is java.lang.annotation.RetentionPolicy.CLASS or java.lang.annotation.RetentionPolicy.RUNTIME, then a Java compiler must ensure that a is represented in the binary representation of the class or interface in which a appears, unless ***a*** annotates a local variable declaration ***or the formal parameter declaration of a lambda expression***.
***Annotations on the declaration of a local variable, or on the declaration of a formal parameter of a lambda expression, are never retained in the binary representation. In contrast, annotations on the type of a local variable declaration, and on the type of a formal parameter of a lambda expression, are retained in the binary representation if their annotation types specify a suitable retention policy. Note that it is not illegal for an annotation type to be meta-annotated with @Target(ElementType.LOCAL_VARIABLE) and @Retention(RetentionPolicy.CLASS) / @Retention(RetentionPolicy.RUNTIME).***"
(Notice the text is "unless ***a*** annotates a local variable declaration or ..." -- JLS3 erroneously said "unless ***m*** annotates a local variable declaration" and the error persisted through JLS7 and JLS8.)
Attachments
Issue Links
- relates to
-
JDK-8079060 javac does not generate RuntimeParameterAnnotation attributes for lambda expressions
-
- Resolved
-