In Java, one can write code such as the following to interrogate the stack:
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
for (StackTraceElement frame : stack) {
System.out.println(frame.getClassName() + "." + frame.getMethodName());
}
Is it possible to get a reflective view of the stack, where rather than just getting the names of the classes and methods, I instead get Class and Method instances? If two methods in a class share a name (with different parameters), can I distinguish between them on the stack?
The
Classbit is easy. UseClass.forName(String):I don’t think so, since the method name returned by
StackTraceElement#getMethodName()does not contain parameter number & type information, which is what you’d need to distinguish between methods with the same name.