Following the specification change in Java 20 (JDK-8295807), the following code should be illegal, as TYPE_USE annotation should not be applicable to variable declaration that uses 'var' (including lambda parameter declaration):
```
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.function.Function;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(value=RUNTIME)
@Target(value={TYPE_USE})
@interface Anno {
}
public class TestClass {
public static void main(String[] args) {
Function<Integer, String> f = (@Anno var val) -> Integer.toHexString(val);
System.out.println(f.apply(10));
}
}
```
Nevertheless, javac accepts this code and silently ignores the annotation (it doesn't appear in the generated TestClass.class, despite the RUNTIME retention). It's expected that this code is rejected with a compilation error "annotation interface not applicable to this kind of declaration"
```
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.function.Function;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention(value=RUNTIME)
@Target(value={TYPE_USE})
@interface Anno {
}
public class TestClass {
public static void main(String[] args) {
Function<Integer, String> f = (@Anno var val) -> Integer.toHexString(val);
System.out.println(f.apply(10));
}
}
```
Nevertheless, javac accepts this code and silently ignores the annotation (it doesn't appear in the generated TestClass.class, despite the RUNTIME retention). It's expected that this code is rejected with a compilation error "annotation interface not applicable to this kind of declaration"
- relates to
-
JDK-8295807 9.7.4: No "closest" type for 'var' lambda parameter
-
- Resolved
-