Link to home
Start Free TrialLog in
Avatar of muskad202
muskad202

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of lwinkenb
lwinkenb

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 muskad202
muskad202

ASKER

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 :)
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);
was thinking along similar lines ... but the idea is certainly "innovative" :)
thanks :D
muskad202
one thing .. how do I revert back to the standard output stream ?
You can revert to the standard output stream with:

System.setOut(mps.out);