Search code examples
javaeclipseannotationsnon-nullablechecker-framework

@Nonnull fields in an init method


Is there a way to get Eclipse to realize that @Nonnull fields have been initialized in some initialization method. I'm looking for something like @EnsuresNonNull({"bar"}) in the otherwise completely unusable Checker Framework.

Example:

import javax.annotation.Nonnull;
public class Foo{
    @Nonnull String foo;
    public Foo(){
        init();
    }
    //need something like @EnsuresNonNull({"foo"});
    public void init(){
        foo="foo";
    }
}

Getting the following error:

The @Nonnull field foo may not have been initialized

Solution

  • Eclipse doesn't currently have this capability, to the best of my knowledge. You can see its documentation for a list of supported annotations.

    Eclipse currently offers a more limited set of annotations than the Checker Framework. That means that a programmer cannot express as many facts about their program, so the Eclipse tool issues more false positive warnings that you have to view or unsoundly suppress. On the other hand, the Eclipse tool is faster and better-integrated with the development environment; it's a tradeoff regarding which one you want to use.

    The Eclipse developers are constantly working to improve their tool, and they are friendly. Therefore, I'm sure they would welcome your suggestions or patches.

    It's a bummer that the Checker Framework didn't work out for you. Your experience is not typical: Google runs the Checker Framework on hundreds of projects every day, and there are plenty of other success stories. You could always submit a bug report; the Checker Framework developers are friendly, too.