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:airlift.util.AirliftUtil.java

/**
 * Gets the attribute type./*from ww  w  . ja  v  a2s.  c  o m*/
 *
 * @param _object the _object
 * @param _attributeName the _attribute name
 * @return the attribute type
 */
public static String getAttributeType(Object _object, String _attributeName) {
    try {
        String getter = "get" + upperTheFirstCharacter(_attributeName);
        java.lang.reflect.Method method = _object.getClass().getMethod(getter);

        return method.getReturnType().getName();
    } catch (Throwable t) {

        throw new RuntimeException(t);
    }
}

From source file:com.swingtech.commons.util.ClassUtil.java

public static String getMethodDeclarationString(final Method method) {
    final StringBuffer retStringBuff = new StringBuffer();

    retStringBuff.append(ClassUtil.getClassNameFromFullPath(method.getReturnType()));
    retStringBuff.append(" ");
    retStringBuff.append(ClassUtil.getClassNameFromFullPath(method.getDeclaringClass()));
    retStringBuff.append(".");
    retStringBuff.append(method.getName());
    retStringBuff.append("(");

    if (!Utility.isNullOrEmpty(method.getParameterTypes())) {
        Class c = null;// w  ww .  j  a v  a  2 s. c o  m
        for (int index = 0; index < method.getParameterTypes().length; index++) {
            c = method.getParameterTypes()[0];
            if (index > 1) {
                retStringBuff.append(", ");
            }
            retStringBuff.append(ClassUtil.getClassNameFromFullPath(c));
            retStringBuff.append(" arg" + index);
        }
    }

    retStringBuff.append(")");

    return retStringBuff.toString();
}

From source file:com.snaplogic.snaps.firstdata.Create.java

static boolean isSetter(Method method) {
    return Modifier.isPublic(method.getModifiers()) && method.getReturnType().equals(void.class)
            && method.getParameterTypes().length == 1 && method.getName().matches(REGEX_SET);
}

From source file:com.doitnext.jsonschema.generator.SchemaGen.java

private static boolean handleProperties(Class<?> classz, StringBuilder sb, Map<String, String> declarations,
        String uriPrefix) {/*from  www  . ja v  a  2s.  co  m*/
    boolean result = false;
    String prepend = "";
    Set<String> processedFields = new HashSet<String>();
    sb.append("{");
    for (Field field : classz.getFields()) {
        JsonSchemaProperty propertyDecl = field.getAnnotation(JsonSchemaProperty.class);
        if (propertyDecl != null) {
            Class<?> propClassz = field.getType();
            StringBuilder sb2 = new StringBuilder();
            boolean inline = true;
            sb2.append("{");
            if (!handleSimpleType(propClassz, sb2, propertyDecl, declarations, uriPrefix)) {
                if (!handleArrayType(propClassz, sb2, propertyDecl, declarations, uriPrefix)) {
                    inline = false;
                }
            }
            sb2.append("}");
            if (inline) {
                sb.append(prepend);
                sb.append("\"");
                sb.append(propertyDecl.name());
                sb.append("\":");
                sb.append(sb2.toString());
                prepend = ", ";
            } else {
                String id = null;
                JsonSchemaClass jsc = propClassz.getAnnotation(JsonSchemaClass.class);
                if (jsc != null)
                    id = jsc.id();
                else
                    id = propClassz.getName();
                declarations.put(id, sb2.toString());

                sb.append(prepend);
                sb.append("\"");
                sb.append(propertyDecl.name());
                sb.append("\":{\"$ref\":\"");
                if (!StringUtils.isEmpty(uriPrefix))
                    sb.append(uriPrefix);
                sb.append(id);
                sb.append("\"}");
                prepend = ", ";
            }
            processedFields.add(propertyDecl.name());
        }
    }
    for (Method method : classz.getMethods()) {
        JsonSchemaProperty propertyDecl = method.getAnnotation(JsonSchemaProperty.class);
        if (propertyDecl != null && !processedFields.contains(propertyDecl.name())) {
            Class<?> propClassz = method.getReturnType();
            StringBuilder sb2 = new StringBuilder();
            boolean inline = true;
            sb2.append("{");
            if (!handleSimpleType(propClassz, sb2, propertyDecl, declarations, uriPrefix)) {
                if (!handleArrayType(propClassz, sb2, propertyDecl, declarations, uriPrefix)) {
                    inline = false;
                }
            }
            sb2.append("}");
            if (inline) {
                sb.append(prepend);
                sb.append("\"");
                sb.append(propertyDecl.name());
                sb.append("\":");
                sb.append(sb2.toString());
                prepend = ", ";
            } else {
                String id = null;
                JsonSchemaClass jsc = propClassz.getAnnotation(JsonSchemaClass.class);
                if (jsc != null)
                    id = jsc.id();
                else
                    id = propClassz.getName();
                declarations.put(id, sb2.toString());

                sb.append(prepend);
                sb.append("\"");
                sb.append(propertyDecl.name());
                sb.append("\":{\"$ref\":\"");
                if (!StringUtils.isEmpty(uriPrefix))
                    sb.append(uriPrefix);
                sb.append(id);
                sb.append("\"}");
                prepend = ", ";
            }
            processedFields.add(propertyDecl.name());
        }
    }
    sb.append("}");
    return result;
}

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

