[Prévia] [Próxima] [Prévia por assunto] [Próxima por assunto]
[Índice cronológico] [Índice de assunto]

RE: JDC Tech Tips, May 7, 2002 (File Channels, Stack Trace Elements)



Eles estao acrescentando um pouquinho mais de reflexao a Java.

Será que daria prá usar isso para implementar um agente móvel que leva consigo
o estado da pilha de uma JVM para outra? Meio difícil, né?

JDC Tech Tips writes:
 > STACK TRACE ELEMENTS
 > 
 > The standard Java library has long had a mechanism for displaying
 > stack tracebacks using the Throwable.printStackTrace method. This
 > method is used to dump the program context for uncaught exceptions. 
 > The trace is printed on the System.err stream or on a specified
 > PrintStream or PrintWriter.
 > 
 > A new library feature gives you programmatic access to stack
 > tracebacks. You can retrieve an array of StackTraceElement 
 > objects, with each object representing a single stack frame in 
 > a trace. Let's look at an example to see how this works:
 > 
 >     class A {
 >         B bref;
 >     
 >         void f() {
 >             bref.g();
 >         }
 >     }
 >     
 >     class B {
 >         void g() {}
 >     }
 >     
 >     class C {
 >         String str;
 >         int len = str.length();
 >     }
 >    
 >     public class TraceDemo1 {
 >     
 >         // dump a single stack trace element
 >     
 >         static void dumpTraceElement(
 >                                StackTraceElement ste) {
 >             System.err.println("filename = " +
 >                 ste.getFileName());
 >             System.err.println("line number = " +
 >                 ste.getLineNumber());
 >             System.err.println("class name = " +
 >                 ste.getClassName());
 >             System.err.println("method name = " +
 >                 ste.getMethodName());
 >             System.err.println("is native method = " +
 >                 ste.isNativeMethod());
 >         }
 >     
 >         // dump an array of stack trace elements, 
 >         // most recent first
 >     
 >         static void dumpTrace(Throwable e) {
 > 
 >             // display exception
 > 
 >             System.err.println("Exception: " + e);
 >             System.err.println();
 >     
 >             // display traceback
 > 
 >             StackTraceElement ste[] = e.getStackTrace();
 >             for (int i = 0; i < ste.length; i++) {
 >                 dumpTraceElement(ste[i]);
 >                 System.err.println();
 >             }
 >         }
 >     
 >         public static void main(String args[]) {
 >     
 >             // call A.f() and trigger an exception
 >     
 >             try {
 >                 A aref = new A();
 >                 aref.f();
 >             }
 >             catch (Throwable e) {
 >     
 >                 // display regular stack trace
 >     
 >                 e.printStackTrace();
 >                 System.err.println();
 >     
 >                 // dump stack trace in custom format
 >     
 >                 dumpTrace(e);
 >             }
 >     
 >             System.err.println();
 >             System.err.println(
 >                      "==============================");
 >             System.err.println();
 >     
 >             // trigger an exception in initialization
 >     
 >             try {
 >                 new C();
 >             }
 >             catch (Throwable e) {
 >                 dumpTrace(e);
 >             }
 >         }
 >     }
 > 
 > In this example, the TraceDemo1 program first calls the A.f 
 > method. That method, in turn, calls B.g. Unfortunately, when B.g 
 > is called, the B object reference is null. This triggers an 
 > exception.
 > 
 > The first thing the program prints is the regular stack trace, 
 > which looks like this:
 > 
 >     java.lang.NullPointerException
 >         at A.f(TraceDemo1.java:5)
 >         at TraceDemo1.main(TraceDemo1.java:61)
 > 
 > Then it displays a customized stack traceback, with the exception
 > name and a sequence of StackTraceElements:
 > 
 >     Exception: java.lang.NullPointerException
 >     
 >     filename = TraceDemo1.java
 >     line number = 5
 >     class name = A
 >     method name = f
 >     is native method = false
 >     
 >     filename = TraceDemo1.java
 >     line number = 61
 >     class name = TraceDemo1
 >     method name = main
 >     is native method = false
 > 
 > Notice that the filename, class name, and method name of the 
 > first StackTraceElement refer to the point of the exception. The
 > exception occurred at line 5 of TraceDemo1.java, within method 
 > A.f.
 > 
 > The second part of the example shows what happens when an 
 > exception is thrown during instance initialization. In this 
 > example, the TraceDemo1 program creates a C object. When the C
 > object is instantiated, it attempts to take the length of the str
 > string. However, because str is never explicitely initialized, 
 > the reference to str is a null reference. Here's the output for 
 > that part of the example:
 > 
 >     Exception: java.lang.NullPointerException
 >     
 >     filename = TraceDemo1.java
 >     line number = 15
 >     class name = C
 >     method name = <init>
 >     is native method = false
 >     
 >     filename = TraceDemo1.java
 >     line number = 83 
 >     class name = TraceDemo1
 >     method name = main
 >     is native method = false
 > 
 > The point of the exception is line 15 of TraceDemo1.java, in the 
 > C class, in a method called <init>, which is a special name for 
 > instance initialization methods within the Java virtual machine*. 
 > 
 > You can use the StackTraceElement feature to implement customized 
 > exception reporting and logging formats. An example of 
 > a customized logging format is one that limits the stack trace to 
 > a manageable depth. For example, if there are 500 stack frames, 
 > you might want to preserve the first ten and the last ten. The 
 > StackTraceElement information is part of the serialized 
 > representation of Throwable class instances, so it's available 
 > for deserialized objects.
 > 
 > For more information about stack trace elements, see the
 > description of the StackTraceElement class at
 > http://java.sun.com/j2se/1.4/docs/api/java/lang/StackTraceElement.html.
 > Also, see section 10.12, Threads and Exceptions, in "The Java(tm) 
 > Programming Language Third Edition" by Arnold, Gosling, and Holmes 
 > http://java.sun.com/docs/books/javaprog/thirdedition/.
 > 
 > .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
 > 
 > IMPORTANT: Please read our Terms of Use, Privacy, and Licensing 
 > policies:
 > http://www.sun.com/share/text/termsofuse.html
 > http://www.sun.com/privacy/ 
 > http://developer.java.sun.com/berkeley_license.html
 > 
 > * FEEDBACK
 >   Comments? Send your feedback on the JDC Tech Tips to: 
 >   PROTECTED
 > 
 > * SUBSCRIBE/UNSUBSCRIBE
 >   - To subscribe, go to the subscriptions page,
 >     (http://developer.java.sun.com/subscription/), choose
 >     the newsletters you want to subscribe to and click "Update".
 >   - To unsubscribe, go to the subscriptions page,
 >     (http://developer.java.sun.com/subscription/), uncheck the
 >     appropriate checkbox, and click "Update".
 >   - To use our one-click unsubscribe facility, see the link at 
 >     the end of this email:
 >     
 > - ARCHIVES
 > You'll find the JDC Tech Tips archives at:
 > 
 > http://java.sun.com/jdc/TechTips/index.html
 > 
 > 
 > - COPYRIGHT
 > Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 > 901 San Antonio Road, Palo Alto, California 94303 USA.
 > 
 > This document is protected by copyright. For more information, see:
 > 
 > http://java.sun.com/jdc/copyright.html
 > 
 > This issue of the JDC Tech Tips is written by Glen McCluskey.
 > 
 > JDC Tech Tips 
 > May 7, 2002
 > 
 > Sun, Sun Microsystems, Java, and Java Developer Connection are 
 > trademarks or registered trademarks of Sun Microsystems, Inc. 
 > in the United States and other countries.
 > 
 > * As used in this document, the terms "Java virtual machine" 
 >   or "JVM" mean a virtual machine for the Java platform.
 > 
 > To use our one-click unsubscribe facility, select the following URL:
 > http://bulkmail.sun.com/unsubscribe?15005005677150047