mybatis3异常工具分享【ExceptionUtil】

/*
 *    Copyright 2009-2012 The MyBatis Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

public class ExceptionUtil {

  public static Throwable unwrapThrowable(Throwable wrapped) {
    Throwable unwrapped = wrapped;
    while (true) {
      if (unwrapped instanceof InvocationTargetException) {
        unwrapped = ((InvocationTargetException) unwrapped).getTargetException();
      } else if (unwrapped instanceof UndeclaredThrowableException) {
        unwrapped = ((UndeclaredThrowableException) unwrapped).getUndeclaredThrowable();
      } else {
        return unwrapped;
      }
    }
  }

}




在反射时可以说要经常用到


优秀到卓越
分享到:

再看ngrinder的exceptionUtils

package org.ngrinder.common.util;

import org.ngrinder.common.exception.NGrinderRuntimeException;

import java.util.List;

/**
 * Exception processing utility.
 * 
 * @author junoyoon
 * @since 3.2.3
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public abstract class ExceptionUtils {
	/**
	 * Check if the exception is {@link NGrinderRuntimeException}. If so, throw it. If
	 * not, wrap the given exception and throw it.
	 * 
	 * @param t 	Throwable
	 * @return exception
	 */
	public static NGrinderRuntimeException processException(Throwable t) {
		if (t instanceof NGrinderRuntimeException) {
			throw (NGrinderRuntimeException) sanitize(t);
		} else {
			throw new NGrinderRuntimeException(sanitize(t), true);
		}
	}

	/**
	 * Check if the exception {@link NGrinderRuntimeException}. If so, throw. If
	 * not, wrap the given exception.
	 * 
	 * @param message	message
	 * @return exception
	 */
	public static NGrinderRuntimeException processException(String message) {
		throw processException(new NGrinderRuntimeException(message));
	}

	/**
	 * Check if the exception is {@link NGrinderRuntimeException}. If so, throw.
	 * If not, wrap the given exception.
	 * 
	 * @param message	message
	 * @param t 		Throwable
	 * @return exception
	 */
	public static NGrinderRuntimeException processException(String message, Throwable t) {
		if (t instanceof NGrinderRuntimeException) {
			throw (NGrinderRuntimeException) sanitize(t);
		} else {
			throw new NGrinderRuntimeException(message, sanitize(t), true);
		}
	}

	/**
	 * Filter the stacktrace elements with only interesting one.
	 * 
	 * @param throwable	throwable
	 * @return {@link Throwable} instance with interested stacktrace elements.
	 */
	public static Throwable sanitize(Throwable throwable) {
		if (throwable instanceof NGrinderRuntimeException) {
			if (((NGrinderRuntimeException) throwable).isSanitized()) {
				return throwable;
			}
		}
		Throwable t = throwable;
		while (t != null) {
			// Note that this getBoolean access may well be synced...
			StackTraceElement[] trace = t.getStackTrace();
			List<StackTraceElement> newTrace = CollectionUtils.newArrayList();
			for (StackTraceElement stackTraceElement : trace) {
				if (isApplicationClass(stackTraceElement.getClassName())) {
					newTrace.add(stackTraceElement);
				}
			}
			StackTraceElement[] clean = new StackTraceElement[newTrace.size()];
			newTrace.toArray(clean);
			t.setStackTrace(clean);
			t = t.getCause();
		}
		if (throwable instanceof NGrinderRuntimeException) {
			((NGrinderRuntimeException) throwable).setSanitized(true);
		}
		return throwable;
	}

	/**
	 * Check if the given class name is the application class or not.
	 * 
	 * @param className	class name including package name
	 * @return true if application class
	 */
	private static boolean isApplicationClass(String className) {
		for (String each : getUninterestingPackages()) {
			if (className.startsWith(each)) {
				return false;
			}
		}
		return true;
	}

	private static final String[] NON_NGRINDER_PACKAGE = ("org.springframework.," + "javax.," + "org.apache.catalina.,"
			+ "sun.," + "net.sf.," + "java.," + "org.ngrinder.common.exception.NGrinderRuntimeException,"
			+ "com.springsource.," + "org.apache.coyote.," + "org.apache.tomact.,"
			+ "org.ngrinder.common.util.ExceptionUtils.").split("(\\s|,)+");

	/**
	 * Get interesting packages.
	 * 
	 * @return interesting packages
	 */
	protected static String[] getUninterestingPackages() {
		return NON_NGRINDER_PACKAGE;
	}

}



0
共 1 条   当前1/1页

© 2014 究问社区 copyRight 豫ICP备13003319号-1