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:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties of a JComponent. Array properties are not included.
 * <P>//  w  ww .j av  a 2s  .  c o m
 * Implementation note: The returned value is a HashMap. This is subject to change, so callers should code against the interface Map.
 * 
 * @param component
 *            the component whose proerties are to be determined
 * @return the class and value of the properties
 */
public static Map<Object, Object> getProperties(JComponent component) {
    Map<Object, Object> retVal = new HashMap<>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    Object value = null;
    for (Method method : methods) {
        if (method.getName().matches("^(is|get).*") && //$NON-NLS-1$
                method.getParameterTypes().length == 0) {
            try {
                Class<?> returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[") && //$NON-NLS-1$
                        !setExclude.contains(method.getName())) {
                    String key = method.getName();
                    value = method.invoke(component);
                    if (value != null && !(value instanceof Component)) {
                        retVal.put(key, value);
                    }
                }
                // ignore exceptions that arise if the property could not be accessed
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                //no catch
            }
        }
    }
    return retVal;
}

From source file:jp.terasoluna.fw.util.GenericPropertyUtil.java

/**
 * <code>JavaBean</code>? <code>Generics</code>?????
 * <p>//w ww . j  a  v a 2  s.c  o  m
 * <h5>?</h5>
 * 
 * <pre>
 * <code>
 * public class Bean {
 *     private Map&lt;String, Boolean&gt; map;
 *     public Map&lt;String, Boolean&gt; getMap() {
 *         return this.map;
 *     }
 * }
 * </code>
 * </pre>
 * 
 * ???<code>Bean</code>?????????? String.class????
 * 
 * <pre>
 * <code>
 * Bean bean = new Bean();
 * Class keyType =
 *     GenericCollectionUtil.resolveType(
 *         bean, "map", Map.class, 0);
 * </code>
 * </pre>
 *
 * @param bean <code>JavaBean</code>
 * @param name <code>Generics</code>????
 * @param genericClass <code>Generics</code>? ???
 * @param index ??
 * @return <code>Generics</code>??? ???????<code>Object</code>???
 * @throws IllegalArgumentException <code>bean</code>? <code>null</code>??<code>name</code>? <code>null</code>
 *             ???? <code>genericClass</code>?<code>null</code>?? <code>index</code>?<code>0</code>
 *             ???????? ???? <code>JavaBean</code>?? ??????????
 * @throws IllegalStateException ?<code>WildCardType</code>???
 */
public static Class<?> resolveType(Object bean, String name, Class<?> genericClass, int index)
        throws IllegalArgumentException, IllegalStateException {
    if (bean == null) {
        throw new IllegalArgumentException("Argument 'bean' (" + Object.class.getName() + " is null");
    }
    if (StringUtils.isBlank(name)) {
        throw new IllegalArgumentException("Argument 'name' (" + String.class.getName() + " is empty");
    }

    Method method = getMethod(bean, name);
    return resolveType(genericClass, method.getReturnType(), method.getGenericReturnType(), index);
}

From source file:com.mobius.software.mqtt.parser.test.StaticData.java

public static boolean isGetter(Method method) {
    if (!method.getName().startsWith("get"))
        return false;
    if (method.getParameterTypes().length != 0)
        return false;
    if (void.class.equals(method.getReturnType()))
        return false;
    return true;/*from   w ww . ja va 2s. co  m*/
}

From source file:com.github.cherimojava.data.mongo.entity.EntityUtils.java

/**
 * returns true if this getter methods return type is either an entity or a list of entities. Otherwise false
 * /*from   w  w w.ja v  a 2 s. c o m*/
 * @param getter
 * @return true if return type is entity or list of entities
 */
public static boolean isValidReferenceClass(Method getter) {
    return (Entity.class.isAssignableFrom(getter.getReturnType())
            || (Collection.class.isAssignableFrom(getter.getReturnType()) && Entity.class.isAssignableFrom(
                    (Class) ((ParameterizedType) getter.getGenericReturnType()).getActualTypeArguments()[0])));
}

From source file:com.robertsmieja.test.utils.junit.GettersAndSettersUtils.java

static <T> void ensureFieldCanHandleNullValues(T value, Method getter, Method setter)
        throws IllegalAccessException, InvocationTargetException {
    Object newFieldValue;// w w w  .j a  v  a2s  .  c  o m
    if (!getter.getReturnType().isPrimitive()) {
        setter.invoke(value, (Object) null);
        newFieldValue = getter.invoke(value);
        Assertions.assertNull(newFieldValue);
    }
}

From source file:com.robonobo.common.util.BeanPropertyAccessor.java

/**
 * Attempts to copy properties from the object from to object to
 * @param from/*from w  ww .  j  ava 2  s.  com*/
 * @param to
 */
public static void copy(Object from, Object to) throws Exception {
    BeanPropertyAccessor fromAccessor = new BeanPropertyAccessor(from);
    BeanPropertyAccessor toAccessor = new BeanPropertyAccessor(to);
    Iterator i = fromAccessor.getPropertyNames().iterator();
    while (i.hasNext()) {
        String property = (String) i.next();
        Method m = fromAccessor.getGetterMethod(property);
        if (fromAccessor.getProperty(property) != null) {
            toAccessor.setProperty(property, m.getReturnType(), fromAccessor.getProperty(property));
        }
    }

}

