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 array elements doesn't work #3658

Open
Yury-Fridlyand opened this issue Apr 19, 2024 · 3 comments
Open

[BUG] @NonNull on array elements doesn't work #3658

Yury-Fridlyand opened this issue Apr 19, 2024 · 3 comments

Comments

@Yury-Fridlyand
Copy link

Describe the bug

private void test (@NonNull String @NonNull [] args)

Such double annotation allows null strings in the array.

To Reproduce
Define test functions

  private void t1 (@NonNull String[] args) {}
  private void t2 (String @NonNull [] args) {}
  private void t3 (@NonNull String @NonNull [] args) {}

Then call all 3

      try {
        t1(null);
      } catch (Exception e) {
        System.out.println("t1(null) error");
      }

      try {
        t1(new String[] { null });
      } catch (Exception e) {
        System.out.println("t1(new String[] { null }) error");
      }

      try {
        t2(null);
      } catch (Exception e) {
        System.out.println("t2(null) error");
      }

      try {
        t2(new String[] { null });
      } catch (Exception e) {
        System.out.println("t2(new String[] { null }) error");
      }

      try {
        t3(null);
      } catch (Exception e) {
        System.out.println("t3(null) error");
      }

      try {
        t3(new String[] { null });
      } catch (Exception e) {
        System.out.println("t3(new String[] { null }) error");
      }

Expected output:

t1(new String[] { null }) error
t2(null) error
t3(null) error
t3(new String[] { null }) error

Actual output:

t1(null) error
t2(null) error
t3(null) error

Expected behavior

private void test (@NonNull String[] args)

Such annotation should prohibit null strings in array.

private void test (String @NonNull [] args)

Such annotation should prohibit null array. (works now)

private void test (@NonNull String @NonNull [] args)

Such double annotation should prohibit null array and null strings in array both.

Version info (please complete the following information):

  • javac 17.0.10 from java-17-openjdk-amd64
  • lombok 1.18.30
  • gradle lombok plugin 6.4.0

Additional context
Meanwhile, IDEA is smart enough to properly warn on annotations:
image
image

@ShashankGarg24
Copy link

Hi, is this issue still open for contribution? if yes, then shall I pick it up?

@Yury-Fridlyand
Copy link
Author

I think yes, contributions are always welcome!

@Rawi01
Copy link
Collaborator

Rawi01 commented Apr 28, 2024

Probably a feature request, not a bug. It was never mentioned anywhere that @NonNull checks array elements.

A similar request (#2271) was rejected a few years ago.

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

3 participants