java.lang.Throwable.fillInStackTrace()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(143)

本文整理了Java中java.lang.Throwable.fillInStackTrace()方法的一些代码示例,展示了Throwable.fillInStackTrace()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Throwable.fillInStackTrace()方法的具体详情如下:
包路径:java.lang.Throwable
类名称:Throwable
方法名:fillInStackTrace

Throwable.fillInStackTrace介绍

[英]Records the stack trace from the point where this method has been called to this Throwable. This method is invoked by the Throwable constructors.

This method is public so that code (such as an RPC system) which catches a Throwable and then re-throws it can replace the construction-time stack trace with a stack trace from the location where the exception was re-thrown, by calling fillInStackTrace.

This method is non-final so that non-Java language implementations can disable VM stack traces for their language. Filling in the stack trace is relatively expensive. Overriding this method in the root of a language's exception hierarchy allows the language to avoid paying for something it doesn't need.
[中]记录从调用此方法的点到此Throwable的堆栈跟踪。此方法由Throwable构造函数调用。
此方法是公共的,因此捕获可丢弃并重新抛出的代码(如RPC系统)可以通过调用fillInStackTrace,用重新抛出异常的位置的堆栈跟踪替换构造时堆栈跟踪。
此方法是非最终的,因此非Java语言实现可以禁用其语言的VM堆栈跟踪。填充堆栈跟踪相对比较昂贵。在一种语言的异常层次结构的根中重写这个方法可以使该语言避免为它不需要的东西付费。

代码示例

代码示例来源:origin: facebook/litho

/**
 * Reset the stacktrace of this Runnable to this point. To be called right before the runnable is
 * scheduled to another thread, in case it was instantiated ahead of time with a different code
 * flow.
 */
public void resetTrace() {
 mTracingThrowable.fillInStackTrace();
}

代码示例来源:origin: robovm/robovm

/**
 * Constructs a new {@code Throwable} with the current stack trace and the
 * given detail message.
 */
public Throwable(String detailMessage) {
  this.detailMessage = detailMessage;
  this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
  fillInStackTrace();
}

代码示例来源:origin: robovm/robovm

/**
 * Constructs a new {@code Throwable} with the current stack trace, the
 * given detail message and cause.
 */
public Throwable(String detailMessage, Throwable cause) {
  this.detailMessage = detailMessage;
  this.cause = cause;
  this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
  fillInStackTrace();
}

代码示例来源:origin: robovm/robovm

/**
 * Constructs a new {@code Throwable} that includes the current stack trace.
 */
public Throwable() {
  this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
  fillInStackTrace();
}

代码示例来源:origin: plantuml/plantuml

private CString(List<Character> data2, int currentStart) {
  this.data2 = data2;
  this.currentStart = currentStart;
  this.uid = UID;
  UID += 2;
  creation.fillInStackTrace();
}

代码示例来源:origin: robovm/robovm

/**
 * Constructs a new {@code Throwable} with the current stack trace and the
 * given cause.
 */
public Throwable(Throwable cause) {
  this.detailMessage = cause == null ? null : cause.toString();
  this.cause = cause;
  this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
  fillInStackTrace();
}

代码示例来源:origin: plantuml/plantuml

RealMoveable(RealLine line, String name) {
  super(line);
  this.name = name;
  this.creationPoint = new Throwable();
  this.creationPoint.fillInStackTrace();
}

代码示例来源:origin: plantuml/plantuml

public PositiveForce(Real fixedPoint, RealMoveable movingPoint, double minimunDistance) {
  if (fixedPoint == movingPoint) {
    throw new IllegalArgumentException();
  }
  this.fixedPoint = fixedPoint;
  this.movingPoint = movingPoint;
  this.minimunDistance = minimunDistance;
  this.creationPoint = new Throwable();
  this.creationPoint.fillInStackTrace();
}

代码示例来源:origin: plantuml/plantuml

RealMax(Collection<Real> reals) {
  super(line(reals));
  this.all.addAll(reals);
  this.creationPoint = new Throwable();
  this.creationPoint.fillInStackTrace();
}

代码示例来源:origin: plantuml/plantuml

public static PSystemVersion createDumpStackTrace() throws IOException {
  final List<String> strings = new ArrayList<String>();
  final Throwable creationPoint = new Throwable();
  creationPoint.fillInStackTrace();
  for (StackTraceElement ste : creationPoint.getStackTrace()) {
    strings.add(ste.toString());
  }
  return new PSystemVersion(false, strings);
}

代码示例来源:origin: org.mockito/mockito-core

public Object answer(InvocationOnMock invocation) throws Throwable {
  if (throwable == null) {
    throw new IllegalStateException("throwable is null: " +
      "you shall not call #answer if #validateFor fails!");
  }
  if (MockUtil.isMock(throwable)) {
    throw throwable;
  }
  Throwable t = throwable.fillInStackTrace();
  if (t == null) {
    //Custom exceptions sometimes return null, see #866
    throw throwable;
  }
  filter.filter(t);
  throw t;
}

