Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » null / nonnull for generics is unusable with eclipse 4.4 + jdk8
null / nonnull for generics is unusable with eclipse 4.4 + jdk8 [message #1384369] Wed, 28 May 2014 20:51 Go to next message
Nicola Zanaga is currently offline Nicola ZanagaFriend
Messages: 56
Registered: July 2009
Member
Ok, i have read the related bug, but I think now it's impossible to handle null/nonnull with eclipse 4.4 , for example:

public class Test7<@Nullable E> {
E e;

@Nullable
E test() {
return null;
}

@NonNull
E getNotNull() {
if( e == null ) throw new NullPointerException();
else return e;
}

}


error: Contradictory null specification

but in this mode i can't have a function that I call to ensure the @NonNull return.
This is a limit, not a feature, for my point of view.


In this mode, you need a continuos
if( x.test() != null ) ..
else "error"

with another pain about a warning calling twice
if( x.test() ) x.test().doSomething()

you need to write
local = x.test();
if( local != null ) local.doSomething();
else "error"

No easy way to do:
x.getNonNull().doSomething() ?
Re: null / nonnull for generics is unusable with eclipse 4.4 + jdk8 [message #1386208 is a reply to message #1384369] Sat, 14 June 2014 21:44 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
I confirm we have a bug here: even though 'E' is declared to be a nullable type, it should be possible to use the nonnull variant of this type by saying '@NonNull E'.

I made a similar observation in https://bugs.eclipse.org/bugs/show_bug.cgi?id=434602#c4 , I will make a link so we don't forget to address this example in the bug.

OTOH, the following is accepts just fine:

import org.eclipse.jdt.annotation.*;

public class Test7<E> {
	E e;

	@Nullable E test() {
		return null;
	}

	@NonNull E getNotNull() {
		E el = e;
		if (el == null)
			throw new NullPointerException();
		else
			return el;
	}
}


I made two relevant changes:

  • remove null annotation from the declaration of 'E' to let each usage decide.
  • pull field 'e' into a local variable prior to doing the null check


the latter change is needed to include the "if (e==null)"-check into flow analysis (given that fields can be accessed by anybody, not just the current method body).

best,
Stephan
Re: null / nonnull for generics is unusable with eclipse 4.4 + jdk8 [message #1403303 is a reply to message #1386208] Tue, 22 July 2014 18:45 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
FYI:

After fixing several bugs in this area, the example in this thread still didn't compile. I then filed and fixed https://bugs.eclipse.org/440143 .

best,
Stephan
Previous Topic:Eclipse will not show everything in project folder
Next Topic:Eclipse crashing on startup
Goto Forum:
  


Current Time: Tue Apr 23 14:52:08 GMT 2024

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

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

Back to the top