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] @NonNull on a primitive array field on a record isn't working #3366

Closed
Igor-Mukhin opened this issue Mar 6, 2023 · 3 comments · Fixed by #3371
Closed

[BUG] @NonNull on a primitive array field on a record isn't working #3366

Igor-Mukhin opened this issue Mar 6, 2023 · 3 comments · Fixed by #3371
Assignees
Labels
accepted The issue/enhancement is valid, sensible, and explained in sufficient detail bug
Milestone

Comments

@Igor-Mukhin
Copy link

Igor-Mukhin commented Mar 6, 2023

Describe the bug
@ NonNull on a primitive array field on a record isn't working

To Reproduce
NamedByteArray.java

public record NamedByteArray(
        @NonNull String name,
        byte @NonNull[] bytes) {
}

NamedByteArrayTest.java

class NamedByteArrayTest {

    @Test void testConstructorDoesNotAcceptNulls() {
        assertThrows(NullPointerException.class, () -> new NamedByteArray(null, new byte[0]));
        // THIS ONE IS NOT WORKING!!!! NullPointerException is not thrown.
        assertThrows(NullPointerException.class, () -> new NamedByteArray("name", null));
    }

}

Expected behavior
@nonnull should throws NPE on primitive array field on a record if it is null.

Version info:

  • Lombok version 1.18.26
  • Java 19

Additional information:
If you decompile the NamedByteArray.class:

public record NamedByteArray(@NonNull String name, byte @NonNull [] bytes) {
    public NamedByteArray(@NonNull String name, byte @NonNull [] bytes) {
        if (name == null) {
            throw new NullPointerException("name is marked non-null but is null");
        } else {
            this.name = name;
            this.bytes = bytes;
        }
    }

.
.
.



@Rawi01
Copy link
Collaborator

Rawi01 commented Mar 13, 2023

It works as expected if you annotated the method parameter and not the array type e.g. @NonNull byte[] bytes.

It seems odd to me that it works at all (e.g. for normal methods) but there's probably a good reason I can't figure out.

@janrieke
Copy link
Contributor

janrieke commented Mar 13, 2023

IMO the meaning should be:

  • String @NonNull [] strings: the array must not be null, but its elements can be null.
  • @NonNull String [] strings: the array may be null, but if it's not, its elements must not be null.
  • @NonNull String @NonNull [] strings: neither the array nor elements may be null.

For primitives, the second case is obviously irrelevant. So I think Lombok is wrong here.

But: That is only true if it's a type annotation and not a parameter annotation. Lombok's @NonNull is both, so it's impossible to distinguish. However Lombok should at least support the type annotation case 1.

@Rawi01 Rawi01 self-assigned this Mar 13, 2023
@Rawi01 Rawi01 added bug accepted The issue/enhancement is valid, sensible, and explained in sufficient detail labels Mar 13, 2023
Rawi01 added a commit to Rawi01/lombok that referenced this issue Mar 14, 2023
Rawi01 added a commit to Rawi01/lombok that referenced this issue Mar 14, 2023
@rzwitserloot rzwitserloot added this to the next-version milestone Mar 23, 2023
@rzwitserloot
Copy link
Collaborator

Available on edge.

Hamled pushed a commit to Hamled/lombok that referenced this issue Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted The issue/enhancement is valid, sensible, and explained in sufficient detail bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants