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

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

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

Throwable.getStackTrace介绍

[英]Returns a clone of the array of stack trace elements of this Throwable. Each StackTraceElement represents an entry in the call stack. The element at position 0 is the top of the stack, that is, the stack frame where this Throwable is thrown.
[中]返回此可丢弃文件的堆栈跟踪元素数组的克隆。每个StackTraceeElement表示调用堆栈中的一个条目。位置0处的元素是堆栈的顶部,也就是抛出此可丢弃项的堆栈帧。

代码示例

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

private LocationImpl(StackTraceFilter stackTraceFilter, Throwable stackTraceHolder) {
  this.stackTraceFilter = stackTraceFilter;
  this.stackTraceHolder = stackTraceHolder;
  if (stackTraceHolder.getStackTrace() == null || stackTraceHolder.getStackTrace().length == 0) {
    //there are corner cases where exception can have a null or empty stack trace
    //for example, a custom exception can override getStackTrace() method
    this.sourceFile = "<unknown source file>";
  } else {
    this.sourceFile = stackTraceFilter.findSourceFile(stackTraceHolder.getStackTrace(), "<unknown source file>");
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean matches(Method method, Class<?> targetClass, Object... args) {
  this.evaluations++;
  for (StackTraceElement element : new Throwable().getStackTrace()) {
    if (element.getClassName().equals(this.clazz.getName()) &&
        (this.methodName == null || element.getMethodName().equals(this.methodName))) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: ReactiveX/RxJava

private void appendStackTrace(StringBuilder b, Throwable ex, String prefix) {
  b.append(prefix).append(ex).append('\n');
  for (StackTraceElement stackElement : ex.getStackTrace()) {
    b.append("\t\tat ").append(stackElement).append('\n');
  }
  if (ex.getCause() != null) {
    b.append("\tCaused by: ");
    appendStackTrace(b, ex.getCause(), "");
  }
}

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

private String getStackTrace (Throwable e) {
  StringBuilder sb = new StringBuilder();
  for (StackTraceElement trace : e.getStackTrace()) {
    sb.append(trace.toString() + "\n");
  }
  return sb.toString();
}

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

private String getStackTrace (Throwable e) {
  StringBuilder sb = new StringBuilder();
  for (StackTraceElement trace : e.getStackTrace()) {
    sb.append(trace.toString() + "\n");
  }
  return sb.toString();
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  public void writeObject(Object obj, AbstractHessianOutput out)
      throws IOException {
    Throwable e = (Throwable) obj;

    e.getStackTrace();

    super.writeObject(obj, out);
  }
}

代码示例来源:origin: skylot/jadx

private static void filter(Throwable th) {
  StackTraceElement[] stackTrace = th.getStackTrace();
  int cutIndex = -1;
  int length = stackTrace.length;
  for (int i = 0; i < length; i++) {
    StackTraceElement stackTraceElement = stackTrace[i];
    if (stackTraceElement.getClassName().startsWith(JADX_API_PACKAGE)) {
      cutIndex = i;
    } else if (cutIndex > 0) {
      cutIndex = i;
      break;
    }
  }
  if (cutIndex > 0 && cutIndex < length) {
    th.setStackTrace(Arrays.copyOfRange(stackTrace, 0, cutIndex));
  }
}

代码示例来源:origin: alibaba/druid

public void setExecuteErrorLast(Throwable executeErrorLast) {
  this.executeErrorLast = executeErrorLast;
  if (executeErrorLast != null) {
    this.executeErrorLastMessage = executeErrorLast.getMessage();
    this.executeErrorLastClass = executeErrorLast.getClass().getName();
    this.executeErrorLastStackTrace = Utils.toString(executeErrorLast.getStackTrace());
  }
}

代码示例来源:origin: alibaba/druid

public void setLastError(Throwable lastError) {
  if (lastError != null) {
    lastErrorClass = lastError.getClass().getName();
    lastErrorMessage = lastError.getMessage();
    lastErrorStackTrace = Utils.toString(lastError.getStackTrace());
  }
}

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

@Override
public String toString() {
  //TODO SF perhaps store the results after invocation?
  StackTraceElement[] filtered = stackTraceFilter.filter(stackTraceHolder.getStackTrace(), false);
  if (filtered.length == 0) {
    return "-> at <<unknown line>>";
  }
  return "-> at " + filtered[0].toString();
}

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

private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception {
 Throwable cause = e.getCause();
 if (cause == null) {
  throw e;
 }
 if (combineStackTraces) {
  StackTraceElement[] combined =
    ObjectArrays.concat(cause.getStackTrace(), e.getStackTrace(), StackTraceElement.class);
  cause.setStackTrace(combined);
 }
 if (cause instanceof Exception) {
  throw (Exception) cause;
 }
 if (cause instanceof Error) {
  throw (Error) cause;
 }
 // The cause is a weird kind of Throwable, so throw the outer exception.
 throw e;
}

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

public RedissonKryoCodecException(Throwable cause) {
    super(cause.getMessage(), cause);
    setStackTrace(cause.getStackTrace());
  }
}

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

private void appendStackTrace(StringBuilder b, Throwable ex, String prefix) {
  b.append(prefix).append(ex).append('\n');
  for (StackTraceElement stackElement : ex.getStackTrace()) {
    b.append("\t\tat ").append(stackElement).append('\n');
  }
  if (ex.getCause() != null) {
    b.append("\tCaused by: ");
    appendStackTrace(b, ex.getCause(), "");
  }
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * @deprecated as of 1.305 use {@link #getEnvironment(TaskListener)}
 */
@Deprecated
public EnvVars getEnvironment() throws IOException, InterruptedException {
  LOGGER.log(WARNING, "deprecated call to Run.getEnvironment\n\tat {0}", new Throwable().getStackTrace()[1]);
  return getEnvironment(new LogTaskListener(LOGGER, Level.INFO));
}

代码示例来源:origin: spring-projects/spring-framework

private void resolve() {
  StackTraceElement[] stack = new Throwable().getStackTrace();
  String sourceClassName = null;
  String sourceMethodName = null;
  boolean found = false;
  for (StackTraceElement element : stack) {
    String className = element.getClassName();
    if (FQCN.equals(className)) {
      found = true;
    }
    else if (found) {
      sourceClassName = className;
      sourceMethodName = element.getMethodName();
      break;
    }
  }
  setSourceClassName(sourceClassName);
  setSourceMethodName(sourceMethodName);
}

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

public void filter(Throwable throwable) {
    if (!config.cleansStackTrace()) {
      return;
    }
    StackTraceElement[] filtered = filter.filter(throwable.getStackTrace(), true);
    throwable.setStackTrace(filtered);
  }
}

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

private static Object tryInvoke(Method origin, Object instance, Object[] arguments) throws Throwable {
  try {
    return origin.invoke(instance, arguments);
  } catch (InvocationTargetException exception) {
    Throwable cause = exception.getCause();
    new ConditionalStackTraceFilter().filter(hideRecursiveCall(cause, new Throwable().getStackTrace().length, origin.getDeclaringClass()));
    throw cause;
  }
}

代码示例来源:origin: spring-projects/spring-framework

private boolean contain(Throwable t, String className, String methodName) {
  for (StackTraceElement element : t.getStackTrace()) {
    if (className.equals(element.getClassName()) && methodName.equals(element.getMethodName())) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: ch.qos.logback/logback-classic

public static void build(ThrowableProxy nestedTP, Throwable nestedThrowable, ThrowableProxy parentTP) {
  StackTraceElement[] nestedSTE = nestedThrowable.getStackTrace();
  int commonFramesCount = -1;
  if (parentTP != null) {
    commonFramesCount = findNumberOfCommonFrames(nestedSTE, parentTP.getStackTraceElementProxyArray());
  }
  nestedTP.commonFrames = commonFramesCount;
  nestedTP.stackTraceElementProxyArray = steArrayToStepArray(nestedSTE);
}

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

public void appendWarnings(Failure failure, String warnings) {
  if (isEmpty(warnings)) {
    return;
  }
  //TODO: this has to protect the use in case jUnit changes and this internal state logic fails
  Throwable throwable = (Throwable) getInternalState(failure, "fThrownException");
  String newMessage = "contains both: actual test failure *and* Mockito warnings.\n" +
      warnings + "\n *** The actual failure is because of: ***\n";
  ExceptionIncludingMockitoWarnings e = new ExceptionIncludingMockitoWarnings(newMessage, throwable);
  e.setStackTrace(throwable.getStackTrace());
  setInternalState(failure, "fThrownException", e);
}

相关文章