?
Solved

How to know which function called a function in java

Posted on 2014-11-27
13
Medium Priority
?
148 Views
Last Modified: 2014-12-05
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
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 12

Accepted Solution

by:
Radek Baranowski earned 1000 total points
ID: 40468864
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
 

Author Comment

by:Rohit Bajaj
ID: 40468991
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 40469090
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 1000 total points
ID: 40469103
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
 
LVL 28

Expert Comment

by:dpearson
ID: 40469999
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
 
LVL 32

Expert Comment

by:awking00
ID: 40470458
Can you post the call from A to C?
0
 
LVL 32

Expert Comment

by:awking00
ID: 40470493
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 40470646
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
 
LVL 32

Expert Comment

by:awking00
ID: 40470713
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
 

Author Comment

by:Rohit Bajaj
ID: 40472402
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 40472697
Ah that's different then. For that, you can do

new Throwable().printStackTrace();

Open in new window

0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 400 total points
ID: 40472831
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 40473454
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

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question