Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug of null analysis on record class #3933

Open
ravenadesk opened this issue Jan 28, 2025 · 0 comments
Open

Bug of null analysis on record class #3933

ravenadesk opened this issue Jan 28, 2025 · 0 comments

Comments

@ravenadesk
Copy link

ravenadesk commented Jan 28, 2025

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:

Image

Image

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;
    }

    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant