Example usage for org.springframework.util ReflectionUtils declaresException

List of usage examples for org.springframework.util ReflectionUtils declaresException

Introduction

In this page you can find the example usage for org.springframework.util ReflectionUtils declaresException.

Prototype

public static boolean declaresException(Method method, Class<?> exceptionType) 

Source Link

Document

Determine whether the given method explicitly declares the given exception or one of its superclasses, which means that an exception of that type can be propagated as-is within a reflective invocation.

Usage

From source file:org.springframework.jmx.access.MBeanClientInterceptor.java

/**
 * Route the invocation to the configured managed resource. Correctly routes JavaBean property
 * access to {@code MBeanServerConnection.get/setAttribute} and method invocation to
 * {@code MBeanServerConnection.invoke}.
 * @param invocation the {@code MethodInvocation} to re-route
 * @return the value returned as a result of the re-routed invocation
 * @throws Throwable an invocation error propagated to the user
 *//* w w w .j a va2 s  .c o m*/
@Nullable
protected Object doInvoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    try {
        Object result;
        if (this.invocationHandler != null) {
            result = this.invocationHandler.invoke(invocation.getThis(), method, invocation.getArguments());
        } else {
            PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
            if (pd != null) {
                result = invokeAttribute(pd, invocation);
            } else {
                result = invokeOperation(method, invocation.getArguments());
            }
        }
        return convertResultValueIfNecessary(result, new MethodParameter(method, -1));
    } catch (MBeanException ex) {
        throw ex.getTargetException();
    } catch (RuntimeMBeanException ex) {
        throw ex.getTargetException();
    } catch (RuntimeErrorException ex) {
        throw ex.getTargetError();
    } catch (RuntimeOperationsException ex) {
        // This one is only thrown by the JMX 1.2 RI, not by the JDK 1.5 JMX code.
        RuntimeException rex = ex.getTargetException();
        if (rex instanceof RuntimeMBeanException) {
            throw ((RuntimeMBeanException) rex).getTargetException();
        } else if (rex instanceof RuntimeErrorException) {
            throw ((RuntimeErrorException) rex).getTargetError();
        } else {
            throw rex;
        }
    } catch (OperationsException ex) {
        if (ReflectionUtils.declaresException(method, ex.getClass())) {
            throw ex;
        } else {
            throw new InvalidInvocationException(ex.getMessage());
        }
    } catch (JMException ex) {
        if (ReflectionUtils.declaresException(method, ex.getClass())) {
            throw ex;
        } else {
            throw new InvocationFailureException("JMX access failed", ex);
        }
    } catch (IOException ex) {
        if (ReflectionUtils.declaresException(method, ex.getClass())) {
            throw ex;
        } else {
            throw new MBeanConnectFailureException("I/O failure during JMX access", ex);
        }
    }
}

From source file:org.springframework.remoting.rmi.RmiClientInterceptorUtils.java

/**
 * Wrap the given arbitrary exception that happened during remote access
 * in either a RemoteException or a Spring RemoteAccessException (if the
 * method signature does not support RemoteException).
 * <p>Only call this for remote access exceptions, not for exceptions
 * thrown by the target service itself!/*from  w w w.  jav a2 s  .c  o  m*/
 * @param method the invoked method
 * @param ex the exception that happened, to be used as cause for the
 * RemoteAccessException or RemoteException
 * @param message the message for the RemoteAccessException respectively
 * RemoteException
 * @return the exception to be thrown to the caller
 */
public static Exception convertRmiAccessException(Method method, Throwable ex, String message) {
    if (logger.isDebugEnabled()) {
        logger.debug(message, ex);
    }
    if (ReflectionUtils.declaresException(method, RemoteException.class)) {
        return new RemoteException(message, ex);
    } else {
        return new RemoteAccessException(message, ex);
    }
}

From source file:org.springframework.remoting.rmi.RmiClientInterceptorUtils.java

/**
 * Convert the given RemoteException that happened during remote access
 * to Spring's RemoteAccessException if the method signature does not
 * support RemoteException. Else, return the original RemoteException.
 * @param method the invoked method/*from w  ww .j av a  2 s  .co  m*/
 * @param ex the RemoteException that happened
 * @param isConnectFailure whether the given exception should be considered
 * a connect failure
 * @param serviceName the name of the service (for debugging purposes)
 * @return the exception to be thrown to the caller
 */
public static Exception convertRmiAccessException(Method method, RemoteException ex, boolean isConnectFailure,
        String serviceName) {

    if (logger.isDebugEnabled()) {
        logger.debug("Remote service [" + serviceName + "] threw exception", ex);
    }
    if (ReflectionUtils.declaresException(method, ex.getClass())) {
        return ex;
    } else {
        if (isConnectFailure) {
            return new RemoteConnectFailureException(
                    "Could not connect to remote service [" + serviceName + "]", ex);
        } else {
            return new RemoteAccessException("Could not access remote service [" + serviceName + "]", ex);
        }
    }
}