exceptionutils.java

来自「hibernate-distribution-3.3.1.GA-dist.zip」· Java 代码 · 共 706 行 · 第 1/2 页

JAVA
706
字号
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors.  All third-party contributions are * distributed under license by Red Hat Middleware LLC. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA  02110-1301  USA * */package org.hibernate.exception;import org.hibernate.util.ArrayHelper;import java.io.PrintStream;import java.io.PrintWriter;import java.io.StringWriter;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.sql.SQLException;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.StringTokenizer;/** * <p>Provides utilities for manipulating and examining * <code>Throwable</code> objects.</p> * * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a> * @author Dmitri Plotnikov * @author Stephen Colebourne * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a> * @author Pete Gieser * @version $Id: ExceptionUtils.java 4782 2004-11-21 00:11:27Z pgmjsd $ * @since 1.0 */public final class ExceptionUtils {	private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );	/**	 * <p>Used when printing stack frames to denote the start of a	 * wrapped exception.</p>	 * <p/>	 * <p>Package private for accessibility by test suite.</p>	 */	static final String WRAPPED_MARKER = " [wrapped] ";	/**	 * <p>The names of methods commonly used to access a wrapped exception.</p>	 */	private static final String[] CAUSE_METHOD_NAMES = {		"getCause",		"getNextException",		"getTargetException",		"getException",		"getSourceException",		"getRootCause",		"getCausedByException",		"getNested"	};	/**	 * <p>The Method object for JDK1.4 getCause.</p>	 */	private static final Method THROWABLE_CAUSE_METHOD;	static {		Method getCauseMethod;		try {			getCauseMethod = Throwable.class.getMethod( "getCause", null );		}		catch ( Exception e ) {			getCauseMethod = null;		}		THROWABLE_CAUSE_METHOD = getCauseMethod;	}	private ExceptionUtils() {	}	//-----------------------------------------------------------------------	/**	 * <p>Adds to the list of method names used in the search for <code>Throwable</code>	 * objects.</p>	 *	 * @param methodName  the methodName to add to the list, <code>null</code>	 *  and empty strings are ignored	 * @since 2.0	 */	/*public static void addCauseMethodName(String methodName) {		if ( StringHelper.isNotEmpty(methodName) ) {			List list = new ArrayList( Arrays.asList(CAUSE_METHOD_NAMES );			list.add(methodName);			CAUSE_METHOD_NAMES = (String[]) list.toArray(new String[list.size()]);		}	}*/	/**	 * <p>Introspects the <code>Throwable</code> to obtain the cause.</p>	 * <p/>	 * <p>The method searches for methods with specific names that return a	 * <code>Throwable</code> object. This will pick up most wrapping exceptions,	 * including those from JDK 1.4, and	 * {@link org.apache.commons.lang.exception.NestableException NestableException}.	 * The method names can be added to using {@link #addCauseMethodName(String)}.</p>	 * <p/>	 * <p>The default list searched for are:</p>	 * <ul>	 * <li><code>getCause()</code></li>	 * <li><code>getNextException()</code></li>	 * <li><code>getTargetException()</code></li>	 * <li><code>getException()</code></li>	 * <li><code>getSourceException()</code></li>	 * <li><code>getRootCause()</code></li>	 * <li><code>getCausedByException()</code></li>	 * <li><code>getNested()</code></li>	 * </ul>	 * <p/>	 * <p>In the absence of any such method, the object is inspected for a	 * <code>detail</code> field assignable to a <code>Throwable</code>.</p>	 * <p/>	 * <p>If none of the above is found, returns <code>null</code>.</p>	 *	 * @param throwable the throwable to introspect for a cause, may be null	 * @return the cause of the <code>Throwable</code>,	 *         <code>null</code> if none found or null throwable input	 */	public static Throwable getCause(Throwable throwable) {		return getCause( throwable, CAUSE_METHOD_NAMES );	}	/**	 * <p>Introspects the <code>Throwable</code> to obtain the cause.</p>	 * <p/>	 * <ol>	 * <li>Try known exception types.</li>	 * <li>Try the supplied array of method names.</li>	 * <li>Try the field 'detail'.</li>	 * </ol>	 * <p/>	 * <p>A <code>null</code> set of method names means use the default set.	 * A <code>null</code> in the set of method names will be ignored.</p>	 *	 * @param throwable   the throwable to introspect for a cause, may be null	 * @param methodNames the method names, null treated as default set	 * @return the cause of the <code>Throwable</code>,	 *         <code>null</code> if none found or null throwable input	 */	public static Throwable getCause(Throwable throwable, String[] methodNames) {		if ( throwable == null ) {			return null;		}		Throwable cause = getCauseUsingWellKnownTypes( throwable );		if ( cause == null ) {			if ( methodNames == null ) {				methodNames = CAUSE_METHOD_NAMES;			}			for ( int i = 0; i < methodNames.length; i++ ) {				String methodName = methodNames[i];				if ( methodName != null ) {					cause = getCauseUsingMethodName( throwable, methodName );					if ( cause != null ) {						break;					}				}			}			if ( cause == null ) {				cause = getCauseUsingFieldName( throwable, "detail" );			}		}		return cause;	}	/**	 * <p>Introspects the <code>Throwable</code> to obtain the root cause.</p>	 * <p/>	 * <p>This method walks through the exception chain to the last element,	 * "root" of the tree, using {@link #getCause(Throwable)}, and	 * returns that exception.</p>	 *	 * @param throwable the throwable to get the root cause for, may be null	 * @return the root cause of the <code>Throwable</code>,	 *         <code>null</code> if none found or null throwable input	 */	public static Throwable getRootCause(Throwable throwable) {		Throwable cause = getCause( throwable );		if ( cause != null ) {			throwable = cause;			while ( ( throwable = getCause( throwable ) ) != null ) {				cause = throwable;			}		}		return cause;	}	/**	 * <p>Finds a <code>Throwable</code> for known types.</p>	 * <p/>	 * <p>Uses <code>instanceof</code> checks to examine the exception,	 * looking for well known types which could contain chained or	 * wrapped exceptions.</p>	 *	 * @param throwable the exception to examine	 * @return the wrapped exception, or <code>null</code> if not found	 */	private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) {		if ( throwable instanceof Nestable ) {			return ( ( Nestable ) throwable ).getCause();		}		else if ( throwable instanceof SQLException ) {			return ( ( SQLException ) throwable ).getNextException();		}		else if ( throwable instanceof InvocationTargetException ) {			return ( ( InvocationTargetException ) throwable ).getTargetException();		}		else {			return null;		}	}	/**	 * <p>Finds a <code>Throwable</code> by method name.</p>	 *	 * @param throwable  the exception to examine	 * @param methodName the name of the method to find and invoke	 * @return the wrapped exception, or <code>null</code> if not found	 */	private static Throwable getCauseUsingMethodName(Throwable throwable, String methodName) {		Method method = null;		try {			method = throwable.getClass().getMethod( methodName, null );		}		catch ( NoSuchMethodException ignored ) {		}		catch ( SecurityException ignored ) {		}		if ( method != null && Throwable.class.isAssignableFrom( method.getReturnType() ) ) {			try {				return ( Throwable ) method.invoke( throwable, ArrayHelper.EMPTY_OBJECT_ARRAY );			}			catch ( IllegalAccessException ignored ) {			}			catch ( IllegalArgumentException ignored ) {			}			catch ( InvocationTargetException ignored ) {			}		}		return null;	}	/**	 * <p>Finds a <code>Throwable</code> by field name.</p>	 *	 * @param throwable the exception to examine	 * @param fieldName the name of the attribute to examine	 * @return the wrapped exception, or <code>null</code> if not found	 */	private static Throwable getCauseUsingFieldName(Throwable throwable, String fieldName) {		Field field = null;		try {			field = throwable.getClass().getField( fieldName );		}		catch ( NoSuchFieldException ignored ) {		}		catch ( SecurityException ignored ) {		}		if ( field != null && Throwable.class.isAssignableFrom( field.getType() ) ) {			try {				return ( Throwable ) field.get( throwable );			}			catch ( IllegalAccessException ignored ) {			}			catch ( IllegalArgumentException ignored ) {			}		}		return null;	}	//-----------------------------------------------------------------------	/**	 * <p>Checks if the Throwable class has a <code>getCause</code> method.</p>	 * <p/>	 * <p>This is true for JDK 1.4 and above.</p>	 *	 * @return true if Throwable is nestable	 * @since 2.0	 */	public static boolean isThrowableNested() {		return ( THROWABLE_CAUSE_METHOD != null );	}	/**	 * <p>Checks whether this <code>Throwable</code> class can store a cause.</p>	 * <p/>	 * <p>This method does <b>not</b> check whether it actually does store a cause.<p>	 *	 * @param throwable the <code>Throwable</code> to examine, may be null	 * @return boolean <code>true</code> if nested otherwise <code>false</code>	 * @since 2.0	 */	public static boolean isNestedThrowable(Throwable throwable) {		if ( throwable == null ) {			return false;		}		if ( throwable instanceof Nestable ) {			return true;		}		else if ( throwable instanceof SQLException ) {			return true;		}		else if ( throwable instanceof InvocationTargetException ) {			return true;		}		else if ( isThrowableNested() ) {			return true;		}		Class cls = throwable.getClass();		for ( int i = 0, isize = CAUSE_METHOD_NAMES.length; i < isize; i++ ) {			try {				Method method = cls.getMethod( CAUSE_METHOD_NAMES[i], null );				if ( method != null && Throwable.class.isAssignableFrom( method.getReturnType() ) ) {					return true;				}			}			catch ( NoSuchMethodException ignored ) {			}			catch ( SecurityException ignored ) {			}		}		try {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?