Solved

How to know which function called a function in java

Posted on 2014-11-27
13
141 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 26

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 26

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javap bin 2 34
javap not working 8 46
how to see all occupied ports on windows 10 laptop 15 65
hibernate jars 4 30
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

777 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