Example usage for java.lang.reflect InvocationTargetException getCause

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

Introduction

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

Prototype

public Throwable getCause() 

Source Link

Document

Returns the cause of this exception (the thrown target exception, which may be null ).

Usage

From source file:com.clicktravel.cheddar.application.retry.RetryableAspect.java

private Object processMethodFailure(final ProceedingJoinPoint proceedingJoinPoint,
        final String[] exceptionHandlerNames, final Throwable thrownException) throws Throwable {
    final Method handlerMethod = getHandlerMethod(proceedingJoinPoint, thrownException.getClass(),
            exceptionHandlerNames);/*from  w  w  w. j a  v a  2s  .  c o m*/
    if (handlerMethod != null) {
        logger.trace("Selected handlerMethod : " + handlerMethod.getName());
        try {
            return handlerMethod.invoke(proceedingJoinPoint.getThis(),
                    getExceptionHandlerArgs(thrownException, proceedingJoinPoint.getArgs()));
        } catch (final InvocationTargetException invocationTargetException) {
            throw invocationTargetException.getCause(); // exception thrown by handler method
        }
    } else {
        throw thrownException;
    }
}

From source file:com.github.jknack.handlebars.helper.MethodHelper.java

@Override
public CharSequence apply(final Object context, final Options options) throws IOException {
    Class<?>[] paramTypes = method.getParameterTypes();
    Object[] args = new Object[paramTypes.length];
    // collect the parameters
    int pidx = 0;
    for (int i = 0; i < paramTypes.length; i++) {
        Class<?> paramType = paramTypes[i];
        Object ctx = i == 0 ? context : null;
        Options opts = i == paramTypes.length - 1 ? options : null;
        Object candidate = options.param(pidx, null);
        Object arg = argument(paramType, candidate, ctx, opts);
        args[i] = arg;//  w  w w .jav a  2  s  .c om
        if (candidate == arg) {
            pidx++;
        }
    }
    try {
        return (CharSequence) method.invoke(source, args);
    } catch (InvocationTargetException ex) {
        throw launderThrowable(ex.getCause());
    } catch (IllegalAccessException ex) {
        throw new IllegalStateException("could not execute helper: " + method.getName(), ex);
    }
}

From source file:smartrics.rest.fitnesse.fixture.PartsFactoryTest.java

@Test
public void buildsRestClientWithoutSquareBracketsInUri() throws Exception {
    // URI validation will throw an exception as per httpclient 3.1
    Config c = Config.getConfig();//from w  ww . j  a  v  a  2s. c  o m
    RestClient restClient = f.buildRestClient(c);
    assertThat(restClient, is(instanceOf(RestClient.class)));
    Method m = getCreateUriMethod(restClient);
    m.setAccessible(true);
    try {
        m.invoke(restClient, "http://localhost:9900?something[data]=1", true);
    } catch (InvocationTargetException e) {
        assertThat(e.getCause(), is(instanceOf(URIException.class)));
    }
}

From source file:com.krawler.br.nodes.Activity.java

@Override
synchronized public void invoke() throws ProcessException {
    KwlLoader loader = operation.getEntityLoader();
    if (loader == null)
        throw new ProcessException("No loader available for entity " + operation.getEntityName());
    Object obj = loader.load(operation.getEntityName());
    if (obj == null)
        throw new ProcessException(
                "Entity not found [" + operation.getEntityName() + "] for operation :" + operation.getName());
    try {//from w w  w  .j  a  v  a  2 s  .  c o  m
        Object res = MethodUtils.invokeMethod(obj, operation.getMethodName(), getParams());
        if (operation.getOutputParameter() != null) {
            heap.put(operation.getOutputParameter().getName(), res);
        }
    } catch (InvocationTargetException ex) {
        throw new ProcessException(ex.getCause().getMessage(), ex.getCause());
    } catch (NoSuchMethodException ex) {
        throw new ProcessException(ex.getMessage(), ex);
    } catch (IllegalAccessException ex) {
        throw new ProcessException(ex.getMessage(), ex);
    }
}

From source file:org.unitils.orm.jpa.util.JpaEntityManagerFactoryLoader.java

/**
 * @param testObject The test instance, not null
 * @param jpaConfig  The configuration parameters for the <code>EntityManagerFactory</code>
 * @return A completely configured <code>AbstractEntityManagerFactoryBean</code>
 *//*from w  w w.j  av  a2  s  .  c om*/
protected AbstractEntityManagerFactoryBean createEntityManagerFactoryBean(Object testObject,
        JpaConfig jpaConfig) {
    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(getDataSource());
    factoryBean.setJpaVendorAdapter(getJpaProviderSupport().getSpringJpaVendorAdaptor());
    String persistenceXmlFile = jpaConfig.getConfigFiles().iterator().next();
    if (!StringUtils.isEmpty(persistenceXmlFile)) {
        factoryBean.setPersistenceXmlLocation(persistenceXmlFile);
    }
    factoryBean.setPersistenceUnitName(jpaConfig.getPersistenceUnitName());
    LoadTimeWeaver loadTimeWeaver = getJpaProviderSupport().getLoadTimeWeaver();
    if (loadTimeWeaver != null) {
        factoryBean.setLoadTimeWeaver(loadTimeWeaver);
    }
    if (jpaConfig.getConfigMethod() != null) {
        try {
            ReflectionUtils.invokeMethod(testObject, jpaConfig.getConfigMethod(), factoryBean);
        } catch (InvocationTargetException e) {
            throw new UnitilsException("Error while invoking custom config method", e.getCause());
        }
    }
    factoryBean.afterPropertiesSet();
    return factoryBean;
}

From source file:com.thoughtworks.go.util.NestedJarClassLoader.java

