Description
I observed an unexpected behavior of null analysis on record class.
Phenomenon
null analysis is not reporting correctly for record class. The value in @Target
of org.springframework.lang.NonNull are {ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}, but the annotation seems not correctly propagating to the accessor of record class. As a comparison I also defined a equivalent class, and the null analysis works fine.
Details:
Expected behavior
According to JEP 395, when set to the record component, the NonNull should be treated as applied to the accessor of record class.
If an annotation on a record component is applicable to a method declaration, then the annotation appears on the corresponding accessor method.
Env
Windows 10 22H2
VsCode 1.96.4
Amazon Corretto 21 21.0.6_7
Extension Pack for Java 0.29.0
Language Support for Java(TM) by Red Hat 1.39.0
Source code:
- InfoRecord.java
package com.example.demo;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
public record InfoRecord(
@NonNull
String title,
@Nullable
String detail
) {
}
- InfoClass.java
package com.example.demo;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
public class InfoClass {
@NonNull
private String title;
@Nullable
private String detail;
public InfoClass(@NonNull String title, @Nullable String detail) {
this.title = title;
this.detail = detail;
}
public @NonNull String getTitle() {
return title;
}
public @Nullable String getDetail() {
return detail;
}
public void setTitle(@NonNull String title) {
this.title = title;
}
public void setDetail(@Nullable String detail) {
this.detail = detail;
}
}