I am looking to provide some convenience through reflection to programmers who will be using my code. To achieve my desired result I would like to get the class object for classes that extend my code. Even though they can’t override the static method I wish to access the information from, it would be currently helpful. I may end up simply refactoring the design a bit to avoid this situation, but wanted to toss this question out to the community.
The only way to achieve this that I am aware of is to take the current thread, navigate the stack trace, and extract the class name, then convert that into an actual instance of the class.
Any other suggestions?
Side note: the above approach is also the only way that I’m aware of getting the calling method.
When in the static method, what is the guarantee that some (any) class that has been extended from yours is on the stack?
If you think about this a little, I suspect you will realize what you are looking for does not exist.
/EDIT 1/ I think what you need is
Hmmm. What if
createisprotectedmethod? Then you could be pretty sure the caller is aB, you can even require that, throwingUnsupportedOperationorIllegalStateif the caller is not assignable toB.You can use
Thread.currentThread().getStackTrace()