Example usage for java.lang.reflect Method getReturnType

List of usage examples for java.lang.reflect Method getReturnType

Introduction

In this page you can find the example usage for java.lang.reflect Method getReturnType.

Prototype

public Class<?> getReturnType() 

Source Link

Document

Returns a Class object that represents the formal return type of the method represented by this Method object.

Usage

From source file:de.dfki.kiara.testapp.TestInvocationHandler.java

@Override
protected Object handleInvocation(Object o, Method method, Object[] os) throws Throwable {
    ObjectMapper mapper = new ObjectMapper();
    String out = mapper.writeValueAsString(os[0]);
    Object obj = mapper.readValue(out, method.getReturnType());
    System.out.println("invoke: object: " + o + " Method: " + method + " Object[] " + Arrays.toString(os));
    System.out.println("String: " + out);
    System.out.println("Object: " + obj);
    return null;/*www .j av a2s.  com*/
}

From source file:com.flipkart.flux.client.intercept.WorkflowInterceptor.java

private void checkForBadSignatures(MethodInvocation invocation) {
    Method method = invocation.getMethod();
    final Class<?> returnType = method.getReturnType();
    if (!returnType.equals(void.class)) {
        throw new IllegalSignatureException(new MethodId(method), "A workflow method can only return void");
    }//from w  w w.j a va  2  s . co  m
    final Class<?>[] parameterTypes = method.getParameterTypes();
    for (Class<?> aParamType : parameterTypes) {
        if (!Event.class.isAssignableFrom(aParamType) && !aParamType.isArray()) {
            throw new IllegalSignatureException(new MethodId(method),
                    "Parameter types should implement the Event interface. Collections of events are also not allowed");
        }
    }

}

From source file:com.invariantproperties.project.student.webservice.server.rest.UnexpectedResourceExceptionHandler.java

/**
 * Check for an unhandled exception from a REST resource. If we catch one
 * AND the method returns a Response we can return a Server Internal Error
 * (500) error code instead of blowing up. We need to check though since
 * some methods don't return a Response.
 * //from  w w w  . j av a 2s .  co  m
 * @param pjp
 * @return
 * @throws Throwable
 */
@Around("target(com.invariantproperties.project.student.webservice.server.rest.AbstractResource)")
public Object checkForUnhandledException(ProceedingJoinPoint pjp) throws Throwable {
    Object results = null;
    Logger log = Logger.getLogger(pjp.getSignature().getClass());

    try {
        results = pjp.proceed(pjp.getArgs());
    } catch (ObjectNotFoundException e) {
        // this is safe to log since we know that we've passed filtering.
        String args = Arrays.toString(pjp.getArgs());
        results = Response.status(Status.NOT_FOUND).entity("object not found: " + args).build();
        if (log.isDebugEnabled()) {
            log.debug("object not found: " + args);
        }
    } catch (Exception e) {
        // find the method we called. We can't cache this since the method
        // may be overloaded
        Method method = findMethod(pjp);
        if ((method != null) && Response.class.isAssignableFrom(method.getReturnType())) {
            // if the method returns a response we can return a 500 message.
            if (!(e instanceof UnitTestException)) {
                if (log.isInfoEnabled()) {
                    log.info(String.format("%s(): unhandled exception: %s", pjp.getSignature().getName(),
                            e.getMessage()), e);
                }
            } else if (log.isTraceEnabled()) {
                log.info("unit test exception: " + e.getMessage());
            }
            results = Response.status(Status.INTERNAL_SERVER_ERROR).build();
        } else {
            // DO NOT LOG THE EXCEPTION. That just clutters the log - let
            // the final handler log it.
            throw e;
        }
    }

    return results;
}

From source file:com.invariantproperties.sandbox.student.webservice.server.rest.UnexpectedResourceExceptionHandler.java

/**
 * Check for an unhandled exception from a REST resource. If we catch one
 * AND the method returns a Response we can return a Server Internal Error
 * (500) error code instead of blowing up. We need to check though since
 * some methods don't return a Response.
 * /*from w  w w  .  ja  v  a  2 s .  c  o  m*/
 * @param pjp
 * @return
 * @throws Throwable
 */
@Around("target(com.invariantproperties.sandbox.student.webservice.server.rest.AbstractResource)")
public Object checkForUnhandledException(ProceedingJoinPoint pjp) throws Throwable {
    Object results = null;
    Logger log = Logger.getLogger(pjp.getSignature().getClass());

    try {
        results = pjp.proceed(pjp.getArgs());
    } catch (ObjectNotFoundException e) {
        // this is safe to log since we know that we've passed filtering.
        String args = Arrays.toString(pjp.getArgs());
        results = Response.status(Status.NOT_FOUND).entity("object not found: " + args).build();
        if (log.isDebugEnabled()) {
            log.debug("object not found: " + args);
        }
    } catch (Exception e) {
        // find the method we called. We can't cache this since the method
        // may be overloaded
        Method method = findMethod(pjp);
        if ((method != null) && Response.class.isAssignableFrom(method.getReturnType())) {
            // if the method returns a response we can return a 500 message.
            if (!(e instanceof UnitTestException)) {
                if (log.isInfoEnabled()) {
                    log.info(String.format("%s(): unhandled exception: %s", pjp.getSignature().getName(),
                            e.getMessage()), e);
                }
            } else if (log.isTraceEnabled()) {
                log.info("unit test exception: " + e.getMessage());
            }
            results = Response.status(Status.INTERNAL_SERVER_ERROR).build();
        } else {
            // DO NOT LOG THE EXCEPTION. That just clutters the log - let
            // the final handler log it.
            throw e;
        }
    }

    return results;
}

From source file:com.otterca.ca.webservice.server.rest.UnexpectedResourceExceptionHandler.java

/**
 * Check for an unhandled exception from a REST resource. If we catch one
 * AND the method returns a Response we can return a Server Internal Error
 * (500) error code instead of blowing up. We need to check though since
 * some methods don't return a Response.
 * /*w  w  w .  j  av  a 2 s.  co m*/
 * @param pjp
 * @return
 * @throws Throwable
 */
@Around("target(com.otterca.ca.webservice.server.rest.AbstractResource)")
public Object checkForUnhandledException(ProceedingJoinPoint pjp) throws Throwable {
    Object results = null;
    Logger log = Logger.getLogger(pjp.getSignature().getClass());

    try {
        results = pjp.proceed(pjp.getArgs());
        //} catch (ObjectNotFoundException e) {
        //    // this is safe to log since we know that we've passed filtering.
        //    String args = Arrays.toString(pjp.getArgs());
        //    results = Response.status(Status.NOT_FOUND).entity("object not found: " + args).build();
        //    if (log.isDebugEnabled()) {
        //        log.debug("object not found: " + args);
        //    }
    } catch (Exception e) {
        // find the method we called. We can't cache this since the method
        // may be overloaded
        Method method = findMethod(pjp);
        if ((method != null) && Response.class.isAssignableFrom(method.getReturnType())) {
            // if the method returns a response we can return a 500 message.
            if (!(e instanceof UnitTestException)) {
                if (log.isInfoEnabled()) {
                    log.info(String.format("%s(): unhandled exception: %s", pjp.getSignature().getName(),
                            e.getMessage()), e);
                }
            } else if (log.isTraceEnabled()) {
                log.info("unit test exception: " + e.getMessage());
            }
            results = Response.status(Status.INTERNAL_SERVER_ERROR).build();
        } else {
            // DO NOT LOG THE EXCEPTION. That just clutters the log - let
            // the final handler log it.
            throw e;
        }
    }

    return results;
}

From source file:com.ottervpn.webservice.server.rest.UnexpectedResourceExceptionHandler.java

/**
 * Check for an unhandled exception from a REST resource. If we catch one
 * AND the method returns a Response we can return a Server Internal Error
 * (500) error code instead of blowing up. We need to check though since
 * some methods don't return a Response.
 * //from  w w  w .ja  v a 2s  . c om
 * @param pjp
 * @return
 * @throws Throwable
 */
@Around("target(com.ottervpn.webservice.server.rest.AbstractResource)")
public Object checkForUnhandledException(ProceedingJoinPoint pjp) throws Throwable {
    Object results = null;
    Logger log = Logger.getLogger(pjp.getSignature().getClass());

    try {
        results = pjp.proceed(pjp.getArgs());
        //} catch (ObjectNotFoundException e) {
        //    // this is safe to log since we know that we've passed filtering.
        //    String args = Arrays.toString(pjp.getArgs());
        //    results = Response.status(Status.NOT_FOUND).entity("object not found: " + args).build();
        //    if (log.isDebugEnabled()) {
        //        log.debug("object not found: " + args);
        //    }
    } catch (Exception e) {
        // find the method we called. We can't cache this since the method
        // may be overloaded
        Method method = findMethod(pjp);
        if ((method != null) && Response.class.isAssignableFrom(method.getReturnType())) {
            // if the method returns a response we can return a 500 message.
            if (!(e instanceof UnitTestException)) {
                if (log.isInfoEnabled()) {
                    log.info(String.format("%s(): unhandled exception: %s", pjp.getSignature().getName(),
                            e.getMessage()), e);
                }
            } else if (log.isTraceEnabled()) {
                log.info("unit test exception: " + e.getMessage());
            }
            results = Response.status(Status.INTERNAL_SERVER_ERROR).build();
        } else {
            // DO NOT LOG THE EXCEPTION. That just clutters the log - let
            // the final handler log it.
            throw e;
        }
    }

    return results;
}

From source file:io.ingenieux.lambada.invoker.UserHandlerFactory.java

protected boolean hasReturnType(Method m, Class<?> t) {
    return m.getReturnType().equals(t);
}

From source file:net.kamhon.ieagle.util.VoUtil.java

/**
 * To upperCase object String field/*ww w .ja  v a 2s.co m*/
 * 
 * @param all
 *            default is false. true means toUpperCase all String field, false toUpperCase the fields have
 *            net.kamhon.ieagle.vo.core.annotation.ToUpperCase.
 * 
 * @param objects
 */
public static void toUpperCaseProperties(boolean all, Object... objects) {
    for (Object object : objects) {
        if (object == null) {
            continue;
        }

        // getter for String field only
        Map<String, Method> getterMap = new HashMap<String, Method>();
        // setter for String field only
        Map<String, Method> setterMap = new HashMap<String, Method>();

        Class<?> clazz = object.getClass();

        Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            /*
             * log.debug("method = " + method.getName());
             * log.debug("method.getParameterTypes().length = " +
             * method.getParameterTypes().length); if
             * (method.getParameterTypes().length == 1)
             * log.debug("method.getParameterTypes()[0] = " +
             * method.getParameterTypes()[0]);
             * log.debug("method.getReturnType() = " +
             * method.getReturnType());
             * log.debug("================================================="
             * );
             */
            if (method.getName().startsWith("get") && method.getParameterTypes().length == 0
                    && method.getReturnType().equals(String.class)) {

                // if method name is getXxx
                String fieldName = method.getName().substring(3);
                fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);

                getterMap.put(fieldName, method);
            } else if (method.getName().startsWith("set") && method.getParameterTypes().length == 1
                    && method.getParameterTypes()[0] == String.class
                    && method.getReturnType().equals(void.class)) {
                // log.debug("setter = " + method.getName());

                // if method name is setXxx
                String fieldName = method.getName().substring(3);
                fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1);

                setterMap.put(fieldName, method);
            }
        }

        // if the key exists in both getter & setter
        for (String key : getterMap.keySet()) {
            if (setterMap.containsKey(key)) {
                try {
                    Method getterMethod = getterMap.get(key);
                    Method setterMethod = setterMap.get(key);

                    // if not all, check on Field
                    if (!all) {
                        Field field = null;

                        Class<?> tmpClazz = clazz;
                        // looping up to superclass to get decleared field
                        do {
                            try {
                                field = tmpClazz.getDeclaredField(key);
                            } catch (Exception ex) {
                                // do nothing
                            }

                            if (field != null) {
                                break;
                            }

                            tmpClazz = tmpClazz.getSuperclass();
                        } while (tmpClazz != null);

                        ToUpperCase toUpperCase = field.getAnnotation(ToUpperCase.class);
                        if (toUpperCase == null || toUpperCase.upperCase() == false) {
                            continue;
                        }
                    }

                    String value = (String) getterMethod.invoke(object, new Object[] {});

                    if (StringUtils.isNotBlank(value))
                        setterMethod.invoke(object, value.toUpperCase());

                } catch (Exception ex) {
                    // log.error("Getter Setter for " + key + " has error ", ex);
                }
            }
        }
    }
}

From source file:de.anhquan.config4j.internal.ConfigHandler.java

private Object invokeGetter(Object proxy, Method method) {
    @SuppressWarnings("unchecked")
    Class clsReturnType = method.getReturnType();

    String strReturnType = StringUtils.capitalize(ClassUtils.getShortClassName(clsReturnType));
    String configMethodName = "get" + strReturnType;

    String propName = findPropertyName(method);

    try {/*  w w w .  ja  v a 2 s.c  om*/
        Method getter = Configuration.class.getMethod(configMethodName, String.class); //String.class is for propName
        return getter.invoke(configuration, propName);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {

        return findDefaultValue(method, clsReturnType);
    }
    return null;
}

From source file:ws.antonov.config.api.consumer.ConfigClientInvocationHandler.java

public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
    ConfigParamsBuilder.ConfigParamsMap configParams = generateConfigParams(method, objects);
    Assert.isAssignable(Message.class, method.getReturnType());
    return configClient.getConfig((Class<Message>) method.getReturnType(), configParams);
}