views:

261

answers:

11

Hey,

I am currently working on a debug/error handling tool for my Java project. This is a long-shot, but is there a way to print out the values of the arguments that are sent into a method?

The JVM must have them stored when calling the method. Anyway I can just grab hold of those values?

My goal is to just call on my own printArguments() method, which should - somehow - print out the arguments, no matter what method I just it in.

Anyone got any ideas how I can solve this? or perhaps maybe other similar information I can access? I want to get as much information as possible at a certain time, for example, the variables.

Any comments would be appreciated! :)

+1  A: 

You will need to elaborate a bit more. Are you building your own compiler from Java source to bytecode? Are you building your own bytecode interpreter? Do you want to hook into an existing bytecode interpreter? The answer entirely depends on what phase you are focusing.

If you have access to the Java source, then aspect-oriented programming might work for you. You can use it to automatically weave your printArguments call into every normal method call.

Martijn
+6  A: 

You should be able to do something with AOP

http://www.devx.com/Java/Article/30799/1954

objects
+3  A: 

This sounds like a job for AOP. If you are using Spring, there are easy solutions.

If you want to roll your solution by hand, you can use introspection to get all the informations you need. I would suggest using a Proxy (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Proxy.html) to wrap the classes you want to log and implement logging in the proxy.

Guillaume
+3  A: 

I am currently working on a debug/error handling tool for my Java project.

Are you using the JPDA? If so, you can probably get a method entry event, get the event's thread and read the arguments from the current stack frame. See this JPDA demo blog post if you are not familiar with it.

If you want to avoid running in debug mode, then (as others have stated) AOP is a good solution. You could roll your own solution using instrumentation and/or byte code manipulation, but that is a more involved approach.

McDowell
+1  A: 

Maybe VisualVM is what you are looking for.

if you are using the latest release from sun you already have visualvm

check this link for more an overview: http://java.dzone.com/articles/best-kept-secret-jdk-visualvm

Neoryder
A: 

I might have been a bit blury.

It's a standard J2SE project. Using standard compiler och runtime, nothing fancy. I want to use the J2SE library as much as possible, rather then including new libs into the project, or use an external tool like Eclipse, since it will be dist. to non-development machines.

corgrath
Stackoverflow is not a thread forum. So please do not post follow ups as an answer. Please post them as comments to the referred answer.
furtelwart
A: 

Ive read a whole bunch now, but I am still not sure what to use.

Visual VM looks like an external tool, which i am not interested in. In order to use Proxy, I have to wrap all classes in the project with it, which I am not interested into.

It boils down to JPDA and AOP, both seems way complicated then they should :) I am not sure what the easier of those two which I can use in the same runtime, no starting the application in debug mode or creating a written something in a new OAP-language.

I wish there was just a simple way to dump the method arguments, without knowing what method, number of arguments or types.

In theory, I can get the method name and the argument-types, thanks to reflection. But not their values.

Any response would be helpful.

corgrath
A: 

If you want to rely only on JDK libraries (1.4+), the only thing I can think of would be to add logging to all your methods, e.g.,

(I'm using Commons Logging syntax, since I'm not familiar with Java logging specifics)

public void foo(String bar) {
    log.debug("entering foo(" + bar + ")");
    //...
}
Jack Leow
That's if you actually know the arguments. I would like figure that out automatically.
corgrath
+1  A: 

The short answer to your original question is no. A method's arguments arrive in slots on the stack, but the method is then free to use those slots to store other things, so you can't know later on whether the contents of those slots are arguments or something else. And that's just talking about the way things are expressed in the bytecode - what actually happens in the native code could be completely different.

In any case, there's no way to reflectively get access to local variables in java, so even if the arguments were preserved, you couldn't get at them.

JPDA is the only way to non-intrusively get access to method arguments, i'm afraid.

Tom Anderson
Thanks for your comment. Ill look into JPDA :)
corgrath
+1  A: 
void PrintValues(Object... params){
   for(object o : params){
      System.out.println(o.toString());
   }
}

Then in any method you care to know the params of you could just pass them in to PrintValues like this.

void SomeMethod(String p1, int p2, StringBuilder p3){
   PrintValues(p1, p2, p3);
}

Am I oversimplifying the requirements? Sounds like what you want would really be a good fit for AOP/reflection but if you're dead set against them this might do some of what you want.

Hardwareguy
Thanks for your comment. As i wrote to Jack Leow, that requires me to specify the arguments. It's a good solution, but I rather develop something so I only need to call PrintValues() and it will automatically loop through the arguments :)
corgrath
A: 

I am not really sure about your requirements but did you take a look at BTrace. You can do exactly the thing you want to.

Additionally, you can base your debugging/error handling tool on BTrace - provide a set of scripts specific to your application and then run them in an ad-hoc manner. GalssFish v3 does something similar.

JB