private void handleClassNotFound(InvocationTargetException e) throws ClassNotFoundException {
    if (e.getCause() instanceof ClassNotFoundException) {
        throw (ClassNotFoundException) e.getCause();
    }/* www .  ja va2 s  . c o  m*/
}

From source file:com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractTypeAwareCheckTest.java

@Test
public void testClassRegularClass() throws Exception {
    Class<?> tokenType = Class.forName("com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck$Token");

    Class<?> regularClassType = Class
            .forName("com.puppycrawl.tools.checkstyle.checks.AbstractTypeAwareCheck$RegularClass");
    Constructor<?> regularClassConstructor = regularClassType.getDeclaredConstructor(tokenType, String.class,
            AbstractTypeAwareCheck.class);
    regularClassConstructor.setAccessible(true);

    try {//  w ww.  j av  a  2s .c o  m
        regularClassConstructor.newInstance(null, "", new JavadocMethodCheck());
    } catch (InvocationTargetException ex) {
        assertTrue(ex.getCause() instanceof IllegalArgumentException);
        assertEquals("ClassInfo's name should be non-null", ex.getCause().getMessage());
    }

    Constructor<?> tokenConstructor = tokenType.getDeclaredConstructor(String.class, int.class, int.class);
    Object token = tokenConstructor.newInstance("blablabla", 1, 1);

    Object regularClass = regularClassConstructor.newInstance(token, "sur", new JavadocMethodCheck());

    Method toString = regularClass.getClass().getDeclaredMethod("toString");
    toString.setAccessible(true);
    String result = (String) toString.invoke(regularClass);
    assertEquals("RegularClass[name=Token[blablabla(1x1)], in class=sur, loadable=true," + " class=null]",
            result);

    Method setClazz = regularClass.getClass().getDeclaredMethod("setClazz", Class.class);
    setClazz.setAccessible(true);
    Class<?> arg = null;
    setClazz.invoke(regularClass, arg);

    Method getClazz = regularClass.getClass().getDeclaredMethod("getClazz");
    getClazz.setAccessible(true);
    assertNull(getClazz.invoke(regularClass));
}

From source file:com.puppycrawl.tools.checkstyle.checks.header.HeaderCheckTest.java

@Test
public void testIoExceptionWhenLoadingHeaderFile() throws Exception {
    HeaderCheck check = PowerMockito.spy(new HeaderCheck());
    PowerMockito.doThrow(new IOException("expected exception")).when(check, "loadHeader", anyObject());

    check.setHeaderFile(getPath("InputRegexpHeader1.java"));

    Method loadHeaderFile = AbstractHeaderCheck.class.getDeclaredMethod("loadHeaderFile");
    loadHeaderFile.setAccessible(true);/*from  www . j  a  v  a 2 s .  c o m*/
    try {
        loadHeaderFile.invoke(check);
        fail("Exception expected");
    } catch (InvocationTargetException ex) {
        assertTrue(ex.getCause() instanceof CheckstyleException);
        assertEquals("unable to load header file " + getPath("InputRegexpHeader1.java"),
                ex.getCause().getMessage());
    }
}

From source file:org.jboss.qa.phaser.ExecutionNode.java

public ExecutionError execute(boolean finalize) {

    // finalizing state, skip non run always methods
    if (finalize && !phaseDefinition.isRunAlways()) {
        return null;
    }// ww  w .j  av a2s.c o m

    try {
        // Invoke phase definition processor
        processor.execute();

        // If phase definition has method, invoke it
        if (phaseDefinition.getMethod() != null) {

            final Class<?>[] paramClasses = phaseDefinition.getMethod().getParameterTypes();
            final Annotation[][] paramAnnotations = phaseDefinition.getMethod().getParameterAnnotations();

            if (paramClasses.length == 0) { // no parameters to inject
                phaseDefinition.getMethod().invoke(phaseDefinition.getJob());
            } else {
                final List<List<Object>> paramInstances = new ArrayList<>();
                for (int i = 0; i < paramClasses.length; i++) {
                    boolean created = false;
                    for (int j = 0; j < paramAnnotations[i].length; j++) {
                        if (paramAnnotations[i][j] instanceof Create) { // Create instance for @Create params
                            final Create create = (Create) paramAnnotations[i][j];
                            final Object o = paramClasses[i].newInstance();
                            if (StringUtils.isNotEmpty(create.id())) {
                                InstanceRegistry.insert(create.id(), o);
                            } else {
                                InstanceRegistry.insert(o);
                            }
                            // Add created instance as unique instance of parameter
                            final List<Object> ip = new ArrayList<>();
                            ip.add(o);
                            paramInstances.add(ip);
                            created = true;
                        }
                    }
                    if (!created) { // Find all existing instances
                        paramInstances.add(InstanceRegistry.get(paramClasses[i]));
                    }
                }

                for (List<Object> paramList : createCartesianProduct(paramInstances)) {
                    phaseDefinition.getMethod().invoke(phaseDefinition.getJob(), paramList.toArray());
                }
            }
        }
        return null; // ok, no exception
    } catch (InvocationTargetException e) {
        return generateExecutionError(e.getCause());
    } catch (Throwable e) {
        return generateExecutionError(e);
    }
}

From source file:com.googlecode.jsonschema2pojo.integration.EnumIT.java

@Test
public void enumDeserializationMethodRejectsInvalidValues()
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    Method fromValue = enumClass.getMethod("fromValue", String.class);

    try {/*from   w  w w . j  a  v  a  2s  .  c o m*/
        fromValue.invoke(enumClass, "something invalid");
        fail();
    } catch (InvocationTargetException e) {
        assertThat(e.getCause(), is(instanceOf(IllegalArgumentException.class)));
    }

}