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

/**
 * Use reflection to access a URL[] getURLs or URL[] getClasspath method so
 * that non-URLClassLoader class loaders, or class loaders that override
 * getURLs to return null or empty, can provide the true classpath info.
 * /*from  w  w  w .ja  v  a  2  s .c  o m*/
 * @param cl
 * @return the urls
 */
public static URL[] getClassLoaderURLs(ClassLoader cl) {
    URL[] urls = {};
    try {
        Class returnType = urls.getClass();
        Class[] parameterTypes = {};
        Class clClass = cl.getClass();
        Method getURLs = clClass.getMethod("getURLs", parameterTypes);
        if (returnType.isAssignableFrom(getURLs.getReturnType())) {
            Object[] args = {};
            urls = (URL[]) getURLs.invoke(cl, args);
        }
        if (urls == null || urls.length == 0) {
            Method getCp = clClass.getMethod("getClasspath", parameterTypes);
            if (returnType.isAssignableFrom(getCp.getReturnType())) {
                Object[] args = {};
                urls = (URL[]) getCp.invoke(cl, args);
            }
        }
    } catch (Exception ignore) {
    }
    return urls;
}

From source file:com.haulmont.cuba.core.config.type.TypeFactory.java

/**
 * Get a TypeFactory instance appropriate for the return type of the
 * specified configuration interface method.
 *
 * @param configInterface The configuration interface.
 * @param method          The method./*from w w w  .j av  a 2  s .c o  m*/
 * @return An appropriate TypeFactory.
 * @throws IllegalArgumentException If the type is not supported.
 */
public static TypeFactory getInstance(Class<?> configInterface, Method method) {
    Class<?> returnType = method.getReturnType();
    if (returnType.isPrimitive()) {
        returnType = ClassUtils.primitiveToWrapper(returnType);
    }
    Factory factory = ConfigUtil.getAnnotation(configInterface, method, Factory.class, true);
    if (factory != null) {
        try {
            if ("".equals(factory.method())) {
                return factory.factory().newInstance();
            } else {
                String methodName = factory.method();
                Method factoryMethod = returnType.getMethod(methodName, String.class);
                if (!isAcceptableMethod(returnType, factoryMethod)) {
                    throw new IllegalArgumentException("Invalid factory method: " + factoryMethod);
                }
                return new StaticTypeFactory(factoryMethod);
            }
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("Unable to instantiate an type factory", e);
        }
    } else {
        if (Entity.class.isAssignableFrom(returnType)) {
            return AppBeans.get(ENTITY_FACTORY_BEAN_NAME, TypeFactory.class);
        } else {
            if (EnumClass.class.isAssignableFrom(returnType)) {
                EnumStore mode = ConfigUtil.getAnnotation(configInterface, method, EnumStore.class, true);
                if (mode != null && EnumStoreMode.ID == mode.value()) {
                    @SuppressWarnings("unchecked")
                    Class<EnumClass> enumeration = (Class<EnumClass>) returnType;
                    Class<?> idType = ConfigUtil.getEnumIdType(enumeration);
                    TypeFactory idFactory = getInferred(idType);
                    try {
                        Method fromIdMethod = returnType.getMethod("fromId", idType);
                        if (!isAcceptableMethod(returnType, fromIdMethod) || idFactory == null) {
                            throw new IllegalArgumentException(
                                    "Cannot use method as factory method: " + method);
                        }
                        return new EnumClassFactory(idFactory, fromIdMethod);
                    } catch (NoSuchMethodException e) {
                        throw new IllegalArgumentException(
                                "fromId method is not found for " + enumeration.getName());
                    }
                }
            }
            TypeFactory factoryT = getInferred(returnType);
            if (factoryT == null) {
                throw new IllegalArgumentException("Unsupported return type for " + method);
            }
            return factoryT;
        }
    }
}

From source file:de.micromata.genome.tpsb.GroovyExceptionInterceptor.java

protected static String methodToString(Method m) {
    StringBuilder sb = new StringBuilder();
    String returnType = m.getReturnType().getName();
    // returnType = "def";
    sb.append(returnType).append(" ").append(m.getName()).append("(");
    int pos = 0;/*from   w w  w . j av  a2s  .  co m*/
    for (Class<?> cls : m.getParameterTypes()) {
        if (pos > 0) {
            sb.append(", ");
        }
        sb.append(cls.getName()).append(" arg" + pos);
        ++pos;
    }
    sb.append(")");
    return sb.toString();
}

