cc.sion.core.utils.Exceptions.java Source code

Java tutorial

Introduction

Here is the source code for cc.sion.core.utils.Exceptions.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cc.sion.core.utils;

import cc.sion.core.utils.annon.Nullable;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * .
 *
 * ?guavaThrowables
 *
 * @author calvin
 */
public class Exceptions {

    /**
     * ErrorStackString.
     */
    public static String getStackTraceAsString(Throwable ex) {
        StringWriter stringWriter = new StringWriter();
        ex.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }

    /**
     * ??????, ?
     */
    public static String getErrorMessageWithNestedException(Throwable ex) {
        Throwable nestedException = ex.getCause();
        return new StringBuilder().append(ex.getMessage()).append(" nested exception is ")
                .append(nestedException.getClass().getName()).append(":").append(nestedException.getMessage())
                .toString();
    }

    /**
     * ??.
     */
    public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses) {
        Throwable cause = ex;
        while (cause != null) {
            for (Class<? extends Exception> causeClass : causeExceptionClasses) {
                if (causeClass.isInstance(cause)) {
                    return true;
                }
            }
            cause = cause.getCause();
        }
        return false;
    }

    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); }
     */
    public static RuntimeException unchecked(Throwable t) {

        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        }
        if (t instanceof Error) {
            throw (Error) t;
        }
        throw new UncheckedException(t);
    }

    /**
     * ?Root Cause.
     *
     * Cause, 
     *
     */
    public static Throwable getRootCause(Throwable ex) {
        return ExceptionUtils.getRootCause(ex);
    }

    /**
     * ??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);
    }

    /**
     * ??.
     */
    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) {
            Exceptions.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) {
            Exceptions.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();
        }
    }

}