Example usage for java.lang ReflectiveOperationException getCause

List of usage examples for java.lang ReflectiveOperationException getCause

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.openo.nfvo.resmanagement.common.util.RestfulUtil.java

/**
 * encapsulate the java reflect exception.<br>
 *
 * @param methodName, Restful's method./*from www.  j  a  v  a2s  . c om*/
 * @param objects, method param array.
 * @return
 * @since NFVO 0.5
 */
public static RestfulResponse getRestRes(String methodName, Object... objects) {
    try {
        if (objects == null || REST_CLIENT == null) {
            return null;
        }

        Class<?>[] classes = new Class[objects.length];
        for (int i = 0; i < objects.length; i++) {
            classes[i] = objects[i].getClass();
        }
        if (methodName.startsWith("async")) {
            classes[classes.length - 1] = RestfulAsyncCallback.class;
        }

        Class<?> rtType = methodName.startsWith("async") ? void.class : RestfulResponse.class;
        MethodType mt = MethodType.methodType(rtType, classes);
        Object result = MethodHandles.lookup().findVirtual(REST_CLIENT.getClass(), methodName, mt)
                .bindTo(REST_CLIENT).invokeWithArguments(objects);
        if (result != null) {
            return (RestfulResponse) result;
        }
        LOGGER.warn("function=getRestRes, msg: invoke Restful async {} method which return type is Void.",
                methodName);
        return null;
    } catch (ReflectiveOperationException e) {
        LOGGER.error("function=getRestRes, msg=error occurs, e={}.", e);
    } catch (Throwable e) {// NOSONAR
        LOGGER.error("function=getRestRes, msg=Throwable, e={}.", e);
        try {
            throw (ServiceException) new ServiceException().initCause(e.getCause());
        } catch (ServiceException se) {
            LOGGER.error("function=getRestRes, msg=ServiceException occurs, e={}.", se);
        }
    }
    return null;
}