Link to home
Start Free TrialLog in
Avatar of mte01
mte01Flag 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??
SOLUTION
Avatar of kawas
kawas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mte01

ASKER

I will try it guys...thanks for your help!
>> // -1 or -2, not sure.

Just 1 ;-)
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).
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
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 ...
We've all had those "patches" I think :-)
Avatar of shinobun
shinobun

>>TimYates

Make sure you synchronize where().  ;)
very true :-)
Avatar of mte01

ASKER

>>TimeYates,Kawas

Thanks for your help guys!