Access method arguments dynamically in Java 7
Is it possible to get method argument values at execution time in Java 7 without using exp开发者_开发百科licit bytecode manipulation tools and other frameworks?
I need it for my javassist logging framework.
public void foo(String arg1, String arg2){
//injected code
Object[] args;
args = ???;//get arg1 and arg2 values in current method context
Logger.logMethodArgs(args);
//end of injected code
...
}
Basically, at that kind of level nothing has changed, the only language level changes are in project coin.
Of course, in the above code you can change String arg1, String arg2
into String ... args
, but that does not work with arguments with different types. Another way around this is to have your IDE (e.g. Eclipse) use the information within the AST (abstract syntax tree) to generate the logging statements. Any parsing IDE should be able to get this right. If you are using a text editor, then programmer can be required to do a bit more and iterate through the number of arguments himself.
http://paranamer.codehaus.org gives you the parameter names. Its another framework though. I use the 'shade' plugin for Maven, to pull the classes into the thing I'm building though so that there's no transitive dependencies for end-users.
精彩评论