Solved

determine calling function, java file, linenumber

Posted on 2004-10-31
370 Views
Last Modified: 2008-01-09
is it possible from within my function body, to find the calling function, line number, java file ?

thanks :)
muskad202
0
Question by:muskad202
    6 Comments
     
    LVL 5

    Accepted Solution

    by:
    You can use Thread.dumpStack() to dump the thread's stack.
    0
     
    LVL 2

    Author Comment

    by:muskad202
    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
     
    LVL 4

    Expert Comment

    by:lcwiding
    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
     
    LVL 2

    Author Comment

    by:muskad202
    was thinking along similar lines ... but the idea is certainly "innovative" :)
    thanks :D
    muskad202
    0
     
    LVL 2

    Author Comment

    by:muskad202
    one thing .. how do I revert back to the standard output stream ?
    0
     
    LVL 4

    Expert Comment

    by:lcwiding
    You can revert to the standard output stream with:

    System.setOut(mps.out);
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Course: Foundations of Front-End Development

    Jump-start a lucrative career in front-end web development, with zero previous coding experience required. This course covers the basic programming concepts and languages required for creating engaging websites from scratch.

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now