From source file:Main.java

public static boolean isAccessor(Method method, String findValue) {
    boolean accessor = false;
    final Class<?>[] parameterTypes = method.getParameterTypes();
    final String methodName = method.getName();
    if (parameterTypes.length == 0 && method.getReturnType() != null
            && (methodName.startsWith("get") || methodName.startsWith("is"))
            && methodName.toLowerCase().indexOf(findValue.toLowerCase()) > -1) {
        accessor = true;/*from  w w  w .ja  v  a  2s.com*/
    }
    return accessor;
}

From source file:com.yahoo.sql4d.sql4ddriver.BaseMapper.java

/**
 * More granular(sets the property of a bean based on a json key value
 * pair)./*from  w w w  . j  a va2  s .  c o m*/
 *
 * @param bean
 * @param key
 * @param value
 * @throws NoSuchMethodException
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws InvocationTargetException
 */
public static void applyKVToBean(Object bean, String key, Object value) throws NoSuchMethodException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Method getterMethod = bean.getClass().getMethod(Util.getterMethodName(key));
    Method setterMethod = bean.getClass().getMethod(Util.setterMethodName(key), getterMethod.getReturnType());
    setterMethod.invoke(bean, value);
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties
 * of a JComponent.  Array properties are not included.
 * <P>/*  www. ja v  a2 s .  co  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<Object, Object>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    Object value = null;
    for (Method method : methods) {
        if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
            try {
                Class returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[")
                        && !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 ex) {
            } catch (IllegalArgumentException ex) {
            } catch (InvocationTargetException ex) {
            }
        }
    }
    return retVal;
}

From source file:com.npower.dm.util.DMUtil.java

/**
 * Use reflection to access a URL[] getURLs or ULR[] getAllURLs method so that
 * non-URLClassLoader class loaders, or class loaders that override getURLs to
 * return null or empty, can provide the true classpath info.
 *///from w  w w.j av a2  s  .  c  o  m
public static URL[] getClassLoaderURLs(ClassLoader cl) {
    URL[] urls = {};
    try {
        Class<?> returnType = urls.getClass();
        Class<?>[] parameterTypes = {};
        Method getURLs = cl.getClass().getMethod("getURLs", parameterTypes);
        if (returnType.isAssignableFrom(getURLs.getReturnType())) {
            Object[] args = {};
            urls = (URL[]) getURLs.invoke(cl, args);
        }
    } catch (Exception ignore) {
    }
    return urls;
}

From source file:Main.java

public static Method getGetter(Object bean, String property) {
    Map<String, Method> cache = GETTER_CACHE.get(bean.getClass());
    if (cache == null) {
        cache = new ConcurrentHashMap<String, Method>();
        for (Method method : bean.getClass().getMethods()) {
            if (Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())
                    && !void.class.equals(method.getReturnType()) && method.getParameterTypes().length == 0) {
                String name = method.getName();
                if (name.length() > 3 && name.startsWith("get")) {
                    cache.put(name.substring(3, 4).toLowerCase() + name.substring(4), method);
                } else if (name.length() > 2 && name.startsWith("is")) {
                    cache.put(name.substring(2, 3).toLowerCase() + name.substring(3), method);
                }/*from   w w  w.  j a v a2  s. c o  m*/
            }
        }
        Map<String, Method> old = GETTER_CACHE.putIfAbsent(bean.getClass(), cache);
        if (old != null) {
            cache = old;
        }
    }
    return cache.get(property);
}

From source file:org.psnively.scala.beans.ScalaBeanInfo.java

public static boolean isScalaSetter(Method method) {
    return method.getParameterTypes().length == 1 && method.getReturnType().equals(Void.TYPE)
            && method.getName().endsWith(SCALA_SETTER_SUFFIX);
}

From source file:org.psnively.scala.beans.ScalaBeanInfo.java

private static boolean isScalaGetter(Method method) {
    return method.getParameterTypes().length == 0 && !method.getReturnType().equals(Void.TYPE)
            && !(method.getName().startsWith("get") || method.getName().startsWith("is"));
}