Using a 'has' method to indicate non-null
已回答
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
请先登录再写评论。
No, there's no such way currently.