Example usage for java.lang.reflect InvocationTargetException getTargetException

List of usage examples for java.lang.reflect InvocationTargetException getTargetException

Introduction

In this page you can find the example usage for java.lang.reflect InvocationTargetException getTargetException.

Prototype

public Throwable getTargetException() 

Source Link

Document

Get the thrown target exception.

Usage

From source file:org.springframework.boot.test.context.assertj.AssertProviderApplicationContextInvocationHandler.java

private Object invokeApplicationContextMethod(Method method, Object[] args) throws Throwable {
    try {/*ww w  .  j a  va2  s.c om*/
        return method.invoke(getStartedApplicationContext(), args);
    } catch (InvocationTargetException ex) {
        throw ex.getTargetException();
    }
}

From source file:org.springframework.core.annotation.AnnotationUtils.java

/**
 * Retrieve the <em>value</em> of a named attribute, given an annotation instance.
 * @param annotation the annotation instance from which to retrieve the value
 * @param attributeName the name of the attribute value to retrieve
 * @return the attribute value, or {@code null} if not found unless the attribute
 * value cannot be retrieved due to an {@link AnnotationConfigurationException},
 * in which case such an exception will be rethrown
 * @see #getValue(Annotation)/* w  ww .jav a 2s  .co m*/
 * @see #rethrowAnnotationConfigurationException(Throwable)
 */
@Nullable
public static Object getValue(@Nullable Annotation annotation, @Nullable String attributeName) {
    if (annotation == null || !StringUtils.hasText(attributeName)) {
        return null;
    }
    try {
        Method method = annotation.annotationType().getDeclaredMethod(attributeName);
        ReflectionUtils.makeAccessible(method);
        return method.invoke(annotation);
    } catch (InvocationTargetException ex) {
        rethrowAnnotationConfigurationException(ex.getTargetException());
        throw new IllegalStateException(
                "Could not obtain value for annotation attribute '" + attributeName + "' in " + annotation, ex);
    } catch (Throwable ex) {
        handleIntrospectionFailure(annotation.getClass(), ex);
        return null;
    }
}

From source file:org.springframework.data.document.web.bind.annotation.support.HandlerMethodInvoker.java

public final void updateModelAttributes(Object handler, Map<String, Object> mavModel,
        ExtendedModelMap implicitModel, NativeWebRequest webRequest) throws Exception {

    if (this.methodResolver.hasSessionAttributes() && this.sessionStatus.isComplete()) {
        for (String attrName : this.methodResolver.getActualSessionAttributeNames()) {
            this.sessionAttributeStore.cleanupAttribute(webRequest, attrName);
        }//  ww w.  j ava2 s. com
    }

    // Expose model attributes as session attributes, if required.
    // Expose BindingResults for all attributes, making custom editors available.
    Map<String, Object> model = (mavModel != null ? mavModel : implicitModel);
    if (model != null) {
        try {
            String[] originalAttrNames = model.keySet().toArray(new String[model.size()]);
            for (String attrName : originalAttrNames) {
                Object attrValue = model.get(attrName);
                boolean isSessionAttr = this.methodResolver.isSessionAttribute(attrName,
                        (attrValue != null ? attrValue.getClass() : null));
                if (isSessionAttr) {
                    if (this.sessionStatus.isComplete()) {
                        implicitModel.put(MODEL_KEY_PREFIX_STALE + attrName, Boolean.TRUE);
                    } else if (!implicitModel.containsKey(MODEL_KEY_PREFIX_STALE + attrName)) {
                        this.sessionAttributeStore.storeAttribute(webRequest, attrName, attrValue);
                    }
                }
                if (!attrName.startsWith(BindingResult.MODEL_KEY_PREFIX)
                        && (isSessionAttr || isBindingCandidate(attrValue))) {
                    String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attrName;
                    if (mavModel != null && !model.containsKey(bindingResultKey)) {
                        WebDataBinder binder = createBinder(webRequest, attrValue, attrName);
                        initBinder(handler, attrName, binder, webRequest);
                        mavModel.put(bindingResultKey, binder.getBindingResult());
                    }
                }
            }
        } catch (InvocationTargetException ex) {
            // User-defined @InitBinder method threw an exception...
            ReflectionUtils.rethrowException(ex.getTargetException());
        }
    }
}

