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

TypeToken.mostSpecializedParameterizedSuperclass(Type type) should not return null #164

Open
tevans78 opened this issue Mar 16, 2023 · 1 comment

Comments

@tevans78
Copy link

If, for some strange reason, you do this...

TypeToken<?> t = new TypeToken() {};

then you get

java.lang.NullPointerException: Cannot invoke "java.lang.reflect.ParameterizedType.getActualTypeArguments()" because the return value of "typetoken.TypeToken.mostSpecializedParameterizedSuperclass(java.lang.reflect.Type)" is null

Given that mostSpecializedParameterizedSuperclass is a private method which is only ever called from the constructor of TypeToken, it isn't a good idea for it to ever return null. If it does ever return null (through misuse of TypeToken), the result will be a generic NPE rather than an specific exception with a helpful error message.

Suggest throwing some flavour of ConfigException rather than returning null. The exception message can be specific to the if block which is throwing it.

Alternatively, if mostSpecializedParameterizedSuperclass can return null, put a null check in the TypeToken constructor and throw the ConfigException from there. However, at this point you will have lost some context about what the (very unexpected) problem is.

Credit to @Azquelt for spotting this issue.

@jansupol
Copy link
Contributor

@tevans78 Thank you for spotting this. Feel free to provide a PR :)

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

No branches or pull requests

2 participants