Example usage for java.lang.reflect Method getGenericReturnType

List of usage examples for java.lang.reflect Method getGenericReturnType

Introduction

In this page you can find the example usage for java.lang.reflect Method getGenericReturnType.

Prototype

public Type getGenericReturnType() 

Source Link

Document

Returns a Type object that represents the formal return type of the method represented by this Method object.

Usage

From source file:Main.java

public static void main(String... args) {
    try {//  w  w w  . ja  v  a2s .c o  m
        Class<?> c = Class.forName("Main");
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            Type gpType = m.getGenericReturnType();
            System.out.println(gpType);

        }
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Deet.java

public static void main(String... args) {
    if (args.length != 4) {
        err.format("Usage: java Deet <classname> <langauge> <country> <variant>%n");
        return;/*from  w  ww . j  a va2 s  .  c  o m*/
    }

    try {
        Class<?> c = Class.forName(args[0]);
        Object t = c.newInstance();

        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            String mname = m.getName();
            if (!mname.startsWith("test") || (m.getGenericReturnType() != boolean.class)) {
                continue;
            }
            Type[] pType = m.getGenericParameterTypes();
            if ((pType.length != 1) || Locale.class.isAssignableFrom(pType[0].getClass())) {
                continue;
            }

            out.format("invoking %s()%n", mname);
            try {
                m.setAccessible(true);
                Object o = m.invoke(t, new Locale(args[1], args[2], args[3]));
                out.format("%s() returned %b%n", mname, (Boolean) o);

                // Handle any exceptions thrown by method to be invoked.
            } catch (InvocationTargetException x) {
                Throwable cause = x.getCause();
                err.format("invocation of %s failed: %s%n", mname, cause.getMessage());
            }
        }

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    } catch (InstantiationException x) {
        x.printStackTrace();
    } catch (IllegalAccessException x) {
        x.printStackTrace();
    }
}

From source file:MethodSpy.java

public static void main(String... args) {
    try {//w  ww  . j  a v a 2 s . c  om
        Class<?> c = Class.forName(args[0]);
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            if (!m.getName().equals(args[1])) {
                continue;
            }
            out.format("%s%n", m.toGenericString());

            out.format(fmt, "ReturnType", m.getReturnType());
            out.format(fmt, "GenericReturnType", m.getGenericReturnType());

        }

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:MethodSpy.java

public static void main(String... args) {
    try {// ww w  .  java 2 s.  com
        Class<?> c = Class.forName(args[0]);
        Method[] allMethods = c.getDeclaredMethods();
        for (Method m : allMethods) {
            if (!m.getName().equals(args[1])) {
                continue;
            }
            out.format("%s%n", m.toGenericString());

            out.format(fmt, "ReturnType", m.getReturnType());
            out.format(fmt, "GenericReturnType", m.getGenericReturnType());

            Class<?>[] pType = m.getParameterTypes();
            Type[] gpType = m.getGenericParameterTypes();
            for (int i = 0; i < pType.length; i++) {
                out.format(fmt, "ParameterType", pType[i]);
                out.format(fmt, "GenericParameterType", gpType[i]);
            }

        }

        // production code should handle these exceptions more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static Class<?> getCollectionType(Method method) {
    if (method.getGenericReturnType() instanceof ParameterizedType) {
        ParameterizedType type = (ParameterizedType) method.getGenericReturnType();
        return (Class<?>) type.getActualTypeArguments()[0];
    }/*from   w w  w .j  av  a 2s . c  o m*/
    return Object.class;
}

From source file:Main.java

public static Type getMethodType(Class<?> clazz, String methodName) {
    try {/* w  ww.j a va  2s .com*/
        Method method = clazz.getMethod(methodName);

        return method.getGenericReturnType();
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.netflix.hystrix.contrib.javanica.utils.TypeHelper.java

/**
 * Check whether return type of the given method is parametrized or not.
 *
 * @param method the method/*from w  w w .  j  a  va  2s  . c o  m*/
 * @return true - if return type is {@link ParameterizedType}, otherwise - false
 */
public static boolean isReturnTypeParametrized(Method method) {
    return method.getGenericReturnType() instanceof ParameterizedType;
}

From source file:ch.ifocusit.plantuml.utils.ClassUtils.java

public static Set<Class> getGenericTypes(Method method) {
    if (method.getGenericReturnType() instanceof ParameterizedType) {
        // manage generics
        return getGenericTypes((ParameterizedType) method.getGenericReturnType());
    }/*from  w  w  w. j a  v a 2s. c o  m*/
    return new HashSet<>();
}

From source file:me.hurel.hqlbuilder.internal.ProxyUtil.java

public static Class<?> getParameter(Method method) {
    return (Class<?>) ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[0];
}

From source file:com.liferay.apio.architect.internal.annotation.representor.processor.TypeProcessor.java

private static java.lang.reflect.Type _getGenericType(Method method) {
    ParameterizedType parameterizedType = (ParameterizedType) method.getGenericReturnType();

    return parameterizedType.getActualTypeArguments()[0];
}