From source file:org.springframework.data.gemfire.listener.adapter.ContinuousQueryListenerAdapter.java

/**
 * Invoke the specified listener method.
 * @param event the event arguments to be passed in
 * @param methodName the method to invoke
 * @see #getListenerMethodName//from w ww.  jav a  2  s . com
 */
protected void invokeListenerMethod(CqEvent event, String methodName) {
    try {
        invoker.invoke(event);
    } catch (InvocationTargetException ex) {
        Throwable targetEx = ex.getTargetException();
        if (targetEx instanceof DataAccessException) {
            throw (DataAccessException) targetEx;
        } else {
            throw new GemfireListenerExecutionFailedException(
                    "Listener method '" + methodName + "' threw exception", targetEx);
        }
    } catch (Throwable ex) {
        throw new GemfireListenerExecutionFailedException("Failed to invoke target method '" + methodName, ex);
    }
}

From source file:org.springframework.data.keyvalue.redis.listener.adapter.MessageListenerAdapter.java

/**
 * Invoke the specified listener method.
 * @param methodName the name of the listener method
 * @param arguments the message arguments to be passed in
 * @return the result returned from the listener method
 * @see #getListenerMethodName/*from   ww w  . jav  a  2  s. co  m*/
 * @see #buildListenerArguments
 */
protected Object invokeListenerMethod(String methodName, Object[] arguments) {
    try {
        MethodInvoker methodInvoker = new MethodInvoker();
        methodInvoker.setTargetObject(getDelegate());
        methodInvoker.setTargetMethod(methodName);
        methodInvoker.setArguments(arguments);
        methodInvoker.prepare();
        return methodInvoker.invoke();
    } catch (InvocationTargetException ex) {
        Throwable targetEx = ex.getTargetException();
        if (targetEx instanceof DataAccessException) {
            throw (DataAccessException) targetEx;
        } else {
            throw new RedisListenerExecutionFailedException(
                    "Listener method '" + methodName + "' threw exception", targetEx);
        }
    } catch (Throwable ex) {
        throw new RedisListenerExecutionFailedException("Failed to invoke target method '" + methodName
                + "' with arguments " + ObjectUtils.nullSafeToString(arguments), ex);
    }
}

From source file:org.springframework.data.redis.listener.adapter.MessageListenerAdapter.java

/**
 * Invoke the specified listener method.
 * /*from  w  ww  .j  av  a  2s  . c om*/
 * @param methodName the name of the listener method
 * @param arguments the message arguments to be passed in
 * @see #getListenerMethodName
 */
protected void invokeListenerMethod(String methodName, Object[] arguments) {
    try {
        invoker.invoke(arguments);
    } catch (InvocationTargetException ex) {
        Throwable targetEx = ex.getTargetException();
        if (targetEx instanceof DataAccessException) {
            throw (DataAccessException) targetEx;
        } else {
            throw new RedisListenerExecutionFailedException(
                    "Listener method '" + methodName + "' threw exception", targetEx);
        }
    } catch (Throwable ex) {
        throw new RedisListenerExecutionFailedException("Failed to invoke target method '" + methodName
                + "' with arguments " + ObjectUtils.nullSafeToString(arguments), ex);
    }
}

From source file:org.springframework.faces.mvc.annotation.support.AnnotatedMethodInvoker.java

/**
 * Invoke the specified method, ensuring that the method is accessible and that all exceptions are re-thrown
 * correctly.//from ww  w.j av  a2 s  .co m
 */
private Object doInvokeMethod(Method method, Object target, Object[] args) throws Exception {
    ReflectionUtils.makeAccessible(method);
    try {
        return method.invoke(target, args);
    } catch (InvocationTargetException ex) {
        ReflectionUtils.rethrowException(ex.getTargetException());
    }
    throw new IllegalStateException("Should never get here");
}

From source file:org.springframework.integration.handler.LambdaMessageProcessor.java

