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

Why does FromStringConverter.convertFromString have a Class param? #12

Closed
fleaflicker opened this issue May 22, 2015 · 2 comments
Closed

Comments

@fleaflicker
Copy link

This is not a bug, but a general design question.

Why does the convertFromString take a Class<? extends T> parameter?

It's used in two places:

  • EnumStringConverter -- When would the specified cls differ from the converter's effectiveType?
  • MethodsStringConverter -- When would a caller want to specify a different cls param?

Thanks, I couldn't find an explanation in the docs or in the source.

@jodastephen
Copy link
Member

The parameter is used to make generics work - to have a return type of T you have to pass something in that is related, Class<T> in this case.

For the "effective type" and why it differs, see "effective type" in the user guide.

@fleaflicker
Copy link
Author

I'm still a little confused. I don't see when a FromStringConverter<T> or its caller would need to use the specified cls type instead of its instance's type <T>.

Can you give an example? (I mentioned MethodsStringConverter and EnumStringConverter because those are the only instances that use the specified cls type and I thought they could be implemented without it. All other implementations ignore the cls parameter.)

Edit: I see now that it's used to throw the ClassCastException immediately when the caller attempts to convert to a subclass (tested in TestStringConvert.test_convert_annotationSuperFactorySubViaSub2). Without it, the exception would only be thrown if the caller attempted to assign the result.

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

No branches or pull requests

1 participant