NPX hunting by Annotations

Hi everybody,

I have the following code:

if (!train.isUnknown()) {
final Path p = train.getISE().getPath();

Where Train has the two following methodes

@Nullable
public final InputScheduleEntry getISE() {
...

and
public final boolean isUnknown() {
return getISE() == null;
}

-


The Code Analysis tells me that there is a possible NPX in
final Path p = train.getISE().getPath();
Because getISE() is @Nullable, of course.

But that fact that this code is protected against the NPX, because it is wrapped into !isUnknown()
escapes to the inspection.

How should I go about that this kind of avoided NPX is detected as solid code?
Is this problem on Nullable propagation described somewhere?

Thanks for any explanation

Erwin

1 comment
Comment actions Permalink

Well, you're out of luck here. Even if the inspection would be clever enough to detect that isUnknown() is implemented as 'getISE() == null' it would warn. You even get a warning on this code:

if (train.getISE() != null) {
final Path p = train.getISE().getPath();

The reason is that there's no guarantee that the two different calls to train.getISE() return the same value (e.g. another thread could set the path to null between the two calls). The only way to reliably avoid a test for nullness is to write:

InputScheduleEntry ise = train.getISE();
if (ise != null) {
final Path p = ise.getPath();

0

Please sign in to leave a comment.