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.acuityph.commons.util.ClassUtils.java

/**
 * Checks if is property getter./*from w w w  . jav a2s . c  o  m*/
 *
 * @param method
 *        the method
 * @return true, if is property getter
 */
public static boolean isPropertyGetter(final Method method) {
    Assert.notNull(method, "method is null!");
    final int mod = method.getModifiers();
    final boolean expectsNoParameters = method.getParameterTypes().length == 0;
    final boolean returnsSomething = !void.class.equals(method.getReturnType());
    return !isPrivate(mod) && !isStatic(mod) && expectsNoParameters && returnsSomething
            && isPropertyGetterName(method.getName());
}

From source file:org.caratarse.auth.model.util.BeanUtils.java

/**
 * Copy the not-null property values of the given source bean into the given target bean.
 * <p>/*  w  w  w  .  j a  v  a 2  s.c  o m*/
 * Note: The source and target classes do not have to match or even be derived from each other,
 * as long as the properties match. Any bean properties that the source bean exposes but the
 * target bean does not will silently be ignored.
 *
 * @param source the source bean
 * @param target the target bean
 * @param editable the class (or interface) to restrict property setting to
 * @param ignoreProperties array of property names to ignore
 * @throws BeansException if the copying failed
 * @see BeanWrapper
 */
private static void copyNotNullProperties(Object source, Object target, Class<?> editable,
        String... ignoreProperties) throws BeansException {

    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");

    Class<?> actualEditable = target.getClass();
    if (editable != null) {
        if (!editable.isInstance(target)) {
            throw new IllegalArgumentException("Target class [" + target.getClass().getName()
                    + "] not assignable to Editable class [" + editable.getName() + "]");
        }
        actualEditable = editable;
    }
    PropertyDescriptor[] targetPds = org.springframework.beans.BeanUtils.getPropertyDescriptors(actualEditable);
    List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

    for (PropertyDescriptor targetPd : targetPds) {
        Method writeMethod = targetPd.getWriteMethod();
        if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
            PropertyDescriptor sourcePd = org.springframework.beans.BeanUtils
                    .getPropertyDescriptor(source.getClass(), targetPd.getName());
            if (sourcePd != null) {
                Method readMethod = sourcePd.getReadMethod();
                if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0],
                        readMethod.getReturnType())) {
                    try {
                        if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                            readMethod.setAccessible(true);
                        }
                        Object value = readMethod.invoke(source);
                        if (value == null) {
                            continue;
                        }
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, value);
                    } catch (Throwable ex) {
                        throw new FatalBeanException(
                                "Could not copy property '" + targetPd.getName() + "' from source to target",
                                ex);
                    }
                }
            }
        }
    }
}

From source file:Utils.java

/**
 * Returns true is the given method is a JMX attribute getter method
 *///from  ww  w. j a v  a2 s  .  c  o m
public static boolean isAttributeGetter(Method m) {
    if (m == null)
        return false;

    String name = m.getName();
    Class retType = m.getReturnType();
    Class[] params = m.getParameterTypes();
    if (retType != Void.TYPE && params.length == 0) {
        if (name.startsWith("get") && name.length() > 3)
            return true;
        else if (name.startsWith("is") && name.length() > 2 && retType == Boolean.TYPE)
            return true;
    }
    return false;
}

From source file:org.tinymediamanager.scraper.entities.MediaGenres.java

/**
 * get all available Languages. Here we use reflection to get rid of the dependency to the tmm core. If org.tinymediamanager.core.Utils is not in
 * our classpath, we only use en as available language
 * //from   w ww.j  a  va2 s  . c o  m
 * @return all available languages
 */
@SuppressWarnings("unchecked")
private static List<Locale> getLanguages() {
    try {
        Class<?> clazz = Class.forName("org.tinymediamanager.core.Utils");
        Object obj = clazz.newInstance();

        Method method = clazz.getDeclaredMethod("getLanguages");
        if (method.getReturnType() == List.class) {
            return (List<Locale>) method.invoke(obj);
        }
    } catch (Exception ignored) {
    }
    return Arrays.asList(new Locale("en", "US"));
}

From source file:com.haulmont.cuba.core.config.ConfigUtil.java

/**
 * Get the type of a method. If the method has a non-void return
 * type, that type is returned. Otherwise if the method has at least
 * one parameter, the type of the first parameter is returned.
 *
 * @param method The method./*from ww  w  .j  ava  2  s .com*/
 * @return The method type, or else {@link Void#TYPE}.
 */
public static Class<?> getMethodType(Method method) {
    Class<?> methodType = method.getReturnType();
    if (Void.TYPE.equals(methodType)) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length > 0) {
            methodType = parameterTypes[0];
        }
    }
    return methodType;
}

From source file:ch.digitalfondue.npjt.QueryType.java

private static JdbcAction actionFromContext(Method method, String template) {

    if (method.getReturnType().isAssignableFrom(AffectedRowCountAndKey.class)) {
        return JdbcAction.INSERT_W_AUTO_GENERATED_KEY;
    } else {/*from ww w  .j a v a  2s. c o m*/
        return actionFromTemplate(template);
    }
}

From source file:Main.java

public static boolean hasGetterSignature(Method m) {
    // First: static methods can't be getters
    if (Modifier.isStatic(m.getModifiers())) {
        return false;
    }//  w  w  w.ja v  a  2  s .com
    // Must take no args
    Class<?>[] pts = m.getParameterTypes();
    if (pts != null && pts.length != 0) {
        return false;
    }
    // Can't be a void method
    if (Void.TYPE == m.getReturnType()) {
        return false;
    }
    // Otherwise looks ok:
    return true;
}

From source file:com.curl.orb.servlet.InstanceManagementUtil.java

public static boolean isGetter(Method method) {
    return (method.getName().startsWith("get") && Character.isUpperCase(method.getName().charAt(3))
            && method.getParameterTypes().length == 0 && method.getReturnType() != void.class);
}

From source file:com.curl.orb.servlet.InstanceManagementUtil.java

public static boolean isSetter(Method method) {
    return (method.getName().startsWith("set") && Character.isUpperCase(method.getName().charAt(3))
            && method.getParameterTypes().length == 1 && method.getReturnType() == void.class);
}

From source file:ar.com.zauber.commons.spring.web.CommandURLParameterGenerator.java

/**
 * @see #getURLParameter(Class, Set)//from   ww w .  j a  v a  2 s. c o  m
 */
public static String getURLParameter(final Class<?> cmdClass, final Map<Class<?>, Object> values) {
    final StringBuilder sb = new StringBuilder("?");
    final Pattern getter = Pattern.compile("^get(.*)$");
    boolean first = true;

    // look for getters
    for (final Method method : cmdClass.getMethods()) {
        final Matcher matcher = getter.matcher(method.getName());
        if (matcher.lookingAt() && matcher.groupCount() == 1 && method.getParameterTypes().length == 0
                && values.containsKey(method.getReturnType())) {
            try {
                cmdClass.getMethod("set" + matcher.group(1), new Class[] { method.getReturnType() });
                if (!first) {
                    sb.append("&");
                } else {
                    first = false;
                }

                sb.append(URLEncoder.encode(matcher.group(1).toLowerCase(), CHARSET));
                sb.append("=");
                sb.append(URLEncoder.encode(values.get(method.getReturnType()).toString(), CHARSET));
            } catch (Exception e) {
                // skip
            }
        }
    }

    return sb.toString();
}