-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Generalizing MoreTypes.isTypeOf()
#1314
base: main
Are you sure you want to change the base?
Generalizing MoreTypes.isTypeOf()
#1314
Conversation
8d39755
to
20f57d5
Compare
I'm not really sure about this. First of all, I don't think we should change the meaning of We could introduce a new method, say A further reason for hesitation is that methods like the existing I'd be interested in more details of your use cases, and also in other people's opinions here. |
I think the name Addressing your two concernsUsing
|
20f57d5
to
a878c43
Compare
Previously, `MoreTypes.isTypeOf(Class<?> clazz, TypeMirror type)` checked if the `type` represents the exact same type as the given class. The shortcomings were: 1. The hierarchy of types were ignored. For example, if the `type` represents the type of `ArrayList`, then `isTypeOf(Collection.class, type)` will return false. 2. If the `type` is of `TypeVariable` type, `IllegalArgumentException` is thrown. 3. If the `type` is of `Wildcard` type, `IllegalArgumentException` is thrown. The new generalized implementation addresses these shortcomings. If the previous behavior for checking the exact type is required, `MoreTypes.isExactTypeOf()` can be used.
With the generalization of `isTypeOf()`, in `SuperficialValidation` is makes sense to use `isExactTypeOf()` instead.
The `MoreTypesIsTypeOfTest` is enhanced to adapt to the generalization of `MoreTypes.isTypeOf()`.
a878c43
to
90e383e
Compare
The Generalization
In this pull request, I am proposing a generalization that I found very useful in my work with annotation processors.
Currently,
MoreTypes.isTypeOf(Class<?> clazz, TypeMirror type)
checks if thetype
represents the exact same type as the given class or not.This has the following shortcomings:
type
represents the type ofArrayList
, thenisTypeOf(Collection.class, type)
will return false.type
is ofTypeVariable
type,IllegalArgumentException
is thrown.type
is ofWildcard
type,IllegalArgumentException
is thrown.The new generalized implementation addresses these shortcomings.
Now, the first shortcoming is no longer an issue (which I find very helpful). And for the other two shortcomings, I had experiences that I just wanted to check the type arguments passed to a class or method. With the current method, a casting and extracting the type is required, and it also breaks the flow of writing an
stream()
.The Exact Type (Previous Behavior)
If the previous behavior for checking the exact type is required,
MoreTypes.isExactTypeOf()
can be used.Naming, and Backward Compatibility
The yes-instances of the old implementation are included in the yes-instances of the new implementation. For the no-instances, I believe the usage of
isTypeOf()
to mean the exact same type should be extremely rare. For this reason, I do not think backward compatibility should be an issue. However, if it is, instead of renaming the old implementation asisExactTypeOf()
we can leave it as is and give a new name for the generalized method. In this case please feel free to suggest a name. As one suggestion maybeisHierarchicalTypeOf()
(?).Changes
Note: The few Javadoc changes are done by Google's formatting plugging for the Intellij.
isTypeOf()
and introducesisExactTypeOf()
, and changes the corresponding unit-test present inMoreTypesTest
.SuperficialValidation
might be one of those rare cases where the use ofisExactTypeOf()
is more appropriate. The second commit applies these changes.isTypeOf()
are added to include tests for the new capabilities.