Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

null checks in streams

18 views
Skip to first unread message

Jon Schewe

unread,
Sep 8, 2024, 9:14:20 PM9/8/24
to Checker Framework discussion
I've got the code below 

private static Optional<String> findRangeShortDescriptionWithTitle(final String title,
final Goal goal) {
...
}


...
return scoreCategory.getAllGoals().stream()//
.filter(Goal.class::isInstance)//
.map(Goal.class::cast)//
.map(g -> findRangeShortDescriptionWithTitle(title, g))//



giving the following error

/home/jpschewe/projects/fll-sw/working-dir/src/main/java/fll/documents/writers/SubjectivePdfWriter.java:853: error: [argument] incompatible argument for parameter goal of SubjectivePdfWriter.findRangeShortDescriptionWithTitle.
.map(g -> findRangeShortDescriptionWithTitle(title, g))//
^
found : @Initialized @PolyNull Goal
required: @Initialized @NonNull Goal

I expect that checker wants the goal parameter to finRangeSHortDescriptionWithTitle to have @PolyNull on it. However before that is called in the stream the "isInstance" method is called and that will filter out nulls.

Michael Ernst

unread,
Sep 9, 2024, 11:29:57 AM9/9/24
to Jon Schewe, Checker Framework discussion
Jon-

Is Goal.isInstance annotated as returning a non-null value?
Is Goal.cast annotated as returning a polynull or a non-null value?

The argument "g in findRangeShortDescriptionWithTitle(title, g)" is non-null" depends on one or both of those facts.

(More generally, the more you can tell us about your code, the more helpful we can be.)

-Mike

--

---
You received this message because you are subscribed to the Google Groups "Checker Framework discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to checker-framework-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/checker-framework-discuss/aa9ba9c9c5302ed95804e47bdd7fa3017a27c04f.camel%40mtu.net.

Jon Schewe

unread,
Sep 10, 2024, 10:14:38 PM9/10/24
to Michael Ernst, Checker Framework discussion
Goal.class.isInstance is built into the JDK, so it returns whatever checker ships with it returning.
The same goes for Goal.class.cast.

Jon Schewe

unread,
Oct 2, 2024, 11:16:04 PM10/2/24
to Michael Ernst, Checker Framework discussion
The method isInstance in java.lang.Class will return false for null. Therefore it's not possible for there to be a null in the stream pipeline after the call to isInstance. So how is the checker framework confused here? Does it not know that java.lang.Class.isInstance cannot return true for null? Or does the checker framework not track null checks through a stream pipeline?
--

---
You received this message because you are subscribed to the Google Groups "Checker Framework discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to checker-framework-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages