How to find in a method being called, which class & method invoked it?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to find in a method being called, which class & method invoked it?

  1. #1
    Join Date
    Jan 2006
    Location
    Sydney
    Posts
    49

    How to find in a method being called, which class & method invoked it?

    At times (chain-of-responsibilty pattern, debugging etc), it is very handy to know in a method being called, which method invoked it. For example You may have an inheritance class hierarchy , which has got a method overridden by its subclasses. Let us look at some sample code to see how this can be achieved using StackTraceElement and Throwable :

    The base caller class:

    Code:
    public class CallerBase { 
    
    public void methodC1() { 
    new Callee().doSomething(); 
    } 
    
    public static void main(String[] args) { 
    CallerBase caller = new CallerBase(); 
    caller.methodC1(); 
    
    caller = new CallerSub(); 
    caller.methodC1(); 
    } 
    }

    The subclass of the caller class:

    Code:
    public class CallerSub extends CallerBase { 
    
    public void methodC1() { 
    new Callee().doSomething(); 
    } 
    }
    finally the important part of the code which determines who invoked it:

    Code:
    public class Callee { 
    
    public void doSomething() { 
    Throwable t = new Throwable(); 
    StackTraceElement[] elements = t.getStackTrace(); 
    
    String calleeMethod = elements[0].getMethodName(); 
    String callerMethodName = elements[1].getMethodName(); 
    String callerClassName = elements[1].getClassName(); 
    
    System.out.println("CallerClassName=" + callerClassName + " , Caller method name: " + callerMethodName); 
    System.out.println("Callee method name: " + calleeMethod); 
    } 
    }
    If you run the class CallerBase then the output is:

    CallerClassName=CallerBase , Caller method name: methodC1
    Callee method name: doSomething
    CallerClassName=CallerSub , Caller method name: methodC1
    Callee method name: doSomething

    -- from the author of the book "Java/J2EE Job Interview Companion" at http://www.lulu.com/java-success
    Arul

  2. #2
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    62
    What is your question exactly?

    Kind regards,
    Noel
    Efficiency is intelligent laziness

Similar Threads

  1. Replies: 2
    Last Post: 04-18-2007, 03:34 AM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  3. How to execute a method of the class loaded
    By naveenkumarg1 in forum Java
    Replies: 1
    Last Post: 11-18-2005, 08:13 PM
  4. Replies: 0
    Last Post: 04-26-2001, 11:01 PM
  5. Question with ADO Find method...
    By Ted Young in forum Database
    Replies: 0
    Last Post: 11-27-2000, 09:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center