DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

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

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links