Find out which method called me

Go To StackoverFlow.com

3

I'm tring to find out a way to get the complete method signature that is calling me.

For example:

public class Called {

    public void whoCallMe() {
        System.out.println("Caller Method: " + new Throwable().getStackTrace()[1].getMethodName());
    }
}

public class Caller {

public static void run(int i) {
    new Called().whoCallMe();
}

public static void run(String str) {
    new Called().whoCallMe();
}

public static void run(boolean b) {
    new Called().whoCallMe();
}

/** MAIN **/
public static void main(String[] args) {
    run("hi");
}

The way I've implemented whoCallMe() method I can see that run method called it, but since I 3 overloads I can't say which one was the caller cause whoCallme return only "run" as the method name.

Do you guys know other way where I could get the complete method signature like run(java.lang.String) ?

2012-04-05 18:40
by RLM
You need the debugging api for that. You can also use a byte code tool to find the method based on the line number - biziclop 2012-04-05 18:44
Why would you need to do this - Jeffrey 2012-04-05 18:49
Maybe this could help you: http://stackoverflow.com/questions/421280/in-java-how-do-i-find-the-caller-of-a-method-using-stacktrace-or-reflectio - SWoeste 2012-04-05 18:56


1

You can use AspectJ to create an aspect which will apply to every method invocation and add the details about the method being invoked to a per-thread stack, and then remove it after method completes. It's going to be insanely expensive, of course. Most likely you don't want to do that. Also you don't want to throw anything to find out who called you.

Basically the answer to your question is: do not do it. Explain why you think you want to do it and somebody will give you a good alternative.

Also, come to think of it, you can argue that method overloading (not overriding!) is considered harmful. Do you really need multiple different methods with different arguments and the same name?

2012-04-05 18:44
by MK.
+1 for the advice to not do this.Louis Wasserman 2012-04-05 18:48
Downvoting w/o a comment is rude - MK. 2012-04-05 22:32
OK,let me explain better why I need to know which method is the caller one: Assume that I have annotated every method with some information I need to retrive from in certain time of processing.Suppose I have run() annotated as cod=1 and run(String arg) annotated as cod=2.In this case how could I know which is the cod value I should assume since I can't differentiate run from run(String) as the caller?About the harmful overloadings MK point to, I can't proibit user to do such thing (it's part of a framework we have in my work). I apreciated all the sugestions/help so far. Thanks guys - RLM 2012-04-09 19:53
I still don't have a good understanding of why you need to know who called you, but can't just require another argument to be passed - MK. 2012-04-09 20:55
Ok, lets go deeper ... when my system loads all the annotations are cached in a hash (key = method signature, value = annotation values) to improve performance avoiding reflection all the time. These annotation are used to set some log information per method. This why I need to know who is the caller method and then search its meta data on the hash cache. I could use an extra argument or stuff like that to get such log data but it's much more elegante/easy to the users simply put an annotaion to do that (elegant, not simple) :) Thanks Dude - RLM 2012-04-10 11:35
Well, when you hear 'logging' it's always a flag that this might be one of the few really legitimate uses for AOP. Have you considered AspectJ - MK. 2012-04-10 12:57
No, I haven't. But how about your first comment where you said "It's going to be insanely expensive, of course" about AspectJ? - RLM 2012-04-10 13:31
You'd have to test that. It might not be that much worse than annotating everything - MK. 2012-04-10 14:23
I'll try soon! Thanks budd - RLM 2012-04-10 18:13
Ads