Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » nonnull analysis on class fields
nonnull analysis on class fields [message #1101673] Wed, 04 September 2013 20:08 Go to next message
Nicola  Zanaga is currently offline Nicola Zanaga
Messages: 23
Registered: July 2009
Junior Member
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;
    }
    
}


Re: nonnull analysis on class fields [message #1101695 is a reply to message #1101673] Wed, 04 September 2013 20:56 Go to previous message
Stephan Herrmann is currently offline Stephan Herrmann
Messages: 1024
Registered: July 2009
Senior Member
Have you seen this help page:
http://help.eclipse.org/topic/org.eclipse.jdt.doc.user/tasks/task-using_null_annotations.htm&cp=1_3_9_0

It contains a section "The case of fields" that covers some of the issues you raise.

Some background - from the time when we designed this feature - can also be found in http://wiki.eclipse.org/JDT_Core/Null_Analysis/Options (not an official statement).

A proposal to make the analysis stronger is being discussed in https://bugs.eclipse.org/414237 (comments welcome in the bug).

Some specific comments to your example:
- even test3 is unsafe, because other methods which are not synchronized on this could still set o to null concurrently.
- test5 is not dangerous in terms of NPE, but I admit that this solution requires one more thing to remember doing. It is particularly for this kind of patterns where the proposal from bug 414237 would help.

Stephan
Previous Topic:cvs check out
Next Topic:Migrate to Ecplise Compiler
Goto Forum:
  


Current Time: Sun Oct 26 03:13:30 GMT 2014

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

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