From source file:mangotiger.poker.channel.EventChannelImpl.java

static boolean methodMatchesEvent(final Method method, final Class<?> event) {
    return METHOD_NAME.equals(method.getName()) && Modifier.isPublic(method.getModifiers())
            && Void.TYPE.equals(method.getReturnType()) && method.getParameterTypes().length == 1
            && method.getParameterTypes()[0].isAssignableFrom(event);
}

From source file:net.sf.morph.reflect.support.MethodHolder.java

/**
 * Generate a String description of a Method.
 * @param method/*from ww w . j  a v a  2 s .  co  m*/
 * @return String
 */
protected static String methodToString(Method method) {
    StringBuffer buffer = new StringBuffer();
    buffer.append("<");
    buffer.append(method.getReturnType() == null ? "void" : method.getReturnType().getName());
    buffer.append("> ");
    buffer.append(method.getName());
    buffer.append("(");
    Class[] parameterTypes = method.getParameterTypes();
    if (parameterTypes != null) {
        for (int i = 0; i < parameterTypes.length; i++) {
            buffer.append(parameterTypes[i].getName());
            if (i != parameterTypes.length - 1) {
                buffer.append(",");
            }
        }
    }
    buffer.append(")");
    return buffer.toString();
}

From source file:cn.wanghaomiao.seimi.core.SeimiBeanResolver.java

private static Object defaultCastToTargetValue(Class target, Field field, List<Object> xpathRes) {
    Method getter = ReflectionUtils.findMethod(target, "get" + upperFirst(field.getName()));
    if (getter != null) {
        if (List.class.equals(getter.getReturnType())) {
            Class[] componentClazzs = GenericUtils.getActualClass(getter.getGenericReturnType());
            if (componentClazzs != null && componentClazzs.length > 0) {
                Class componentClass = componentClazzs[0];
                if (String.class.isAssignableFrom(componentClass)) {
                    List<String> resTmp = new LinkedList<>();
                    for (Object obj : xpathRes) {
                        if (obj instanceof Element) {
                            resTmp.add(((Element) obj).html());
                        } else {
                            resTmp.add(obj.toString());
                        }//w  ww. ja  va  2  s. c o m
                    }
                    return resTmp;
                } else if (Element.class.isAssignableFrom(componentClass)) {
                    return xpathRes;
                } else if (GenericUtils.isNumber(componentClass)) {
                    List resTmp = new LinkedList();
                    for (Object obj : xpathRes) {
                        resTmp.add(GenericUtils.castToNumber(componentClass, obj.toString()));
                    }
                    return resTmp;
                } else {
                    throw new SeimiBeanResolveException("not support field type");
                }
            }
        } else if (!Collection.class.isAssignableFrom(getter.getReturnType())
                && getter.getReturnType().isArray()) {
            Class componentClass = getter.getReturnType().getComponentType();
            if (String.class.isAssignableFrom(componentClass)) {
                List<String> resTmp = new LinkedList<>();
                for (Object obj : xpathRes) {
                    if (obj instanceof Element) {
                        resTmp.add(((Element) obj).html());
                    } else {
                        resTmp.add(obj.toString());
                    }
                }
                return resTmp;
            } else if (Element.class.isAssignableFrom(componentClass)) {
                return xpathRes;
            } else if (GenericUtils.isNumber(componentClass)) {
                List resTmp = new LinkedList();
                for (Object obj : xpathRes) {
                    resTmp.add(GenericUtils.castToNumber(componentClass, obj.toString()));
                }
                return resTmp;
            } else {
                throw new SeimiBeanResolveException("not support field type");
            }
        } else if (!Collection.class.isAssignableFrom(getter.getReturnType())
                && GenericUtils.isNumber(field.getType())) {
            return GenericUtils.castToNumber(field.getType(), StringUtils.join(xpathRes, ""));
        } else if (!Collection.class.isAssignableFrom(getter.getReturnType())
                && String.class.isAssignableFrom(field.getType())) {
            return StringUtils.join(xpathRes, "");
        }
    }
    return null;
}

From source file:org.carewebframework.shell.property.PropertyUtil.java

/**
 * Returns the requested method from an object instance.
 * /*from w  ww . j  ava 2s  .  c om*/
 * @param methodName Name of the setter method.
 * @param instance Object instance to search.
 * @param valueClass The desired property return type (null if don't care).
 * @param setter If true, search for setter method signature. If false, getter method signature.
 * @return The requested method.
 * @throws NoSuchMethodException If method was not found.
 */
private static Method findMethod(String methodName, Object instance, Class<?> valueClass, boolean setter)
        throws NoSuchMethodException {
    if (methodName == null) {
        return null;
    }

    int paramCount = setter ? 1 : 0;

    for (Method method : instance.getClass().getMethods()) {
        if (method.getName().equals(methodName) && method.getParameterTypes().length == paramCount) {
            Class<?> targetClass = setter ? method.getParameterTypes()[0] : method.getReturnType();

            if (valueClass == null || TypeUtils.isAssignable(targetClass, valueClass)) {
                return method;
            }
        }
    }

    throw new NoSuchMethodException("Compatible method not found: " + methodName);

}