src/share/classes/java/lang/Throwable.java

Print this page




 760         }
 761 
 762         void println(Object o) {
 763             printWriter.println(o);
 764         }
 765     }
 766 
 767     /**
 768      * Fills in the execution stack trace. This method records within this
 769      * {@code Throwable} object information about the current state of
 770      * the stack frames for the current thread.
 771      *
 772      * <p>If the stack trace of this {@code Throwable} {@linkplain
 773      * Throwable#Throwable(String, Throwable, boolean, boolean) is not
 774      * writable}, calling this method has no effect.
 775      *
 776      * @return  a reference to this {@code Throwable} instance.
 777      * @see     java.lang.Throwable#printStackTrace()
 778      */
 779     public synchronized Throwable fillInStackTrace() {
 780         if (stackTrace != null) {

 781             fillInStackTrace(0);
 782             stackTrace = UNASSIGNED_STACK;
 783         }
 784         return this;
 785     }
 786 
 787     private native Throwable fillInStackTrace(int dummy);
 788 
 789     /**
 790      * Provides programmatic access to the stack trace information printed by
 791      * {@link #printStackTrace()}.  Returns an array of stack trace elements,
 792      * each representing one stack frame.  The zeroth element of the array
 793      * (assuming the array's length is non-zero) represents the top of the
 794      * stack, which is the last method invocation in the sequence.  Typically,
 795      * this is the point at which this throwable was created and thrown.
 796      * The last element of the array (assuming the array's length is non-zero)
 797      * represents the bottom of the stack, which is the first method invocation
 798      * in the sequence.
 799      *
 800      * <p>Some virtual machines may, under some circumstances, omit one
 801      * or more stack frames from the stack trace.  In the extreme case,
 802      * a virtual machine that has no stack trace information concerning
 803      * this throwable is permitted to return a zero-length array from this
 804      * method.  Generally speaking, the array returned by this method will
 805      * contain one element for every frame that would be printed by
 806      * {@code printStackTrace}.  Writes to the returned array do not
 807      * affect future calls to this method.
 808      *
 809      * @return an array of stack trace elements representing the stack trace
 810      *         pertaining to this throwable.
 811      * @since  1.4
 812      */
 813     public StackTraceElement[] getStackTrace() {
 814         return getOurStackTrace().clone();
 815     }
 816 
 817     private synchronized StackTraceElement[] getOurStackTrace() {
 818         // Initialize stack trace field with information from
 819         // backtrace if this is the first call to this method
 820         if (stackTrace == UNASSIGNED_STACK) {

 821             int depth = getStackTraceDepth();
 822             stackTrace = new StackTraceElement[depth];
 823             for (int i=0; i < depth; i++)
 824                 stackTrace[i] = getStackTraceElement(i);
 825         } else if (stackTrace == null) {
 826             return UNASSIGNED_STACK;
 827         }
 828         return stackTrace;
 829     }
 830 
 831     /**
 832      * Sets the stack trace elements that will be returned by
 833      * {@link #getStackTrace()} and printed by {@link #printStackTrace()}
 834      * and related methods.
 835      *
 836      * This method, which is designed for use by RPC frameworks and other
 837      * advanced systems, allows the client to override the default
 838      * stack trace that is either generated by {@link #fillInStackTrace()}
 839      * when a throwable is constructed or deserialized when a throwable is
 840      * read from a serialization stream.


 848      * this {@code Throwable}.  The specified array is copied by this
 849      * call; changes in the specified array after the method invocation
 850      * returns will have no affect on this {@code Throwable}'s stack
 851      * trace.
 852      *
 853      * @throws NullPointerException if {@code stackTrace} is
 854      *         {@code null} or if any of the elements of
 855      *         {@code stackTrace} are {@code null}
 856      *
 857      * @since  1.4
 858      */
 859     public void setStackTrace(StackTraceElement[] stackTrace) {
 860         // Validate argument
 861         StackTraceElement[] defensiveCopy = stackTrace.clone();
 862         for (int i = 0; i < defensiveCopy.length; i++) {
 863             if (defensiveCopy[i] == null)
 864                 throw new NullPointerException("stackTrace[" + i + "]");
 865         }
 866 
 867         synchronized (this) {
 868             if (this.stackTrace == null) // Immutable stack

 869                 return;
 870             this.stackTrace = defensiveCopy;
 871         }
 872     }
 873 
 874     /**
 875      * Returns the number of elements in the stack trace (or 0 if the stack
 876      * trace is unavailable).
 877      *
 878      * package-protection for use by SharedSecrets.
 879      */
 880     native int getStackTraceDepth();
 881 
 882     /**
 883      * Returns the specified element of the stack trace.
 884      *
 885      * package-protection for use by SharedSecrets.
 886      *
 887      * @param index index of the element to return.
 888      * @throws IndexOutOfBoundsException if {@code index < 0 ||




 760         }
 761 
 762         void println(Object o) {
 763             printWriter.println(o);
 764         }
 765     }
 766 
 767     /**
 768      * Fills in the execution stack trace. This method records within this
 769      * {@code Throwable} object information about the current state of
 770      * the stack frames for the current thread.
 771      *
 772      * <p>If the stack trace of this {@code Throwable} {@linkplain
 773      * Throwable#Throwable(String, Throwable, boolean, boolean) is not
 774      * writable}, calling this method has no effect.
 775      *
 776      * @return  a reference to this {@code Throwable} instance.
 777      * @see     java.lang.Throwable#printStackTrace()
 778      */
 779     public synchronized Throwable fillInStackTrace() {
 780         if (stackTrace != null ||
 781             backtrace != null /* Out of protocol state */ ) {
 782             fillInStackTrace(0);
 783             stackTrace = UNASSIGNED_STACK;
 784         }
 785         return this;
 786     }
 787 
 788     private native Throwable fillInStackTrace(int dummy);
 789 
 790     /**
 791      * Provides programmatic access to the stack trace information printed by
 792      * {@link #printStackTrace()}.  Returns an array of stack trace elements,
 793      * each representing one stack frame.  The zeroth element of the array
 794      * (assuming the array's length is non-zero) represents the top of the
 795      * stack, which is the last method invocation in the sequence.  Typically,
 796      * this is the point at which this throwable was created and thrown.
 797      * The last element of the array (assuming the array's length is non-zero)
 798      * represents the bottom of the stack, which is the first method invocation
 799      * in the sequence.
 800      *
 801      * <p>Some virtual machines may, under some circumstances, omit one
 802      * or more stack frames from the stack trace.  In the extreme case,
 803      * a virtual machine that has no stack trace information concerning
 804      * this throwable is permitted to return a zero-length array from this
 805      * method.  Generally speaking, the array returned by this method will
 806      * contain one element for every frame that would be printed by
 807      * {@code printStackTrace}.  Writes to the returned array do not
 808      * affect future calls to this method.
 809      *
 810      * @return an array of stack trace elements representing the stack trace
 811      *         pertaining to this throwable.
 812      * @since  1.4
 813      */
 814     public StackTraceElement[] getStackTrace() {
 815         return getOurStackTrace().clone();
 816     }
 817 
 818     private synchronized StackTraceElement[] getOurStackTrace() {
 819         // Initialize stack trace field with information from
 820         // backtrace if this is the first call to this method
 821         if (stackTrace == UNASSIGNED_STACK ||
 822             (stackTrace == null && backtrace != null) /* Out of protocol state */) {
 823             int depth = getStackTraceDepth();
 824             stackTrace = new StackTraceElement[depth];
 825             for (int i=0; i < depth; i++)
 826                 stackTrace[i] = getStackTraceElement(i);
 827         } else if (stackTrace == null) {
 828             return UNASSIGNED_STACK;
 829         }
 830         return stackTrace;
 831     }
 832 
 833     /**
 834      * Sets the stack trace elements that will be returned by
 835      * {@link #getStackTrace()} and printed by {@link #printStackTrace()}
 836      * and related methods.
 837      *
 838      * This method, which is designed for use by RPC frameworks and other
 839      * advanced systems, allows the client to override the default
 840      * stack trace that is either generated by {@link #fillInStackTrace()}
 841      * when a throwable is constructed or deserialized when a throwable is
 842      * read from a serialization stream.


 850      * this {@code Throwable}.  The specified array is copied by this
 851      * call; changes in the specified array after the method invocation
 852      * returns will have no affect on this {@code Throwable}'s stack
 853      * trace.
 854      *
 855      * @throws NullPointerException if {@code stackTrace} is
 856      *         {@code null} or if any of the elements of
 857      *         {@code stackTrace} are {@code null}
 858      *
 859      * @since  1.4
 860      */
 861     public void setStackTrace(StackTraceElement[] stackTrace) {
 862         // Validate argument
 863         StackTraceElement[] defensiveCopy = stackTrace.clone();
 864         for (int i = 0; i < defensiveCopy.length; i++) {
 865             if (defensiveCopy[i] == null)
 866                 throw new NullPointerException("stackTrace[" + i + "]");
 867         }
 868 
 869         synchronized (this) {
 870             if (this.stackTrace == null && // Immutable stack
 871                 backtrace == null) // Test for out of protocol state
 872                 return;
 873             this.stackTrace = defensiveCopy;
 874         }
 875     }
 876 
 877     /**
 878      * Returns the number of elements in the stack trace (or 0 if the stack
 879      * trace is unavailable).
 880      *
 881      * package-protection for use by SharedSecrets.
 882      */
 883     native int getStackTraceDepth();
 884 
 885     /**
 886      * Returns the specified element of the stack trace.
 887      *
 888      * package-protection for use by SharedSecrets.
 889      *
 890      * @param index index of the element to return.
 891      * @throws IndexOutOfBoundsException if {@code index < 0 ||