Java Reflection Method Return getMethod(Class parentClass, Class returnType, String methodName, Class... types)

Here you can find the source of getMethod(Class parentClass, Class returnType, String methodName, Class... types)

Description

get Method

License

Open Source License

Declaration

public static Method getMethod(Class<?> parentClass, Class<?> returnType, String methodName,
            Class<?>... types) 

Method Source Code


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

import java.lang.reflect.Method;

public class Main {
    public static Method getMethod(Class<?> parentClass, Class<?> returnType, String methodName,
            Class<?>... types) {
        for (Method method : parentClass.getDeclaredMethods()) {
            if (equalsTo(method, returnType, methodName, types))
                return method;
        }//from w  w  w  . j a v  a  2  s. c  o  m

        return null;
    }

    private static boolean equalsTo(Method otherMethod, Class<?> returnType, String methodName,
            Class<?>... methodTypes) {
        if (methodName != null && !methodName.equals(otherMethod.getName()))
            return false;

        if (otherMethod.getParameterCount() != methodTypes.length)
            return false; // not equal methods

        if (!otherMethod.getReturnType().equals(returnType))
            return false;

        for (int i = 0; i < otherMethod.getParameterTypes().length; i++) {
            if (otherMethod.getParameterTypes()[i] != methodTypes[i])
                return false;
        }

        return true;
    }
}

Related

  1. getMethod(Class clazz, String methodName, Class returnType, Class... inputParams)
  2. getMethod(Class clazz, String name, String returnType, String[] args)
  3. getMethod(Class type, String name, Class returnType, Class paramType, boolean caseSensitive)
  4. getMethodByReturnType(final Class source, final Class type)
  5. getMethodByTypes(Class clazz, String name, Class returnType, Class... parameterTypes)
  6. getMethodDesc(Class returnType, Class... params)