Eclipse Nullness warning (after Java8) [message #1385694] |
Tue, 10 June 2014 14:29  |
Eclipse User |
|
|
|
Hi,
I am trying to understand a warning that Eclipse started to give after I upgraded my project to Java8 (and started using the new Nullness type qualifiers):
For the following code:
String[] arguments = // Call to a library code with no annotations, could return anything.
if (arguments != null)
{
String @NonNull [] temp = arguments;
}
The assignment (temp to arguments) gives the following warning:
Null type safety (type annotations): The expression of type 'String[]' needs unchecked conversion to conform to 'String @NonNull[]'
I cannot quite understand why there is a warning. I think Eclipse java compiler should be able to infer that arguments array is not null after the "if" check, am I mistaken? By the way, arguments is a local variable.
Any idea why I might be getting the warning?
Thanks,
|
|
|
Re: Eclipse Nullness warning (after Java8) [message #1385763 is a reply to message #1385694] |
Wed, 11 June 2014 06:01   |
Eclipse User |
|
|
|
Is that really declaring that the array is not null or rather that it
doesn't contain any null strings (which clearly has not been checked)?
On 10/06/2014 4:29 PM, Kivanc Muslu wrote:
> Hi,
> I am trying to understand a warning that Eclipse started to give after
> I upgraded my project to Java8 (and started using the new Nullness
> type qualifiers):
>
> For the following code:
>
>
> String[] arguments = // Call to a library code with no annotations,
> could return anything.
> if (arguments != null)
> {
> String @NonNull [] temp = arguments;
> }
>
>
> The assignment (temp to arguments) gives the following warning:
>
> Null type safety (type annotations): The expression of type 'String[]'
> needs unchecked conversion to conform to 'String @NonNull[]'
>
>
> I cannot quite understand why there is a warning. I think Eclipse java
> compiler should be able to infer that arguments array is not null
> after the "if" check, am I mistaken? By the way, arguments is a local
> variable.
>
> Any idea why I might be getting the warning?
> Thanks,
|
|
|
Re: Eclipse Nullness warning (after Java8) [message #1385764 is a reply to message #1385763] |
Wed, 11 June 2014 06:13   |
Eclipse User |
|
|
|
To my best knowledge, Java8 brought "type" qualifiers, which changed the location (and meaning) of some annotations.
Java7:
@NonNull String [] temp; // temp array cannot be null, we don't know anything about its contents.
Note that with Java7, there was no way to describe/check the nullness of the elements of an array, so the annotation way at the beginning.
Java8:
@NonNull String [] temp; // temp can be null, however if it is not null, its elements cannot be null.
Notice the location of the annotation. It comes before the type "String" so it qualifies String rather than the array. String in this instance represents the elements of the array.
String @NonNull [] temp; // temp cannot be null, we don't know anything about its elements (same as Java7)
Again, notice the place of the annotation. It comes before the array declaration ([]) hence qualifies it.
@NonNull String @NonNull [] temp; // temp cannot be null and its elements cannot be null, too.
I am pretty confident, but I might be missing something, please let me know if this is the case.
Thanks,
|
|
|
Re: Eclipse Nullness warning (after Java8) [message #1385769 is a reply to message #1385764] |
Wed, 11 June 2014 06:57   |
Eclipse User |
|
|
|
Kivanc,
Okay, it was a dumb question of mine then. No doubt a highly qualified
JDT expert will take note...
On 11/06/2014 8:13 AM, Kivanc Muslu wrote:
> To my best knowledge, Java8 brought "type" qualifiers, which changed
> the location (and meaning) of some annotations.
>
> Java7:
>
> @NonNull String [] temp; // temp array cannot be null, we don't know
> anything about its contents.
>
> Note that with Java7, there was no way to describe/check the nullness
> of the elements of an array, so the annotation way at the beginning.
>
> Java8:
>
> @NonNull String [] temp; // temp can be null, however if it is not
> null, its elements cannot be null.
>
> Notice the location of the annotation. It comes before the type
> "String" so it qualifies String rather than the array. String in this
> instance represents the elements of the array.
>
>
> String @NonNull [] temp; // temp cannot be null, we don't know
> anything about its elements (same as Java7)
>
> Again, notice the place of the annotation. It comes before the array
> declaration ([]) hence qualifies it.
>
>
> @NonNull String @NonNull [] temp; // temp cannot be null and its
> elements cannot be null, too.
>
>
> I am pretty confident, but I might be missing something, please let me
> know if this is the case.
> Thanks,
|
|
|
Re: Eclipse Nullness warning (after Java8) [message #1385952 is a reply to message #1385769] |
Thu, 12 June 2014 12:52  |
Eclipse User |
|
|
|
Sadly this is a simple bug in our implementation: for one particular case we are not correctly combining the information from annotations with results from flow analysis (happens when provided type is an array type with no null annotations at all and expected type requires @NonNull only at the outermost array type).
Bug filed (with draft patch): https://bugs.eclipse.org/437270
While it's too late for Luna, this'll be my first fix after release, a good candidate for 4.4.1, actually.
For the time being I'd suggest to add this to the declaration of 'temp':
@SuppressWarnings("null") // see https://bugs.eclipse.org/437270
sorry for the inconvenience,
Stephan
|
|
|
Powered by
FUDForum. Page generated in 0.03943 seconds