Java Reflection Method Return getMethodReturnType(Class clazz, String name)

Here you can find the source of getMethodReturnType(Class clazz, String name)

Description

Returns a Class object that identifies the declared class as a return type for the method represented by the given String name parameter inside the invoked Class clazz parameter.

License

Open Source License

Parameter

Parameter Description
clazz the Class object whose declared methods to be checked for the wanted method name.
name the method name as String to be compared with Method#getName()

Return

the Class object representing the return type of the given method name.

Declaration

public static Class<?> getMethodReturnType(Class<?> clazz, String name) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;

public class Main {
    /**/*from   ww  w . ja  v  a  2 s. c o  m*/
     * Returns a {@code Class} object that identifies the declared class as a
     * return type for the method represented by the given {@code String name}
     * parameter inside the invoked {@code Class<?> clazz} parameter.
     * 
     * @param clazz
     *            the {@code Class} object whose declared methods to be checked
     *            for the wanted method name.
     * @param name
     *            the method name as {@code String} to be compared with
     *            {@link Method#getName()}
     * @return the {@code Class} object representing the return type of the
     *         given method name.
     * 
     * @see {@link Class#getDeclaredMethods()}
     * @see {@link Method#getReturnType()}
     */
    public static Class<?> getMethodReturnType(Class<?> clazz, String name) {
        if (clazz == null || name == null || name.isEmpty()) {
            return null;
        }

        name = name.toLowerCase();
        Class<?> returnType = null;

        for (Method method : clazz.getDeclaredMethods()) {
            if (method.getName().equals(name)) {
                returnType = method.getReturnType();
                break;
            }
        }

        return returnType;
    }
}

Related

  1. getMethodGenericReturnType(Method method, int index)
  2. getMethodGenericReturnType(Method method, int index)
  3. getMethodInvoker(final Class returnType, final String methodName)
  4. getMethodNoArgs(final Class objClass, final String methodName, final Class... returnTypePreference)
  5. getMethodReturnType(Class clazz, String methodName)
  6. getMethodReturnType(Class clazz, String name)