How to know which function called a function in java

HI,
I have a function A() and B() both of which makes call to a function C() from inside them.

Now when i am inside C() how can i know if the call came from A() or B().
What are the possible ways i can achieve it.

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Radek BaranowskiFull-stack Java DeveloperCommented:
run your program, do a thread dump with kill -3 <pid>, open the dump in thread dump analyzer and look at stack.

or,

implement some clever dummy logging for dev puprposes and print out call order to the log or console.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rohit BajajAuthor Commented:
I want to know about the function from inside C(). That is if A() called me then do ....if B() called me then do ....
0
CEHJCommented:
There's something wrong with your design if you need to know/do that. The reason you can be sure of that is that

a. there's no mechanism that can tell you that without the use of some horrible kludge like parsing the call stack
b. absence of said mechanism == absence of need for said mechanism
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Radek BaranowskiFull-stack Java DeveloperCommented:
Just trace down place in A() when you call C()

then just one line above put

System.our.println("I am calling C right now.");

Open in new window


do the same in B()

you can also put in C()

System.our.println("I am in C right now.");

Open in new window


then run the whole thing and check out log/console to see what is message order.

You can also add to C() declaration a parameter String whoCalledMe;

then invoke C  from A with

C(<other args>,"A called me");

Open in new window


and from B

C(<other args>,"B called me");

Open in new window

and from C do

System.our.println(whoCalledMe);

Open in new window


and you'll see it in the log/console
0
dpearsonCommented:
I think we may be making the solution of this too hard.  It may just be that you can add a parameter to C (a bit like Radek suggested).  So your code becomes:

C(boolean doExtraSteps) {
    if (doExtraSteps) {
        // We know we were called by A and now do the extra steps
    } else {
       // We know we were called by B and do whatever B requires
    }
}

And modify the callers to pass in this extra flag:

A(true) ;
B(false) ;

In general, the idea is you pass something into C() which indicates how it was called.  That's the general principle.

Doug
0
awking00Commented:
Can you post the call from A to C?
0
awking00Commented:
I didn't read dpearson's response very closely, but I believe he has the right idea. Add a boolean value of true to the call from A or false to the call from B and include a boolean parameter in C. My initial idea was to just modify the call from A and overload the C method to do A stuff with one set of parameters and B stuff with another, but that would require unnecessary effort.
0
CEHJCommented:
Can anyone show me a real world example in the context of professional software of having to pass parameters to determine which method has called which?
0
awking00Commented:
CEHJ asks a very pertinent question. To have a method that needs to determine what other method called it seems to be of poor design at best.

Robinsuri,
Can you post the calling statements to C from A and B describing any parameter types passed? Also, give a literal explanation of why  C needs to know whether it was called by A or B.
0
Rohit BajajAuthor Commented:
Hi,
This question came to my mind when i was working on android. I was on the lines that onResume() method is called from onPause() method or onStart() method. So i wanted to know which of  onPause() or onStart() called onResume(). But this thinking was wrong. onResume() is indeed called after onStart() but it does not call it.

So the original way i came to this question was wrong. Now having any practical use case for such a thing.
0
CEHJCommented:
Ah that's different then. For that, you can do

new Throwable().printStackTrace();

Open in new window

0
dpearsonCommented:
Can anyone show me a real world example in the context of professional software of having to pass parameters to determine which method has called which?
I'm not sure if this question was aimed at my response, but I was simply suggesting the normal role that all parameters play - we parameterize methods just so we can produce different behavior when they're called from different parent methods.  And so of course the inverse is true - if you wish to know - you can work back to how you were called (and hence who called you) by inspecting the parameters.

in any case, for an Android app the onResume() etc. methods are all part of the framework, so you can't change the signatures and pass any parameters.  This really was more of a "stack trace" type question and CEHJ has posted the way we dump stack traces in Java (yep - it's ugly!).

Doug
0
CEHJCommented:
I'm not sure if this question was aimed at my response

No, it was really because i was concerned that the question was being asked as a result of design problems.
That's what can happen when the goal is unstated: http://technojeeves.com/index.php/aliasjava1/15-smart-questions
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.