Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Eclipse Nullness warning (after Java8)
icon5.gif  Eclipse Nullness warning (after Java8) [message #1385694] Tue, 10 June 2014 14:29 Go to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33258
Registered: July 2009
Senior Member
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,


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Eclipse Nullness warning (after Java8) [message #1385764 is a reply to message #1385763] Wed, 11 June 2014 06:13 Go to previous messageGo to next message
Kivanc Muslu is currently offline Kivanc MusluFriend
Messages: 153
Registered: November 2010
Senior Member
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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33258
Registered: July 2009
Senior Member
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,


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Eclipse Nullness warning (after Java8) [message #1385952 is a reply to message #1385769] Thu, 12 June 2014 12:52 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
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
Previous Topic:Running a java program with apitrace from Eclipse
Next Topic:The selection can not be run on any server
Goto Forum:
  


Current Time: Wed Dec 04 00:14:44 GMT 2024

Powered by FUDForum. Page generated in 0.02873 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top