How to examine function/member argument values at run time
Is it possible to examine parameter names and values of a function inside that function?
I need a logging utility which logs not only class and function name and location in code, but also function parameters.
With class Member it is possible to get argument types, but not names or actual runtime values.
StackFrameElement does not contain this information either, or atleast it is not accessible. Function parameter values must be in stack but how can I access it?
Thanks for your help.
[519 byte] By [
qzwxecrva] at [2007-11-26 19:10:24]

# 2
It currently logs only the filename, class, function and linenumber using stack trace. See code snappets below. String returned from getLocationInCode( int ) is either logged or used e.g. as part of exception message.
public static String getLocationInCode( int stackTraceElementId ) {
...
StackTraceElement[] sTE = new Throwable().getStackTrace();
...
return stackTraceElementList[ stackTraceElementId ].getFileName() + " " +
stackTraceElementList[ stackTraceElementId ].getClassName() + "." +
stackTraceElementList[ stackTraceElementId ].getMethodName() + " (" +
stackTraceElementList[ stackTraceElementId ].getLineNumber() + ")";
}
# 3
I can see 3 possible approaches.
1. Use JNI to get at the stack. (This includes jdb-based approaches). Fairly evil.
2. Pass the parameters into the logging method. Easy but brittle.
3. Use bytecode modification. Using a well-established AOP tool such as AspectJ is probably the nicest way of doing this, although a java.lang.instrument approach may be more your cup of tea.
# 5
> Thank you! I think I try AspectJ.
I'd also point you at Springs AOP framework, since AspectJ essentially involves learning another language - kind-of - but Spring AOP is declarative and uses standard java code. AspectJ is a more complete solution, but Spring AOP will get you up-and-running faster. you'll probably even be able to get what you want right out-of-the-box