How to access generic info on class
Let's say I have a class like this:
Is there any way to fetch the generic info on
T
, i.e. it has to be an object that extends World.
I've tried this for a while, but it's really hard to find any information on this, as searching this on the internet only gives me the good old "how do I find the generic class on runtime" I only need generic info, and it does not need to be runtime. (I'm trying generate code out of code)14 Replies
⌛
This post has been reserved for your question.
Hey @Thorinwasher! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
You can use
HelloWorld.class.getTypeParameters()
to get the T
Oh... it's that easy...
Similarly, you an use
Field#getGenericType
For Method
, there's getGenericReturnType()
and getGenericParameterTypes()
(and also getGenericExceptionTypes()
)
also Method#getTypeParameters
if you have generic types declared in the method signatureWow, thanks. I feel stupid now. It's literally been right there all the time 🤦♂️
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
finding stuff can be hard if you haven't seen it before
That's very true, I will try and use these and if it works, I will close this.
welcome to the world of type erasure. If you wanna submit a complaint get in line.
Why? What they want to do seems perfectly doable
it is, but you have to do it during runtime
Seems like this has solved my issue on that, so I will close this thread (Some other issue has surfaced now though which I have not thought about 😿 )
Thanks for the help by the way!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@270177852157067264>.