Solved

How to know which function called a function in java

Posted on 2014-11-27
13
138 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 11

Assisted Solution

by:Radek Baranowski
Radek Baranowski earned 250 total points
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
Can you post the call from A to C?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now