-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Improve creation of ParameterizedType
#2375
Merged
eamonnmcmanus
merged 2 commits into
google:main
from
Marcono1234:marcono1234/parameterized-type-improvements
Jun 23, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 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
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
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
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.
I ran #2376 against all of Google's internal tests and found one failure where someone was calling this method with a non-generic type and an empty
typeArguments
array. Of course then it is just the same as callingget(NonGeneric.class)
, but I think this case should continue to work. It's something of a historical quirk that the reflection API represents classes with no type parameters so differently from classes with some.There were no other failures, so I think the other changes are probably safe.
This change would imply updating the
@throws
text.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.
Thanks for testing this!
Was this actually a bug in the code or is there some reason why the code was doing this? The only situation I can imagine is if the
typeArguments
is created dynamically and the direct caller ofgetParameterized
does not know whether the raw class it provides is generic; but even then I think it is a bit questionable requesting creation of aParameterizedType
when it is not actually known (and not verified) that the given raw type is generic.Are you able to share a bit more information about the code where this failed, maybe in some redacted or simplified way?
Except that
getParameterized
actually gives you aTypeToken(ParameterizedType)
where theParameterizedType
is bogus, instead of aTypeToken(Class)
. So I am really not sure if we should continue to support this.We could change
TypeToken.getParameterized
to return aTypeToken(Class)
in that case, but that could on the other hand also cause backward compatibility issues; though supportingTypeToken.getParameterized
to create bogusTypeToken(ParameterizedType)
is probably also not something we want to support.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 code I was talking about is in a test, and looks something like this:
I had not realized that
TypeToken.getParameterized
returns aParameterizedType
with no type arguments in this case. That is indeed a bizarre sort of beast, and I can't help feeling that this code works mostly by accident. Since it's test code, rather than production, and apparently nothing else in all of Google's code is doing this, I think it's probably OK to introduce this exception. We should make sure to mention it in the next release notes, though.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.
For the record, I did find another instance, in non-test code this time. I think the code in question happened to be broken for unrelated reasons when I did my earlier test, which is why I didn't see the failure then. I think it might be worth catching this case and returning
TypeToken.get(rawType)
, assuming the type-argument list is also empty.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.
Ok, have created #2447 for that. I hope that is what you had in mind.