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:com.egreen.tesla.server.api.config.resolver.RequestResolver.java

public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ClassNotFoundException, CannotCompileException, InstantiationException, IllegalAccessException,
        NoSuchMethodException, IllegalArgumentException, InvocationTargetException, IOException,
        ServletException, SecurityException, NotFoundException {
    boolean noError = true;
    if (requestMapping == null) {
        requestMapping = (RequestMapping) ctMethod.getAnnotation(RequestMapping.class);
    }/*from  w w w.j  a  v  a2s .  c  o  m*/

    ObjectResolver objectResolver = ObjectResolver.getInstance(request, response);
    Object newInstance = objectResolver.getInstance(ctClass);

    LOGGER.info("Object Class " + newInstance);
    Field[] fields1 = ctClass.getDeclaredFields();
    for (Field field : fields1) {
        field.setAccessible(true);
        LOGGER.info("Retrive All Feild Has Values " + field.get(newInstance));

    }

    Method method = null;
    Object methodParams[] = null;
    Class[] methodParamType = null;
    for (Object[] objects : ctMethod.getParameterAnnotations()) {
        if (objects != null) {
            methodParamType = new Class[objects.length];
            methodParams = new Object[objects.length];
            for (int i = 0; i < objects.length; i++) {
                Object objectInstance = objects[i];
                if (objectInstance instanceof Param) {
                    Param param = (Param) objectInstance;
                    methodParamType[i] = param.value().getClass();
                    String parameter = request.getParameter(param.value());
                    methodParams[i] = parameter;
                    if (parameter == null) {
                        sendErrorMessage(response, "Invalid parameter " + param.value());
                        noError = false;
                    }
                }
            }
        }
    }

    if (noError) {
        LOGGER.info(Arrays.toString(methodParamType));
        if (methodParamType == null || methodParamType.length == 0) {
            method = ctClass.getMethod(ctMethod.getName());
        } else {
            method = ctClass.getMethod(ctMethod.getName(), methodParamType);
        }
        Object invoke = null;
        LOGGER.info("Going to Invok " + method + " On " + newInstance);
        if (methodParams == null || methodParams.length == 0) {

            invoke = method.invoke(newInstance);
        } else {
            LOGGER.info(Arrays.toString(methodParams));
            try {
                invoke = method.invoke(newInstance, methodParams);
                LOGGER.info(invoke);
            } catch (InvocationTargetException exception) {
                exception.getTargetException().printStackTrace();
            }
        }

        if (method.getAnnotation(ResponseBody.class) != null) {
            // Json
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
            StringWriter stringEmp = new StringWriter();
            objectMapper.writeValue(stringEmp, invoke);
            response.getWriter().write(stringEmp.toString());
        } else if (invoke instanceof String) {
            String resorcePath = templateResolver.resorcePath(invoke + "");
            LOGGER.debug(resorcePath);
            LOGGER.debug(request);
            LOGGER.debug(response);
            request.setAttribute("viewpath", resorcePath);
            request.getRequestDispatcher("/").forward(request, response);

            // response.sendRedirect("/");
        } else {
            sendErrorMessage(response, "Cannot resolve Response Object");
        }
    }

}

From source file:gov.nih.nci.firebird.proxy.PoolingHandler.java

private Object invoke(Method method, Object[] args) throws Throwable {
    Object client = pool.borrowObject();
    try {//  w w w  . ja v  a2 s  . co m
        Object ret = method.invoke(client, args);
        pool.returnObject(client);
        return ret;
    } catch (InvocationTargetException t) {
        if (isValidException(t.getTargetException())) {
            pool.returnObject(client);
        } else {
            pool.invalidateObject(client);
            LOG.error("invalidated delegate due to an unexpected exception", t.getTargetException());
        }
        throw t.getTargetException();
    }
}

From source file:pl.com.bottega.ecommerce.system.saga.impl.SimpleSagaEngine.java

/**
 * TODO handle exception in more generic way
 *///from   ww  w .  ja va  2s .  c  o  m
