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
Solved

How to know which function called a function in java

Posted on 2014-11-27
13
142 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
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 11

Accepted Solution

by:
Radek Baranowski earned 250 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 11

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 250 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 27

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 27

Assisted Solution

by:dpearson
dpearson earned 100 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 150 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
type mismatch (Object[] to double[] 4 33
Adding multiple JVM environments to RedHat 6 7 46
Bot application - advice 3 38
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

840 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