private final static List findMutatorMethods(Class _class) {
    List methodList = new ArrayList();

    Enhancer.getMethods(_class, null, methodList);

    Predicate mutatorPredicate = new Predicate() {
        public boolean evaluate(Object _object) {
            Method method = (Method) _object;

            boolean startsWithSet = (method.getName().startsWith("set") == true);
            boolean returnTypeIsVoid = ("void".equals(method.getReturnType().getName()) == true);
            boolean parameterTypeCountIsOne = (method.getParameterTypes().length == 1);
            boolean isPublic = (Modifier.isPublic(method.getModifiers()) == true);

            return startsWithSet && returnTypeIsVoid && parameterTypeCountIsOne && isPublic;
        }/*  w  w  w.  ja v  a  2s.c om*/
    };

    CollectionUtils.filter(methodList, mutatorPredicate);

    return methodList;
}

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

private final static List findOtherPublicMethods(Class _class) {
    List methodList = new ArrayList();

    Enhancer.getMethods(_class, null, methodList);

    Predicate mutatorPredicate = new Predicate() {
        public boolean evaluate(Object _object) {
            Method method = (Method) _object;

            boolean startsWithSet = (method.getName().startsWith("set") == true);
            boolean returnTypeIsVoid = ("void".equals(method.getReturnType().getName()) == true);
            boolean parameterTypeCountIsOne = (method.getParameterTypes().length == 1);
            boolean isPublic = (Modifier.isPublic(method.getModifiers()) == true);

            return ((startsWithSet && returnTypeIsVoid && parameterTypeCountIsOne) == false) && isPublic;
        }/*from  w  w w  .j  ava2  s . c  om*/
    };

    CollectionUtils.filter(methodList, mutatorPredicate);

    return methodList;
}

From source file:com.p5solutions.mapping.EntityMapper.java

/**
 * Does the class type have any of the following annotations {@link MapClass} or {@link MapClasses}
 * //from w  ww  .ja v  a 2s.  c o m
 * @param clazz
 *          the clazz to search against
 * 
 * @return <code>true</code> , if successfully found annotations otherwise <code>false</code>
 */
@SuppressWarnings("unchecked")
protected static final boolean mappable(Method method) {

    // get the return value of the method.
    Class<?> clazz = method.getReturnType();

    // if the method has @MapExpand or the return type of the Method has
    // @MapClass or @MapClasses, then the object is expandable or walkable
    return ReflectionUtility.hasAnyAnnotation(method, MapExpand.class)
            || ReflectionUtility.hasAnyAnnotation(clazz, MapClass.class, MapClasses.class);
}

From source file:com.google.gwtjsonrpc.server.JsonServlet.java

private static Map<String, MethodHandle> methods(final RemoteJsonService impl) {
    final Class<? extends RemoteJsonService> d = findInterface(impl.getClass());
    if (d == null) {
        return Collections.<String, MethodHandle>emptyMap();
    }/*www  .  j  a va 2s .c om*/

    final Map<String, MethodHandle> r = new HashMap<String, MethodHandle>();
    for (final Method m : d.getMethods()) {
        if (!Modifier.isPublic(m.getModifiers())) {
            continue;
        }

        if (m.getReturnType() != Void.TYPE) {
            continue;
        }

        final Class<?>[] params = m.getParameterTypes();
        if (params.length < 1) {
            continue;
        }

        if (!params[params.length - 1].isAssignableFrom(AsyncCallback.class)) {
            continue;
        }

        final MethodHandle h = new MethodHandle(impl, m);
        r.put(h.getName(), h);
    }
    return Collections.unmodifiableMap(r);
}

From source file:io.stallion.reflection.PropertyUtils.java

private static Class getPropertyType(Object target, String propertyName) {
    String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    Method[] methods = target.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if ((method.getName().equals(getterName) || method.getName().equals(getterIsName))
                && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) {
            return method.getReturnType();
        }//w  w w.j  ava 2s  .  c  o  m
    }
    throw new PropertyException(
            "no property '" + propertyName + "' in class '" + target.getClass().getName() + "'");
}

From source file:io.stallion.reflection.PropertyUtils.java

public static Method getGetter(Object target, String propertyName) {
    String cacheKey = "getGetter" + "|" + target.getClass().getCanonicalName() + "|" + propertyName;
    if (target instanceof BaseJavascriptModel) {
        cacheKey = "getGetter" + "|jsModel" + ((BaseJavascriptModel) target).getBucketName() + "|"
                + propertyName;/*from  ww w.j av a2  s .  co  m*/
    }
    if (lookupCache.containsKey(cacheKey)) {
        return (Method) lookupCache.get(cacheKey);
    }
    String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    String getterIsName = "is" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
    Method[] methods = target.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        if ((method.getName().equals(getterName) || method.getName().equals(getterIsName))
                && !method.getReturnType().equals(void.class) && method.getParameterTypes().length == 0) {
            lookupCache.put(cacheKey, method);
            return method;
        }
    }
    lookupCache.put(cacheKey, null);
    throw new PropertyException(
            "no readable property '" + propertyName + "' in class '" + target.getClass().getName() + "'");
}