The Artima Developer Community
Sponsored Link

Java Buzz Forum
Faster stack traces

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Norman Richards

Posts: 396
Nickname: orb
Registered: Jun, 2003

Norman Richards is co-author of XDoclet in Action
Faster stack traces Posted: Jan 18, 2005 8:59 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Norman Richards.
Original Post: Faster stack traces
Feed Title: Orb [norman richards]
Feed URL: http://members.capmac.org/~orb/blog.cgi/tech/java?flav=rss
Feed Description: Monkey number 312,978,199
Latest Java Buzz Posts
Latest Java Buzz Posts by Norman Richards
Latest Posts From Orb [norman richards]

Advertisement

I was digging around in some JVM classes and encountered an interesting way to look up the call stack. Normally you would need to create a stack trace from an exception like this:

    public static void bar() {
        System.out.println("Bar was called by: " + 
            (new Throwable()).getStackTrace()[1].getClassName());
    }

That's safe and portable but also slow and ugly. You can get some nice debugging information, potentially, but you can't can't the actual class object. On the other hand, there is sun.reflect.Reflection.getCallerClass(), which gives you the actual class very easily:

    public static void foo() {
        System.out.println("Foo was called by: " +
            sun.reflect.Reflection.getCallerClass(2));
    }

That's entirely proprietary to the Sun JVM and subject to change at any release. but, it's nice to have around as a quick hack if you need it. The argument to the method specifies how far up the stack trace to look, starting with the Reflection class at 0. So, the current class is 1. The calling class is 2, etc...

Read: Faster stack traces

Topic: The very best company to work for in 2005 Previous Topic   Next Topic Topic: My Earliest USENET Post

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use