代码示例来源:origin: commons-logging/commons-logging

try {
  Throwable throwable = new Throwable();
  throwable.fillInStackTrace();
  StringWriter stringWriter = new StringWriter();
  PrintWriter printWriter = new PrintWriter( stringWriter );

代码示例来源:origin: chentao0707/SimplifyReader

/**
 * Formats the caller's provided message and prepends useful info like
 * calling thread ID and method name.
 */
private static String buildMessage(String format, Object... args) {
  String msg = (args == null) ? format : String.format(Locale.US, format, args);
  StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
  String caller = "<unknown>";
  // Walk up the stack looking for the first caller outside of VolleyLog.
  // It will be at least two frames up, so start there.
  for (int i = 2; i < trace.length; i++) {
    Class<?> clazz = trace[i].getClass();
    if (!clazz.equals(VolleyLog.class)) {
      String callingClass = trace[i].getClassName();
      callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
      callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);
      caller = callingClass + "." + trace[i].getMethodName();
      break;
    }
  }
  return String.format(Locale.US, "[%d] %s: %s",
      Thread.currentThread().getId(), caller, msg);
}

代码示例来源:origin: mcxiaoke/android-volley

/**
 * Formats the caller's provided message and prepends useful info like
 * calling thread ID and method name.
 */
private static String buildMessage(String format, Object... args) {
  String msg = (args == null) ? format : String.format(Locale.US, format, args);
  StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
  String caller = "<unknown>";
  // Walk up the stack looking for the first caller outside of VolleyLog.
  // It will be at least two frames up, so start there.
  for (int i = 2; i < trace.length; i++) {
    Class<?> clazz = trace[i].getClass();
    if (!clazz.equals(VolleyLog.class)) {
      String callingClass = trace[i].getClassName();
      callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
      callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);
      caller = callingClass + "." + trace[i].getMethodName();
      break;
    }
  }
  return String.format(Locale.US, "[%d] %s: %s",
      Thread.currentThread().getId(), caller, msg);
}

代码示例来源:origin: killbill/killbill

private static String getDebugInfo() {
    final Throwable t = new Throwable();
    t.fillInStackTrace();

    final StackTraceElement[] stackTrace = t.getStackTrace();
    if (stackTrace == null) {
      return null;
    }

    final StringBuilder dump = new StringBuilder();
    int firstEntitySqlDaoCall = 0;

    String className;
    for (int i = 0; i < stackTrace.length; i++) {
      className = stackTrace[i].getClassName();
      if (className.startsWith("org.killbill.billing.util.entity.dao.EntitySqlDaoTransactionalJdbiWrapper")) {
        firstEntitySqlDaoCall = i;
      }
    }
    final int j = 1 + firstEntitySqlDaoCall;

    dump.append(stackTrace[j].getClassName()).append(".").append(stackTrace[j].getMethodName()).append("(").
        append(stackTrace[j].getFileName()).append(":").append(stackTrace[j].getLineNumber()).append(")");

    return dump.toString();
  }
}

代码示例来源:origin: google/j2objc

public Object answer(InvocationOnMock invocation) throws Throwable {
  Throwable throwable = (Throwable) ObjenesisHelper.newInstance(throwableClass);
  throwable.fillInStackTrace();
  filter.filter(throwable);
  throw throwable;
}

代码示例来源:origin: plantuml/plantuml

@Override
double getCurrentValueInternal() {
  double result = all.get(0).getCurrentValue();
  for (int i = 1; i < all.size(); i++) {
    Throwable t = new Throwable();
    t.fillInStackTrace();
    final int stackLength = t.getStackTrace().length;
    if (stackLength > 1000) {
      System.err.println("The faulty RealMax " + getName());
      System.err.println("has been created here:");
      printCreationStackTrace();
      throw new IllegalStateException("Infinite recursion?");
    }
    final double v = all.get(i).getCurrentValue();
    if (v > result) {
      result = v;
    }
  }
  return result;
}

代码示例来源:origin: google/j2objc

public Object answer(InvocationOnMock invocation) throws Throwable {
  if (new MockUtil().isMock(throwable)) {
    throw throwable;
  }
  Throwable t = throwable.fillInStackTrace();
  filter.filter(t);
  throw t;
}

代码示例来源:origin: reactor/reactor-core

@Override
public synchronized Throwable fillInStackTrace() {
  return getCause() != null ? getCause().fillInStackTrace() :
      super.fillInStackTrace();
}

代码示例来源:origin: org.easymock/easymock

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  try {
    if (control.getState() instanceof RecordState) {
      LastControl.reportLastControl(control);
    }
    return control.getState().invoke(new Invocation(proxy, method, args));
  } catch (RuntimeExceptionWrapper e) {
    throw e.getRuntimeException().fillInStackTrace();
  } catch (AssertionErrorWrapper e) {
    throw e.getAssertionError().fillInStackTrace();
  } catch (ThrowableWrapper t) {
    throw t.getThrowable().fillInStackTrace();
  }
  // then let all unwrapped exceptions pass unmodified
}

相关文章