Throwable类

package java.lang;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Objects;
import java.util.Set;


public class Throwable
  implements Serializable
{
  private static final long serialVersionUID = -3042686055658047285L;
  private transient Object backtrace;
  private String detailMessage;
  private static final StackTraceElement[] UNASSIGNED_STACK;
  private Throwable cause = this;


  private StackTraceElement[] stackTrace = UNASSIGNED_STACK;
  private static final List<Throwable> SUPPRESSED_SENTINEL;
  private List<Throwable> suppressedExceptions = SUPPRESSED_SENTINEL;
  private static final String NULL_CAUSE_MESSAGE = "Cannot suppress a null exception.";
  private static final String SELF_SUPPRESSION_MESSAGE = "Self-suppression not permitted";
  private static final String CAUSE_CAPTION = "Caused by: ";
  private static final String SUPPRESSED_CAPTION = "Suppressed: ";
  private static final Throwable[] EMPTY_THROWABLE_ARRAY;


  public Throwable()
  {
    fillInStackTrace();
  }


  public Throwable(String paramString)
  {
    fillInStackTrace();
    this.detailMessage = paramString;
  }


  public Throwable(String paramString, Throwable paramThrowable)
  {
    fillInStackTrace();
    this.detailMessage = paramString;
    this.cause = paramThrowable;
  }


  public Throwable(Throwable paramThrowable)
  {
    fillInStackTrace();
    this.detailMessage = (paramThrowable == null ? null : paramThrowable.toString());
    this.cause = paramThrowable;
  }


  protected Throwable(String paramString, Throwable paramThrowable, boolean paramBoolean1, boolean paramBoolean2)
  {
    if (paramBoolean2)
      fillInStackTrace();
    else {
      this.stackTrace = null;
    }
    this.detailMessage = paramString;
    this.cause = paramThrowable;
    if (!paramBoolean1)
      this.suppressedExceptions = null;
  }


  public String getMessage()
  {
    return this.detailMessage;
  }


  public String getLocalizedMessage()
  {
    return getMessage();
  }


  public synchronized Throwable getCause()
  {
    return this.cause == this ? null : this.cause;
  }


  public synchronized Throwable initCause(Throwable paramThrowable)
  {
    if (this.cause != this) {
      throw new IllegalStateException("Can't overwrite cause with " + Objects.toString(paramThrowable, "a null"), this);
    }
    if (paramThrowable == this)
      throw new IllegalArgumentException("Self-causation not permitted", this);
    this.cause = paramThrowable;
    return this;
  }


  public String toString()
  {
    String str1 = getClass().getName();
    String str2 = getLocalizedMessage();
    return str2 != null ? str1 + ": " + str2 : str1;
  }


  public void printStackTrace()
  {
    printStackTrace(System.err);
  }


  public void printStackTrace(PrintStream paramPrintStream)
  {
    printStackTrace(new WrappedPrintStream(paramPrintStream));
  }


  private void printStackTrace(PrintStreamOrWriter paramPrintStreamOrWriter)
  {
    Set localSet = Collections.newSetFromMap(new IdentityHashMap());


    localSet.add(this);


    synchronized (paramPrintStreamOrWriter.lock())
    {
      paramPrintStreamOrWriter.println(this);
      StackTraceElement[] arrayOfStackTraceElement = getOurStackTrace();
      Object localObject2;
      for (localObject2 : arrayOfStackTraceElement) {
        paramPrintStreamOrWriter.println("\tat " + localObject2);
      }


      for (localObject2 : getSuppressed()) {
        localObject2.printEnclosedStackTrace(paramPrintStreamOrWriter, arrayOfStackTraceElement, "Suppressed: ", "\t", localSet);
      }


      ??? = getCause();
      if (??? != null)
        ((Throwable)???).printEnclosedStackTrace(paramPrintStreamOrWriter, arrayOfStackTraceElement, "Caused by: ", "", localSet);
    }
  }


  private void printEnclosedStackTrace(PrintStreamOrWriter paramPrintStreamOrWriter, StackTraceElement[] paramArrayOfStackTraceElement, String paramString1, String paramString2, Set<Throwable> paramSet)
  {
    assert (Thread.holdsLock(paramPrintStreamOrWriter.lock()));
    if (paramSet.contains(this)) {
      paramPrintStreamOrWriter.println("\t[CIRCULAR REFERENCE:" + this + "]");
    } else {
      paramSet.add(this);


      StackTraceElement[] arrayOfStackTraceElement = getOurStackTrace();
      int i = arrayOfStackTraceElement.length - 1;
      int j = paramArrayOfStackTraceElement.length - 1;
      while ((i >= 0) && (j >= 0) && (arrayOfStackTraceElement[i].equals(paramArrayOfStackTraceElement[j]))) {
        i--; j--;
      }
      int k = arrayOfStackTraceElement.length - 1 - i;


      paramPrintStreamOrWriter.println(paramString2 + paramString1 + this);
      for (int m = 0; m <= i; m++)
        paramPrintStreamOrWriter.println(paramString2 + "\tat " + arrayOfStackTraceElement[m]);
      if (k != 0) {
        paramPrintStreamOrWriter.println(paramString2 + "\t... " + k + " more");
      }


      for (Object localObject2 : getSuppressed()) {
        localObject2.printEnclosedStackTrace(paramPrintStreamOrWriter, arrayOfStackTraceElement, "Suppressed: ", paramString2 + "\t", paramSet);
      }


      ??? = getCause();
      if (??? != null)
        ((Throwable)???).printEnclosedStackTrace(paramPrintStreamOrWriter, arrayOfStackTraceElement, "Caused by: ", paramString2, paramSet);
    }
  }


  public void printStackTrace(PrintWriter paramPrintWriter)
  {
    printStackTrace(new WrappedPrintWriter(paramPrintWriter));
  }


  public synchronized Throwable fillInStackTrace()
  {
    if ((this.stackTrace != null) || (this.backtrace != null))
    {
      fillInStackTrace(0);
      this.stackTrace = UNASSIGNED_STACK;
    }
    return this;
  }


  private native Throwable fillInStackTrace(int paramInt);


  public StackTraceElement[] getStackTrace()
  {
    return (StackTraceElement[])getOurStackTrace().clone();
  }


  private synchronized StackTraceElement[] getOurStackTrace()
  {
    if ((this.stackTrace == UNASSIGNED_STACK) || ((this.stackTrace == null) && (this.backtrace != null)))
    {
      int i = getStackTraceDepth();
      this.stackTrace = new StackTraceElement[i];
      for (int j = 0; j < i; j++)
        this.stackTrace[j] = getStackTraceElement(j);
    } else if (this.stackTrace == null) {
      return UNASSIGNED_STACK;
    }
    return this.stackTrace;
  }


  public void setStackTrace(StackTraceElement[] paramArrayOfStackTraceElement)
  {
    StackTraceElement[] arrayOfStackTraceElement = (StackTraceElement[])paramArrayOfStackTraceElement.clone();
    for (int i = 0; i < arrayOfStackTraceElement.length; i++) {
      if (arrayOfStackTraceElement[i] == null) {
        throw new NullPointerException("stackTrace[" + i + "]");
      }
    }
    synchronized (this) {
      if ((this.stackTrace == null) && (this.backtrace == null))
      {
        return;
      }this.stackTrace = arrayOfStackTraceElement;
    }
  }


  native int getStackTraceDepth();


  native StackTraceElement getStackTraceElement(int paramInt);


  private void readObject(ObjectInputStream paramObjectInputStream)
    throws IOException, ClassNotFoundException
  {
    paramObjectInputStream.defaultReadObject();
    Object localObject1;
    if (this.suppressedExceptions != null) {
      localObject1 = null;
      if (this.suppressedExceptions.isEmpty())
      {
        localObject1 = SUPPRESSED_SENTINEL;
      } else {
        localObject1 = new ArrayList(1);
        for (Throwable localThrowable : this.suppressedExceptions)
        {
          if (localThrowable == null)
            throw new NullPointerException("Cannot suppress a null exception.");
          if (localThrowable == this)
            throw new IllegalArgumentException("Self-suppression not permitted");
          ((List)localObject1).add(localThrowable);
        }
      }
      this.suppressedExceptions = ((List)localObject1);
    }


    if (this.stackTrace != null) {
      if (this.stackTrace.length == 0)
        this.stackTrace = ((StackTraceElement[])UNASSIGNED_STACK.clone());
      else if ((this.stackTrace.length == 1) && (SentinelHolder.STACK_TRACE_ELEMENT_SENTINEL.equals(this.stackTrace[0])))
      {
        this.stackTrace = null;
      }
      else for (Object localObject2 : this.stackTrace) {
          if (localObject2 == null) {
            throw new NullPointerException("null StackTraceElement in serial stream. ");
          }
        }




    }
    else
    {
      this.stackTrace = ((StackTraceElement[])UNASSIGNED_STACK.clone());
    }
  }


  private synchronized void writeObject(ObjectOutputStream paramObjectOutputStream)
    throws IOException
  {
    getOurStackTrace();


    StackTraceElement[] arrayOfStackTraceElement = this.stackTrace;
    try {
      if (this.stackTrace == null)
        this.stackTrace = SentinelHolder.STACK_TRACE_SENTINEL;
      paramObjectOutputStream.defaultWriteObject();
    } finally {
      this.stackTrace = arrayOfStackTraceElement;
    }
  }


  public final synchronized void addSuppressed(Throwable paramThrowable)
  {
    if (paramThrowable == this) {
      throw new IllegalArgumentException("Self-suppression not permitted", paramThrowable);
    }
    if (paramThrowable == null) {
      throw new NullPointerException("Cannot suppress a null exception.");
    }
    if (this.suppressedExceptions == null) {
      return;
    }
    if (this.suppressedExceptions == SUPPRESSED_SENTINEL) {
      this.suppressedExceptions = new ArrayList(1);
    }
    this.suppressedExceptions.add(paramThrowable);
  }


  public final synchronized Throwable[] getSuppressed()
  {
    if ((this.suppressedExceptions == SUPPRESSED_SENTINEL) || (this.suppressedExceptions == null))
    {
      return EMPTY_THROWABLE_ARRAY;
    }
    return (Throwable[])this.suppressedExceptions.toArray(EMPTY_THROWABLE_ARRAY);
  }


  static
  {
    UNASSIGNED_STACK = new StackTraceElement[0];


    SUPPRESSED_SENTINEL = Collections.unmodifiableList(new ArrayList(0));


    EMPTY_THROWABLE_ARRAY = new Throwable[0];
  }


  private static class WrappedPrintWriter extends Throwable.PrintStreamOrWriter
  {
    private final PrintWriter printWriter;


    WrappedPrintWriter(PrintWriter paramPrintWriter)
    {
      super();
      this.printWriter = paramPrintWriter;
    }


    Object lock()
    {
      return this.printWriter;
    }


    void println(Object paramObject)
    {
      this.printWriter.println(paramObject);
    }
  }


  private static class WrappedPrintStream extends Throwable.PrintStreamOrWriter
  {
    private final PrintStream printStream;


    WrappedPrintStream(PrintStream paramPrintStream)
    {
      super();
      this.printStream = paramPrintStream;
    }


    Object lock()
    {
      return this.printStream;
    }


    void println(Object paramObject)
    {
      this.printStream.println(paramObject);
    }
  }


  private static abstract class PrintStreamOrWriter
  {
    abstract Object lock();


    abstract void println(Object paramObject);
  }


  private static class SentinelHolder
  {
    public static final StackTraceElement STACK_TRACE_ELEMENT_SENTINEL = new StackTraceElement("", "", null, -2147483648);
    public static final StackTraceElement[] STACK_TRACE_SENTINEL = { STACK_TRACE_ELEMENT_SENTINEL };
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值