Using a 'has' method to indicate non-null

Answered

IntelliJ is great about telling me when an @Nullable method result isn't checked for null.

However, in some cases I have a method which will provide the same semantics. E.g., 

public class Bar {
/**
* @return true if we have a 'foo'; false otherwise
*/
public boolean hasFoo() {
// code goes here
}

@Nullable
public Foo getFoo() {
// code goes here
}
}

Elsewhere in my code, I may have something like:

if ( bar.hasFoo() ) {
bar.getFoo().someMethod();
}

Is there some way to tell IntelliJ that my 'hasFoo' should serve as a null check so that the "bar.getFoo()" is caught by the inspection?

 

Thanks,

 

jay

1 comment
Comment actions Permalink
Official comment

No, there's no such way currently.

Please sign in to leave a comment.