What's the appropriate way to see if a ResolvedType represents a simple class that I know about?
Let's say I pass a new GenericType<String>{} to a method, and I just want to see if the generic parameter is String. I think (I haven't tried it yet) I would do this (code not optimized, for illustration):
void foo(GenericType<?> genericType) {
TypeResolver typeResolver = new TypeResolver();
ResolvedType resolvedType = typeResolver.resolve(genericType);
How would I then see if resolvedType indicates a simple String? I'm guessing I could do this:
if(resolvedType.getTypeParameters.isEmpty() && resolvedType.getErasedType().equals(String.class)) {
…
But that seems sort of awkward. Is there a better way?
(Note that the resolvedType.getTypeParameters.isEmpty() check is arguably not necessary here, as String has no generic parameters, but I'm looking for a general approach.)
Or should I be going in the other direction, resolving String.class and then comparing the resolved types?
if(resolvedType.equals(typeResolver.resolve(String.class))) {
…
I don't know how much overhead the extra resolution of String.class adds, though.
This may be related to #31.
What's the appropriate way to see if a
ResolvedTyperepresents a simple class that I know about?Let's say I pass a
new GenericType<String>{}to a method, and I just want to see if the generic parameter isString. I think (I haven't tried it yet) I would do this (code not optimized, for illustration):How would I then see if
resolvedTypeindicates a simpleString? I'm guessing I could do this:But that seems sort of awkward. Is there a better way?
(Note that the
resolvedType.getTypeParameters.isEmpty()check is arguably not necessary here, asStringhas no generic parameters, but I'm looking for a general approach.)Or should I be going in the other direction, resolving
String.classand then comparing the resolved types?I don't know how much overhead the extra resolution of
String.classadds, though.This may be related to #31.