nonnull analysis on class fields [message #1101673] |
Wed, 04 September 2013 16:08  |
Eclipse User |
|
|
|
I think that the analysis on class field is wrong.
test1 give no warning
test2 give null type safety
I don't see a motivation for that.
If it's because another thread can set null the field, test3 should work fine (now give a warning)
test4 give also a warning
test5 is a solution to clear warning, but it is more dangerous that test4, in fact you need to pass by a local variabile, initialize a local variabile and remember to set the class field !
I don't think is a good idea create dangerous code to bypass a warning
import org.eclipse.jdt.annotation.NonNull;
public class TestCase {
public Object o;
@NonNull
public Object test1() {
Object local = new Object();
return local;
}
@NonNull
public Object test2() {
o = new Object();
return o;
}
@NonNull
synchronized public Object test3() {
o = new Object();
return o;
}
@NonNull
public Object test4() {
if( o != null )
return o;
o = new Object();
return o;
}
@NonNull
public Object test5() {
Object local = o;
if( local != null )
return local;
local = new Object();
// remember to set
// o = local
return local;
}
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.06634 seconds