typetools / typetools/checker-framework
Setting precondition for a overriden function
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
I work on an Android App. I am trying to improve annotations used in our code to reduce NPEs. The code I am working has many instances where a subclass overrides a function in superclass and this function uses a @Nullable or @MonotonicNonNull member variable of the subclass. Here is a make-up example
public abstract class AbstractViewFragment{
public void onLoad(){
// do something here
}
}
public class SpecificViewFragment implements AbstractViewFragments{
@Nullable private Object cursor;
@MonotonicNonNull protected Object viewDetail;
protected Object webView;
SpecificViewFragment(){
webView = new Object();
}
…
@override
public void onLoad(){
if (viewDetail.isType1()){
// do A
}else{
//do B
}
}
}
Nullness checker throws an error for using viewDetail.isType1() method as possible and in most cases, such errors are suppressed using checkNotNull(“viewDetail”) at the beginning of the SpecificViewFragment.onLoad() method and has caused NPEs as the callee of this function didn’t make sure that viewDetail is non_null.
If we try to add RequiresNonNull(“viewDetails”) to SpecificViewFragment.onLoad() it throws the error below:
error: [contracts.precondition.override.invalid] Subclass precondition is stronger for `this.var` in `overriddenFuntion`
As I understand, based on behavioural subtyping, precondition cannot be strengthened in the subtype.
But using checkNotNull is not preventing NPEs in the code either and is neither a great way to tell developers that they should be thinking about viewDetails being null. What would be a better solution in such cases?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no file, test, or entry point; start by locating handling for @RequiresNonNull and the contracts.precondition.override.invalid diagnostic. Before coding, clarify the intended supported behavior and acceptance test, since the report currently asks for guidance rather than specifying a change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- compilers, devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100