@SuppressWarnings("rawtypes")
private Object loadSagaData(SagaManager loader, Object event) {
    Method loaderMethod = findHandlerMethodForEvent(loader.getClass(), event);
    try {
        Object sagaData = loaderMethod.invoke(loader, event);
        return sagaData;
    } catch (InvocationTargetException e) {
        // NRE is ok here, it means that saga hasn't been started yet
        if (e.getTargetException() instanceof NoResultException) {
            return null;
        } else {
            throw new RuntimeException(e);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Invoke method./*w w  w  .j  ava  2s. c o m*/
 *
 * @param bean the bean
 * @param method the method
 * @param args the args
 * @return the object
 */
public static Object invokeMethod(Object bean, Method method, Object... args) {
    AccessibleScope ascope = new AccessibleScope(method);
    try {
        return method.invoke(bean, args);
    } catch (InvocationTargetException ex) {
        if (ex.getTargetException() instanceof RuntimeException) {
            throw (RuntimeException) ex.getTargetException();
        }
        throw new RuntimeException("Failure calling method: " + bean.getClass().getName() + "."
                + method.getName() + ": " + ex.getMessage(), ex);
    } catch (Exception ex) {
        throw new RuntimeException("Failure calling method: " + bean.getClass().getName() + "."
                + method.getName() + ": " + ex.getMessage(), ex);
    } finally {
        ascope.restore();
    }
}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Invokes a static method.//w  ww  .  j  a v a 2 s .  c om
 *
 * @param clazz the clazz
 * @param method the method
 * @param args the args
 * @return return value of method call
 * @throws RuntimeException if bean is null, method cannot be found
 */
public static Object invokeStaticMethod(Class<?> clazz, String method, Object... args) {
    Method m = findMethod(null, clazz, method, args);
    if (m == null) {
        throw new RuntimeException(
                "Canot find method to call: " + clazz.getName() + "." + method + getArgsDescriptor(args));
    }
    AccessibleScope ascope = new AccessibleScope(m);
    try {
        return m.invoke(null, args);
    } catch (InvocationTargetException ex) {
        if (ex.getTargetException() instanceof RuntimeException) {
            throw (RuntimeException) ex.getTargetException();
        }
        throw new RuntimeException(
                "Failure calling method: " + clazz.getName() + "." + method + ": " + ex.getMessage(), ex);
    } catch (Exception ex) {
        throw new RuntimeException(
                "Failure calling method: " + clazz.getName() + "." + method + ": " + ex.getMessage(), ex);
    } finally {
        ascope.restore();
    }
}

From source file:org.xins.common.spring.XinsClientInterceptor.java

public Object invoke(MethodInvocation invocation) throws Throwable {
    if (this.capi == null) {
        throw new IllegalStateException("XinsClientInterceptor is not properly initialized - "
                + "invoke 'prepare' before attempting any operations");
    }//from   w w w.j a  v  a 2 s.com

    try {
        return invocation.getMethod().invoke(this.capi, invocation.getArguments());
    } catch (InvocationTargetException ex) {
        if (ex.getTargetException() instanceof XINSCallException) {
            XINSCallException callEx = (XINSCallException) ex.getTargetException();
            throw convertXinsAccessException(callEx);
        }
        throw ex.getTargetException();
    } catch (Throwable ex) {
        throw new RemoteProxyFailureException(
                "Failed to invoke XINS API for remote service [" + getServiceUrl() + "]", ex);
    }
}

From source file:com.sun.faces.el.MethodBindingImpl.java

public Object invoke(FacesContext context, Object params[])
        throws EvaluationException, MethodNotFoundException {

    if (context == null) {
        throw new NullPointerException();
    }/*  w w  w.j  av  a2  s  .c  o  m*/
    Object base = vb.getValue(context);
    Method method = method(base);
    try {
        return (method.invoke(base, params));
    } catch (IllegalAccessException e) {
        throw new EvaluationException(e);
    } catch (InvocationTargetException ite) {
        throw new EvaluationException(ite.getTargetException());
    }

}

From source file:com.espertech.esper.event.bean.BeanInstantiatorByCtor.java

public Object instantiate() {
    try {/*from w  w w  .jav  a  2s  .c  o m*/
        return ctor.newInstance();
    } catch (InvocationTargetException e) {
        String message = "Unexpected exception encountered invoking constructor '" + ctor.getName()
                + "' on class '" + ctor.getDeclaringClass().getName() + "': "
                + e.getTargetException().getMessage();
        log.error(message, e);
        return null;
    } catch (IllegalAccessException ex) {
        return handle(ex);
    } catch (InstantiationException ex) {
        return handle(ex);
    }
}

From source file:com.espertech.esper.event.bean.BeanEventPropertyWriter.java

protected void invoke(Object[] values, Object target) {
    try {/*from  w  w  w .j ava  2s .  c om*/
        writerMethod.invoke(target, values);
    } catch (InvocationTargetException e) {
        String message = "Unexpected exception encountered invoking setter-method '"
                + writerMethod.getJavaMethod() + "' on class '" + clazz.getName() + "' : "
                + e.getTargetException().getMessage();
        log.error(message, e);
    }
}

From source file:com.espertech.esper.event.bean.BeanInstantiatorByFactoryReflection.java

public Object instantiate() {
    try {/*from w ww  .j  av  a  2s .c  o m*/
        return method.invoke(null, null);
    } catch (InvocationTargetException e) {
        String message = "Unexpected exception encountered invoking factory method '" + method.getName()
                + "' on class '" + method.getDeclaringClass().getName() + "': "
                + e.getTargetException().getMessage();
        log.error(message, e);
        return null;
    } catch (IllegalAccessException ex) {
        String message = "Unexpected exception encountered invoking factory method '" + method.getName()
                + "' on class '" + method.getDeclaringClass().getName() + "': " + ex.getMessage();
        log.error(message, ex);
        return null;
    }
}