• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object says "Who's asking?" before executing method

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to search though a path list, and find out what object called the method currently running.
Thanks,
Tom
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One quick and dirty way to do this is have the method throw an Exception, and then wrap it in a try catch and print the stack trace. Below is a test class to demonstrate.
Julia
import java.lang.Exception;
public class Throw{
public static void main(String[] args){
try{
doWork();
}catch(Exception ex){
ex.printStackTrace();
}
}

public static void doWork() throws Exception{
try{
System.out.println("doWork");
}catch(Exception ex){
}finally{

throw new Exception();
}
}
}//end Throw
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Several points here:
(1) No need to throw and catch an exception here - just create a Throwable and print (or analyze) its stack trace (see examples in (2) and (3)).
(2) If you're using a JDK older that 1.4, see this article.
(3) If you're using JDK 1.4, analyzing a stack trace is much nicer. Here's a sample:

You can modify this to only look at part of the stack trace. I suspect you'll probably be most interested in the name of the class that had the method that called the method that has the StackTraceAnalyzer.show() method call. This would be element 2.
(4) Unfortunately, the stack trace can not tell you which object was involved in a call - it only gives you the class name (along with method and line number). If you need to know the identity of the calling object, you'll need another strategy. If you're just trying to identify the calling object of the current method, that's easy - the "this" keyword gives you a reference to that object. However to find objects associated with other methods from other classes (or static methods of the same class) in the call stack, it's much harder. The only way I can think of is, if you really need this information for a particular method, change its parameter list to include a reference to the associated calling object. This is a pain in the butt, and will only work for those methods you modify - it's not a general solution. Moreover it makes the API confusing, as you have to make clear to other programmers what this extra object is which they're supposed to include. And if they include the wrong one, or null, or they lie - nothing you can do about it.
[ December 11, 2002: Message edited by: Jim Yingst ]
reply
    Bookmark Topic Watch Topic
  • New Topic