Java Reflection Method Return getMethodNoArgs(final Class objClass, final String methodName, final Class... returnTypePreference)

Here you can find the source of getMethodNoArgs(final Class objClass, final String methodName, final Class... returnTypePreference)

Description

Direct getMethod attempt.

License

Open Source License

Parameter

Parameter Description
objClass a parameter
methodName a parameter
returnTypePreference a parameter

Declaration

public static Method getMethodNoArgs(final Class<?> objClass, final String methodName,
        final Class<?>... returnTypePreference) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {
    /**/* ww w. jav  a2 s . c  om*/
     * Direct getMethod attempt.
     * @param objClass
     * @param methodName
     * @param returnTypePreference
     * @return
     */
    public static Method getMethodNoArgs(final Class<?> objClass, final String methodName,
            final Class<?>... returnTypePreference) {
        try {
            final Method methodFound = objClass.getMethod(methodName);
            if (methodFound != null) {
                if (returnTypePreference == null || returnTypePreference.length == 0) {
                    return methodFound;
                }
                final Class<?> returnType = methodFound.getReturnType();
                for (int i = 0; i < returnTypePreference.length; i++) {
                    if (returnType == returnTypePreference[i]) {
                        return methodFound;
                    }
                }
            }
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        }
        return null;
    }

    /**
     * Fail-safe getMethod.
     * @param clazz
     * @param methodName
     * @param arguments
     * @return null in case of errors.
     */
    public static Method getMethod(Class<?> clazz, String methodName, Class<?>... arguments) {
        try {
            return clazz.getMethod(methodName, arguments);
        } catch (NoSuchMethodException e) {
        } catch (SecurityException e) {
        }
        return null;
    }

    /**
     * Get a method matching one of the declared argument specifications.
     * @param clazz
     * @param methodName
     * @param argumentLists
     * @return The first matching method (given order).
     */
    public static Method getMethod(Class<?> clazz, String methodName, Class<?>[]... argumentLists) {
        Method method = null;
        for (Class<?>[] arguments : argumentLists) {
            method = getMethod(clazz, methodName, arguments);
            if (method != null) {
                return method;
            }
        }
        return null;
    }
}

Related

  1. getMethode(Class clasS, Class Returntype, int modifier, Class... classes)
  2. getMethodGenericReturnType(Method method, Class rawType, int index)
  3. getMethodGenericReturnType(Method method, int index)
  4. getMethodGenericReturnType(Method method, int index)
  5. getMethodInvoker(final Class returnType, final String methodName)
  6. getMethodReturnType(Class clazz, String methodName)
  7. getMethodReturnType(Class clazz, String name)
  8. getMethodReturnType(Class clazz, String name)