Skip to content
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

getTargetType() and getParameterizedTypeArguments() represents types/classes differently #11

Closed
Elisedlund opened this issue Apr 24, 2014 · 6 comments

Comments

@Elisedlund
Copy link

StringToTypeParserHelper contains:
public Type getTargetType()
public List<Class<?>> getParameterizedTypeArguments()
why doesn't getTargetType also return a class?

what I'am saying is getTargetType() and getParameterizedTypeArguments() could be aligned more.

@drapostolos
Copy link
Owner

The reason getTargetType() does not return a Class<?> is cos it could return a generic type, where java.reflection.Type contains information about it's type arguments and Class<?> doesn't.

The only reason getParameterizedTypeArguments() does not return a java.reflection.Type is that Generic types of other generic types (and so on) are not supported (perhaps for no reason?).

But, I agree with you, the method names suggests they return the same type. some solutions are:

  • Let getParameterizedTypeArguments() return a List<Type>
  • Change name to getParameterizedClassArguments()

The 1st option is perhaps more generic, or maybe add both methods? getParameterizedTypeArguments(), getParameterizedClassArguments()

@Elisedlund
Copy link
Author

"The reason getTargetType() does not return a Class<?> is cos it could return a generic type, where"
Can't see why "Type" contains more generic Information than "Class" except if you cast it to ParameterizedType

What I do now because I need the class is:
(Class<?>) helper.getTargetType();

Maybe both methods could exist.
helper.getTargetType();
helper.getTargetClass();

@drapostolos
Copy link
Owner

The reason Type is return from getTargetType is in case someone wants to cast it to a ParameterizedType and retrieve the generic information. Additionally, casting a ParameterizedType type to Class<?> will cause a ClassCastException.

Seems reasonable to add a helper.getTargetClass() method. The method could include error handling to provide a better exception/error message (rather than a ClassCastException) when casting ParameterizedType to Class<?>.

@drapostolos
Copy link
Owner

This is pushed to master:

Added new method:
public <T> Class<T> getTargetClass() which handles the casting from Class<?> to Class internally. No need for ugly unchecked castings in client code.

Renamed to:
public <T> List<Class<T>> getParameterizedClassArguments(), which now also handles the casting to Class internally.

Added new method:
public <T> Class<T> getParameterizedClassArgumentByIndex(int index)
Convenient method for retrieving elements by index position in the list as returned from above method.

@Elisedlund
Copy link
Author

Looks nice.
Tried getTargetClass() and it works as expected.

@drapostolos
Copy link
Owner

Now available in v0.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants