determine calling function, java file, linenumber

is it possible from within my function body, to find the calling function, line number, java file ?

thanks :)
muskad202
LVL 2
muskad202Asked:
Who is Participating?
 
lwinkenbCommented:
You can use Thread.dumpStack() to dump the thread's stack.
0
 
muskad202Author Commented:
Thread.dumpStack() would print the entire thing to the console ... i'm looking for something that i can use programatically (e.g., if calling function == "some function" then do something) ..
thanks anywayz :)
0
 
lcwidingCommented:
Actually, dumpStack() can work, but you first need to redirect System.out to a stream object that you define, so that you can monitor what is printed there. Generally, your filter could just echo to the console, and when a flag is set, copy the stack dump to a vector for later processing. Something like:

class MyPrintStream extends PrintStream
{
    PrintStream copy = null;
    PrintStream out;

   public MyPrintStream(PrintStream out)
   {
      this.out = out;
   }

   //
   // use this as a model for as many of the PrintStream methods as you feel need to be filtered.
   //
   public void write(int b)
   {
      out.write(b);
      if (copy != null)
      {
         copy.write(b);
      }
   }

   public setCopy(PrintStream copy)
   {
      this.copy = copy;
   }
}

MyPrintStream mps = new MyPrintStream(System.out);
System.setOut(mps);

//
// when it is time to check the call stack
//
ByteArrayOutputStream boas = new ByteArrayOutputStream();
mps.setCopy(new PrintStream(boas));
new Exception().printStrackTrace();
mps.setCopy(nuill);
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
muskad202Author Commented:
was thinking along similar lines ... but the idea is certainly "innovative" :)
thanks :D
muskad202
0
 
muskad202Author Commented:
one thing .. how do I revert back to the standard output stream ?
0
 
lcwidingCommented:
You can revert to the standard output stream with:

System.setOut(mps.out);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.