@Override
public Object processMessage(Message<?> message) {
    Object[] args = buildArgs(message);

    try {//from  www  .j a  v a 2 s  . c om
        return this.method.invoke(this.target, args);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof ClassCastException) {
            logger.error(
                    "Could not invoke the method due to a class cast exception, if using a lambda in the DSL, "
                            + "consider using an overloaded EIP method that takes a Class<?> argument to explicitly "
                            + "specify the type. An example of when this often occurs is if the lambda is configured to "
                            + "receive a Message<?> argument.",
                    e.getCause());
        }
        throw new MessageHandlingException(message, e.getCause());
    } catch (Exception e) {
        throw new MessageHandlingException(message, e);
    }
}

From source file:org.springframework.jdbc.support.lob.OracleLobHandler.java

/**
 * Initialize any LOB resources before a read is done.
 * <p>This implementation calls {@code BLOB.open(BLOB.MODE_READONLY)} or
 * {@code CLOB.open(CLOB.MODE_READONLY)} on any non-temporary LOBs if
 * {@code releaseResourcesAfterRead} property is set to {@code true}.
 * <p>This method can be overridden by sublcasses if different behavior is desired.
 * @param con the connection to be usde for initilization
 * @param lob the LOB to initialize/* w  ww  .j  a v a 2s .  c  o  m*/
 */
protected void initializeResourcesBeforeRead(Connection con, Object lob) {
    if (this.releaseResourcesAfterRead) {
        initOracleDriverClasses(con);
        try {
            /*
            if (!((BLOB) lob.isTemporary() {
            */
            Method isTemporary = lob.getClass().getMethod("isTemporary");
            Boolean temporary = (Boolean) isTemporary.invoke(lob);
            if (!temporary) {
                /*
                ((BLOB) lob).open(BLOB.MODE_READONLY);
                */
                Method open = lob.getClass().getMethod("open", int.class);
                open.invoke(lob, this.modeReadOnlyConstants.get(lob.getClass()));
            }
        } catch (InvocationTargetException ex) {
            logger.error("Could not open Oracle LOB", ex.getTargetException());
        } catch (Exception ex) {
            throw new DataAccessResourceFailureException("Could not open Oracle LOB", ex);
        }
    }
}

From source file:org.springframework.jdbc.support.lob.OracleLobHandler.java

/**
 * Release any LOB resources after read is complete.
 * <p>If {@code releaseResourcesAfterRead} property is set to {@code true}
 * then this implementation calls/*from   w w w. ja  v a  2 s. c o m*/
 * {@code BLOB.close()} or {@code CLOB.close()}
 * on any non-temporary LOBs that are open or
 * {@code BLOB.freeTemporary()} or {@code CLOB.freeTemporary()}
 * on any temporary LOBs.
 * <p>This method can be overridden by sublcasses if different behavior is desired.
 * @param con the connection to be usde for initilization
 * @param lob the LOB to initialize
 */
protected void releaseResourcesAfterRead(Connection con, Object lob) {
    if (this.releaseResourcesAfterRead) {
        initOracleDriverClasses(con);
        Boolean temporary = Boolean.FALSE;
        try {
            /*
            if (((BLOB) lob.isTemporary() {
            */
            Method isTemporary = lob.getClass().getMethod("isTemporary");
            temporary = (Boolean) isTemporary.invoke(lob);
            if (temporary) {
                /*
                ((BLOB) lob).freeTemporary();
                */
                Method freeTemporary = lob.getClass().getMethod("freeTemporary");
                freeTemporary.invoke(lob);
            } else {
                /*
                if (((BLOB) lob.isOpen() {
                */
                Method isOpen = lob.getClass().getMethod("isOpen");
                Boolean open = (Boolean) isOpen.invoke(lob);
                if (open) {
                    /*
                    ((BLOB) lob).close();
                    */
                    Method close = lob.getClass().getMethod("close");
                    close.invoke(lob);
                }
            }
        } catch (InvocationTargetException ex) {
            if (temporary) {
                logger.error("Could not free Oracle LOB", ex.getTargetException());
            } else {
                logger.error("Could not close Oracle LOB", ex.getTargetException());
            }
        } catch (Exception ex) {
            if (temporary) {
                throw new DataAccessResourceFailureException("Could not free Oracle LOB", ex);
            } else {
                throw new DataAccessResourceFailureException("Could not close Oracle LOB", ex);
            }
        }
    }
}