Avatar of mte01
mte01
Flag for Lebanon asked on

Knowing the caller method

Hey experts,

 Is there a way using some reflection classes to know (at runtime) the caller method inside a certain method?? The thing is that I have a method that is called by around 35 methods, and I want to do a checking on just 3 or 4 caller methods, so this way would be better than adding a param for all 35 calls to make the check (since this change is only temporary)..any help on knowing the caller method??
Java

Avatar of undefined
Last Comment
mte01

8/22/2022 - Mon
SOLUTION
kawas

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
TimYates

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
mte01

ASKER
I will try it guys...thanks for your help!
TimYates

>> // -1 or -2, not sure.

Just 1 ;-)
kawas

Thanks Tim.

Just out of curiosity, does anyone know which one is most efficient (Resources used, time taken to execute, etc)? They all do pretty much the same thing, from what I can see, but I am a little curious as too which one is most efficient (curious because I have production code that does this).
Your help has saved me hundreds of hours of internet surfing.
fblack61
TimYates

I think they're all the same (mine may be slower as it adds all those strings together and prints them to the console) ;-)

Using a class like this might be more efficient:

  public class WhereAmI
  {
    private static Throwable cachedException = new Throwable() ;
    public static String where()
    {
      cachedException.fillInStackTrace() ;
      StackTraceElement ste = cachedException.getStackTrace()[ 1 ] ;
      return new StringBuffer( "Called by " ).
              append( ste.getClassName() ).
              append( ":" ).
              append( ste.getMethodName() ).
              append( " (" ).
              append( ste.getLineNumber() ).
              append( ")" ).toString() ;
    }
  }

As then you aren't creating a Throwable object every time...  just filling it in ;-)

I would have concerns about having this in production code though, unless it was for debug...  The flow of an application (imho) shouldn't be dependant on the route taken to get there, but by the current state of the system...

If you see what I mean ;-)

Tim
kawas

Thanks, I have received those warnings before and I agree. In my case, it was a 'temp' patch that should have been changed about a year ago ...
TimYates

We've all had those "patches" I think :-)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
shinobun

>>TimYates

Make sure you synchronize where().  ;)
TimYates

very true :-)
mte01

ASKER
>>TimeYates,Kawas

Thanks for your help guys!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck