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:com.google.code.ssm.aop.CacheBase.java

protected void verifyReturnTypeIsList(final Method method, final Class<?> annotationClass) {
    if (!verifyTypeIsList(method.getReturnType())) {
        throw new InvalidAnnotationException(String.format(
                "The annotation [%s] is only valid on a method that returns a [%s] or its subclass. "
                        + "[%s] does not fulfill this requirement.",
                annotationClass.getName(), List.class.getName(), method.toString()));
    }//from w w  w.jav a2 s  . c o  m
}

From source file:com.haulmont.cuba.gui.xml.DeclarativeColumnGenerator.java

protected Method findGeneratorMethod(Class cls, String methodName) {
    Method exactMethod = MethodUtils.getAccessibleMethod(cls, methodName, Entity.class);
    if (exactMethod != null) {
        return exactMethod;
    }/* www. j a  v  a2  s .co m*/

    // search through all methods
    Method[] methods = cls.getMethods();
    for (Method availableMethod : methods) {
        if (availableMethod.getName().equals(methodName)) {
            if (availableMethod.getParameterCount() == 1
                    && Component.class.isAssignableFrom(availableMethod.getReturnType())) {
                if (Entity.class.isAssignableFrom(availableMethod.getParameterTypes()[0])) {
                    // get accessible version of method
                    return MethodUtils.getAccessibleMethod(availableMethod);
                }
            }
        }
    }
    return null;
}

From source file:com.sf.ddao.crud.ops.CheckIfDirtyOperation.java

public boolean execute(Context context) throws Exception {
    final MethodCallCtx callCtx = CtxHelper.get(context, MethodCallCtx.class);
    final Object[] args = callCtx.getArgs();
    final Method method = callCtx.getMethod();
    for (Object arg : args) {
        if (arg instanceof DirtyableBean) {
            DirtyableBean dirtyableBean = (DirtyableBean) arg;
            if (!dirtyableBean.beanIsDirty()) {
                if (method.getReturnType() == Integer.TYPE || method.getReturnType() == Integer.class) {
                    callCtx.setLastReturn(0);
                }//w  ww  .ja  v a 2  s  . c  o m
                log.debug("Bean '{}' is not dirty and doesn't have to be updated" + arg);
                return PROCESSING_COMPLETE;
            }
        }
    }
    return CONTINUE_PROCESSING;
}

From source file:com.taobao.adfs.distributed.rpc.RPC.java

/** Expert: Make multiple, parallel calls to a set of servers. */
public static Object[] call(Method method, Object[][] params, InetSocketAddress[] addrs, Configuration conf)
        throws IOException {

    Invocation[] invocations = new Invocation[params.length];
    for (int i = 0; i < params.length; i++)
        invocations[i] = new Invocation(method, params[i]);
    Client client = CLIENTS.getClient(conf);
    try {// w ww.j  a  v  a 2s.  c o  m
        Writable[] wrappedValues = client.call(invocations, addrs);

        if (method.getReturnType() == Void.TYPE) {
            return null;
        }

        Object[] values = (Object[]) Array.newInstance(method.getReturnType(), wrappedValues.length);
        for (int i = 0; i < values.length; i++)
            if (wrappedValues[i] != null)
                values[i] = ((ObjectWritable) wrappedValues[i]).get();

        return values;
    } finally {
        CLIENTS.stopClient(client);
    }
}

From source file:org.easyj.orm.jpa.JPAEntityService.java

@Override
public <T> String save(T t) {
    String ret = EntityService.STATUS_SUCCESS;
    T newT;//from ww  w .  ja  v  a  2  s  .c o m

    try {
        newT = dao.save(t);

        Method getId = t.getClass().getMethod("getId", new Class[0]);

        t.getClass().getMethod("setId", getId.getReturnType()).invoke(t, getId.invoke(newT, new Object[0]));
    } catch (EntityExistsException ex) {
        ret = EntityService.STATUS_ERROR_EXISTS;
    } catch (ConstraintViolationException ex) {
        ret = EntityService.STATUS_ERROR_CONSTRAINT_VIOLATION;
    } catch (NoSuchMethodException ex) {
        //ignore
    } catch (Exception ex) {
        ret = EntityService.STATUS_ERROR;
    }

    return ret;
}

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

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    String methodName = method.getName();

    @SuppressWarnings("unchecked")
    Class returnType = method.getReturnType();

    Matcher matcher;//from w w  w . ja  va2 s .  c  o m

    if (GETTER_REG.matcher(methodName).matches())
        return invokeGetter(proxy, method);
    else if ((matcher = SETTER_REG.matcher(methodName)).matches() && returnType.equals(void.class)) {

        String key = matcher.group(2);
        Method getter = findGetter(key, returnType);
        String propName = findPropertyName(getter);

        return invokeSetter(proxy, method, propName, args[0]);
    }

    return null;
}

From source file:info.archinnov.achilles.helper.EntityIntrospector.java

public Method findSetter(Class<?> beanClass, Field field) {
    log.debug("Find setter for field {} in class {}", field.getName(), beanClass.getCanonicalName());

    String fieldName = field.getName();

    try {/*  www  . j  a  va  2  s .  c  o m*/
        String setter = this.deriveSetterName(field);
        Method setterMethod = beanClass.getMethod(setter, field.getType());

        if (!setterMethod.getReturnType().toString().equals("void")) {
            throw new AchillesBeanMappingException("The setter for field '" + fieldName + "' of type '"
                    + field.getDeclaringClass().getCanonicalName()
                    + "' does not return correct type or does not have the correct parameter");
        }

        log.trace("Derived setter method : {}", setterMethod.getName());
        return setterMethod;

    } catch (NoSuchMethodException e) {
        throw new AchillesBeanMappingException("The setter for field '" + fieldName + "' of type '"
                + field.getDeclaringClass().getCanonicalName() + "' does not exist or is incorrect");
    }
}

From source file:org.opentides.util.CrudUtil.java

/**
 * This method retrieves the object type that corresponds to the property specified.
 * This method can recurse inner classes until specified property is reached.
 * /*from  www.j a  va 2  s.c o  m*/
 * For example:
 * obj.firstName
 * obj.address.Zipcode
 * 
 * @param obj
 * @param property
 * @return
 */
public static Class<?> retrieveObjectType(Object obj, String property) {
    if (property.contains(".")) {
        // we need to recurse down to final object
        String props[] = property.split("\\.");
        try {
            Method method = obj.getClass().getMethod(NamingUtil.toGetterName(props[0]));
            Object ivalue = method.invoke(obj);
            return retrieveObjectType(ivalue, property.substring(props[0].length() + 1));
        } catch (Exception e) {
            throw new InvalidImplementationException("Failed to retrieve value for " + property, e);
        }
    } else {
        // let's get the object value directly
        try {
            Method method = obj.getClass().getMethod(NamingUtil.toGetterName(property));
            return method.getReturnType();
        } catch (Exception e) {
            throw new InvalidImplementationException("Failed to retrieve value for " + property, e);
        }
    }
}

From source file:com.netflix.dyno.contrib.ElasticConnectionPoolConfigurationPublisher.java

JSONObject createJsonFromConfig(ConnectionPoolConfiguration config) throws JSONException {
    if (config == null) {
        throw new IllegalArgumentException("Valid ConnectionPoolConfiguration instance is required");
    }/*from  w  w w .j  a  v a  2 s  .  c o m*/

    JSONObject json = new JSONObject();

    json.put("ApplicationName", applicationName);
    json.put("DynomiteClusterName", clusterName);

    Method[] methods = config.getClass().getMethods();
    for (Method method : methods) {
        if (method.getName().startsWith("get")) {
            Class<?> ret = method.getReturnType();
            if (ret.isPrimitive() || ret == java.lang.String.class) {
                try {
                    json.put(method.getName().substring(3), method.invoke(config));
                } catch (ReflectiveOperationException ex) {
                    /* forget it */
                }
            }
        }
    }

    // jar version
    Set<String> jars = new HashSet<String>() {
        {
            add("dyno-core");
            add("dyno-contrib");
            add("dyno-jedis");
            add("dyno-reddison");
        }
    };

    json.put("Versions", new JSONObject(getLibraryVersion(this.getClass(), jars)));

    return json;
}