I need to get the name of the class in an over-ridden method which has called the method
How can it be done?
Throwable t = new Throwable();
StackTraceElement[] elements = t.getStackTrace();
Class StackTraceElement has various methods like
String calleeMethod = elements[0].getMethodName();
String callerMethodName = elements[1].getMethodName();
String callerClassName = elements[1].getClassName();
您可以使用this.getClass()
方法内部的当前对象的类,或使用Thread.currentThread().getStackTrace()
遍历调用路径。
You can get the class with:
class.getMethod("your_over-ridden_method_name").getDeclaringClass();
For example:
System.out.println(class.getMethod("your_over-ridden_method_name") + " declared by " + class.getMethod("your_over-ridden_method_name").getDeclaringClass());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.