I'm trying to get some runtime information to do some debugging. Is there a way to access the parameters actually passed to a method?
I know the calling class and method, but not the parameters and their value at runtime. Any Ideas how to solve this? Thanks in advance.
My code so far:
//get the class I wanna know something about out of the call history
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
String callerMethod = stacktrace[2].getMethodName();
//load the class to get the information
Class<?> c = Class.forName(stacktrace[2].getClassName());
Method[] methods = c.getMethods();
Class<?>[] parameters = methods[1].getParameterTypes();
//get the parameter data
(parameters[y].isArray()? parameters[y].getComponentType() : parameters[y]).getName();
//TODO: how about the parameter value at runtime?
The code has to be generic for all Classes I will implement. Example of usage:
public class doClass() {
public void doSomething(Object o) {
//here comes my debug line
magicDebugger(level);
}
}
level
is the switch/trigger to activate console/db/file/mail output or whatever of some information
I expect following output (maybe there's some System.out.println in the magicDebugger class):
[debug] caller: com.company.package.doClass.doSomething(Object o); value of o at runtime = java.lang.String "test"