Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2017 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package me.j360.dubbo.modules.util.base; import com.google.common.base.Throwables; import me.j360.dubbo.modules.util.base.annotation.Nullable; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; /** * . * * 1. . * * 2. StackTrace???????StackTrace??StackTrace * * @author calvin */ public class ExceptionUtil { private static final StackTraceElement[] EMPTY_STACK_TRACE = new StackTraceElement[0]; /** * CheckedException?RuntimeException?, ????CheckExcetpion. * * CheckedExceptionUndeclaredThrowableExceptionRunTimeExceptionError??. * * from Commons Lange 3.5 ExceptionUtils. * * unchecked()?Sonar? * * ?: * * <pre> * try{ ... }catch(Exception e){ throw unchecked(t); } * * @see ExceptionUtils#wrapAndThrow(Throwable) */ public static RuntimeException unchecked(Throwable t) { if (t instanceof RuntimeException) { throw (RuntimeException) t; } if (t instanceof Error) { throw (Error) t; } throw new UncheckedException(t); } /** * ??cause. ??. * * FutureExecutionException ??InvocationTargetException ?Cause * * ?? unchecked() UncheckedException??. * * from Quasar and Tomcat's ExceptionUtils */ public static Throwable unwrap(Throwable t) { if (t instanceof java.util.concurrent.ExecutionException || t instanceof java.lang.reflect.InvocationTargetException || t instanceof UncheckedException) { return t.getCause(); } return t; } /** * ?uncheckedunwrap */ public static RuntimeException uncheckedAndWrap(Throwable t) { Throwable unwrapped = unwrap(t); if (unwrapped instanceof RuntimeException) { throw (RuntimeException) unwrapped; } if (unwrapped instanceof Error) { throw (Error) unwrapped; } throw new UncheckedException(unwrapped); } /** * StackTrace[]?String, Loggere.printStackTrace(). * * @see Throwables#getStackTraceAsString(Throwable) */ public static String stackTraceText(Throwable t) { return Throwables.getStackTraceAsString(t); } /** * ?Root Cause. * * Cause, * * @see Throwables#getRootCause(Throwable) */ public static Throwable getRootCause(Throwable t) { return Throwables.getRootCause(t); } /** * ??. */ @SuppressWarnings("unchecked") public static boolean isCausedBy(Throwable t, Class<? extends Exception>... causeExceptionClasses) { Throwable cause = t; while (cause != null) { for (Class<? extends Exception> causeClass : causeExceptionClasses) { if (causeClass.isInstance(cause)) { return true; } } cause = cause.getCause(); } return false; } /** * ??: ?. * * Throwable.toString()?? * * @see ExceptionUtils#getMessage(Throwable) */ public static String toStringWithShortName(@Nullable Throwable t) { return ExceptionUtils.getMessage(t); } /** * ??: ? <-- RootCause??: ? */ public static String toStringWithRootCause(@Nullable Throwable t) { if (t == null) { return StringUtils.EMPTY; } final String clsName = ClassUtils.getShortClassName(t, null); final String message = StringUtils.defaultString(t.getMessage()); Throwable cause = getRootCause(t); StringBuilder sb = new StringBuilder(128).append(clsName).append(": ").append(message); if (cause != t) { sb.append("; <---").append(toStringWithShortName(cause)); } return sb.toString(); } /////////// StackTrace //////// /** * from Netty, ??StackTrace. * * ??, ????StackTrace. ???. * * ?????. * * <pre> * private static RuntimeException TIMEOUT_EXCEPTION = ExceptionUtil.setStackTrace(new RuntimeException("Timeout"), * MyClass.class, "mymethod"); * * </pre> */ public static <T extends Throwable> T setStackTrace(T exception, Class<?> throwClass, String throwClazz) { exception.setStackTrace( new StackTraceElement[] { new StackTraceElement(throwClass.getName(), throwClazz, null, -1) }); return exception;// NOSONAR } /** * StackTrace. ?StackTrace?, ????. * * ?StackTrace???(logger)?Trace. * * Cause??, ??CauseStackTrace. */ public static <T extends Throwable> T clearStackTrace(T exception) { Throwable cause = exception; while (cause != null) { cause.setStackTrace(EMPTY_STACK_TRACE); cause = cause.getCause(); } return exception;// NOSONAR } /** * Message??, ?clone()???? */ public static class CloneableException extends Exception implements Cloneable { private static final long serialVersionUID = -6270471689928560417L; protected String message; public CloneableException() { super((Throwable) null); } public CloneableException(String message) { super((Throwable) null); this.message = message; } public CloneableException(String message, Throwable cause) { super(cause); this.message = message; } @Override public CloneableException clone() { try { return (CloneableException) super.clone(); } catch (CloneNotSupportedException e) {// NOSONAR return null; } } public CloneableException clone(String message) { CloneableException newException = this.clone(); newException.setMessage(message); return newException; } @Override public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public CloneableException setStackTrace(Class<?> throwClazz, String throwMethod) { ExceptionUtil.setStackTrace(this, throwClazz, throwMethod); return this; } } /** * ?fillInStackTrace()??StackTrace. * * Message????? */ public static class CloneableRuntimeException extends RuntimeException implements Cloneable { private static final long serialVersionUID = 3984796576627959400L; protected String message; public CloneableRuntimeException() { super((Throwable) null); } public CloneableRuntimeException(String message) { super((Throwable) null); this.message = message; } public CloneableRuntimeException(String message, Throwable cause) { super(cause); this.message = message; } @Override public CloneableRuntimeException clone() { try { return (CloneableRuntimeException) super.clone(); } catch (CloneNotSupportedException e) { // NOSONAR return null; } } public CloneableRuntimeException clone(String message) { CloneableRuntimeException newException = this.clone(); newException.setMessage(message); return newException; } @Override public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public CloneableRuntimeException setStackTrace(Class<?> throwClazz, String throwMethod) { ExceptionUtil.setStackTrace(this, throwClazz, throwMethod); return this; } } /** * CheckedExceptionwrapper * * @author calvin * */ public static class UncheckedException extends RuntimeException { private static final long serialVersionUID = 4140223302171577501L; public UncheckedException(Throwable cause) { super(cause); } @Override public String getMessage() { return super.getCause().getMessage(); } } }