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

return 'net.jodah.typetools.TypeResolver$Unknown' #58

Open
luckypolaris opened this issue Sep 11, 2019 · 1 comment
Open

return 'net.jodah.typetools.TypeResolver$Unknown' #58

luckypolaris opened this issue Sep 11, 2019 · 1 comment

Comments

@luckypolaris
Copy link

luckypolaris commented Sep 11, 2019

Hi,bro:
I try to get the raw type from lambda expression like this:

public interface Callback<T> { void handler(List<T> result); }

Callback<String> callback = result -> { //do something }; Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Callback.class, callback.getClass());
but I get the error type such as 'net.jodah.typetools.TypeResolver$Unknown'

so,how can I do?

@scruel
Copy link

scruel commented Nov 9, 2021

One level Class won't be able to get the actual generic type at runtime in its class, cause when source code file(.java) compile to bytecode file(.class), will erase T to java.lang.Object(T:Ljava/lang/Object) rather than actual type you defined in other place of your code.
By the way, if subclasses write the actual type arguments(or bound type parameter) when extends generic type class, then such generic info will be saved in bytecode's class signature when you compile them. As a result, you can get the actual type from them.
See: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3

In conclusion, Callback<String> and Callback<Integer> are the same from the Callback class bytecode side(and runtime) view, so unless you tell it which type you are using(by invoking its method), reflection can only get the type as java.lang.Object from Callback itself.

@jhalterman This issue can close now.
But I think this method should return an Object.class(or throw an unsupported exception) rather than return an unknown object.

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