Bug 562222 - Null analysis error when passing array element to a method
Summary: Null analysis error when passing array element to a method
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.23   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2020-04-16 10:13 EDT by Andy Poudret CLA
Modified: 2024-05-08 09:05 EDT (History)
2 users (show)

See Also:


Attachments
Java project reproducing the issue (4.42 KB, application/x-zip-compressed)
2020-04-16 10:13 EDT, Andy Poudret CLA
no flags Details
Java project reproducing the issue (Eclipse 2022-03) (26.06 KB, application/x-zip-compressed)
2022-04-19 04:30 EDT, Andy Poudret CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andy Poudret CLA 2020-04-16 10:13:14 EDT
Created attachment 282475 [details]
Java project reproducing the issue

Hello,

Let's say you have a method which returns an array of objects.
If you call this method and get an element of the array, you will be able to call a method of the element, even though it may be null.
However, you are not allowed to pass the element to a method if it requires a non-null parameter.

I think this behavior is not consistent: either the analyzer considers that it is not required to check that the element is null, in which case I should be able to pass the element to the method ; or the analyzer requires a null check, in which cas I should not be able to call a method of the element.

I have attached an Eclipse JDT project which reproduces this issue.
Tested with JDK 1.7, with null analysis enabled in project configuration.

Best regards,
Andy
Comment 1 Eclipse Genie CLA 2022-04-07 08:05:38 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 2 Andy Poudret CLA 2022-04-19 04:30:30 EDT
Created attachment 288459 [details]
Java project reproducing the issue (Eclipse 2022-03)
Comment 3 Andy Poudret CLA 2022-04-19 04:32:42 EDT
The issue is still reproduced with Eclipse 2022-03. I have updated the Java project so that it works on this Eclipse version.
Comment 4 Adam Gent CLA 2022-05-17 10:03:44 EDT
I checked your code and it works mostly fine for me. BTW analysis does not work unless you are on Java 8 or above.

The @NonNullByDefault does not make the array values NonNull by default. Only the Array itself. The array values are not "marked" which is different than marked nullable.

Because the values of the array are undefined you get weird behavior. It is like in legacy mode (e.g. `NonNullbyDefault({})` ) so I guess it allows method chaining. 

That is `createArray()[0]` does not have a Nullable type so you can call methods on it however if you pass it around there is problem because the type does not match as printValue requires `@NonNull Integer` and not `missing_annotation Integer`.

An easy fix is to do:

        private static @Nullable Integer  [] createArray() {
                return new @Nullable Integer[] { 5, 3, null };
        }

Notice how I had to mark the array twice. 

Or you can actually specify it in `@NonNullByDefault({Location.ARRAY_CONTENTS, ...)`.
Comment 5 Eclipse Genie CLA 2024-05-08 09:05:19 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.