forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Annotate InvocationHandler for nullness. #74
Open
cpovirk
wants to merge
3
commits into
typetools:master
Choose a base branch
from
cpovirk:invocationhandler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks wrong to me.
@Nullable
on a formal parameter means "null is always a legal argument". (This is something that we should change in the future, such as by adding a@NullOk
that means that and changing@Nullable
to mean "there exists at least one invocation for which null is a legal argument.)That's why
Method.invoke
isn't annotated with@Nullable
. I thinkInvocationHandler.invoke
should be consistent withMethod.invoke
.Adding a
@CfComment
here, as you suggest, would definitely be useful. (And if I am confused, it would clarify my thinking.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks, I was looking at this differently. I'll lay out why I was looking at it that way, and based on what you think, I can change the signature and add a
@CfComment
.I was thinking less about the meaning of the annotations and more about the effect on the Checker Framework user. (This is probably not a promising start to my explanation ;)) And I was doing that in the light of the fact that
InvocationHandler.invoke
is a method that users implement, rather than a method that they call. This is the opposite ofMethod.invoke
. Admittedly, "implemented, not called" is not a distinction that is enforced in any way. And I even see a surprising number of calls to the method in the Google codebase (though typically just to delegate from oneInvocationHandler
to another; otherwise, to test anInvocationHandler
(which may not be the best way to do that, but setting that aside)).Based on that, my thinking is: Users who implement
invoke
will receive a nullargs
array when callers invoke no-arg methods, liketoString()
. To protect those users fromNullPointerException
, the Checker Framework could issue an error if they try to dereferenceargs
without a null check. The way for us to make that happen is to annotate the parameter with@Nullable
.For what it's worth, I managed to dig up one report of a prod bug in an Android app as a result of a missing null check. (Bonus: The bug was initially hidden because Android didn't originally pass
null
as the contract requires :)) I assume that most people are lucky enough to first hit it during development, before deploying to prod.Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Building on those thoughts:
I wondered if there were other APIs in the JDK like this -- that is, "callback" APIs in which the user is expected to implement a method, and the user may need to check a parameter for
null
in some cases. I couldn't think of much offhand. The one that came to mind wasCompletionStage.whenComplete
(inherited byCompletableFuture
) and the similarhandle
method:Using my "effect on the Checker Framework user" model, I would want to encourage the user to null-check the values before dereferencing them, so I would write:
But as with
InvocationHandler
, my@Nullable
here wouldn't really mean "null is always a legal argument." For example, it wouldn't be legal forCompletionStage<@NonNull String>.whenComplete
to invoke its callback with the arguments(null, null)
: At least one of the arguments must be non-null. The "null is always a legal argument" model says that neither type should be marked@Nullable
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other part of this that I found interesting: Does
@Nullable
on a return type mean "null is always a legal return value?" That doesn't seem to be how it's used currently. (I know you've referred in the past to "two definitions with different meanings" for@Nullable
. Perhaps the difference in usage is a result of the difference in definition?) For example:null
is a valid return value fromQueue.poll
only if the queue is empty.null
is a valid return value fromSortedSet.comparator
andSpliterator.getComparator
only if the collection uses natural order.More generally, methods rarely declare
null
to always be a legal return value, excluding the occasional case likeExecutorService.submit
:jdk/src/java.base/share/classes/java/util/concurrent/ExecutorService.java
Line 269 in aadd76c
Instead, it's almost always legal under certain conditions. This feels similar to the case we're discussing with
InvocationHandler
, only with parameters to a callback.(When it comes to parameters to methods that users call, the story is sometimes different: Methods do more frequently declare
null
to always be a valid parameter. For example, there'sString.valueOf
.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I think this all remains compatible with what you're saying: We don't have the distinction between
@Nullable
and@NullOk
currently. So, for "null sometimes" types, we prefer@Nullable
for return types and@NonNull
for parameter types, without trying to make more subjective distinctions between "APIs that users call" and "APIs that users implement.")