find Method Ignore Case And Arguments Types Assigned - Java Reflection

Java examples for Reflection:Method Parameter

Description

find Method Ignore Case And Arguments Types Assigned

Demo Code


//package com.java2s;

import java.lang.reflect.Method;
import java.util.Arrays;

public class Main {

    public static Method findMethodIgnoreCaseAndArgsTypesAssigned(
            final Method[] methods, final String methodName,
            final Class<?>... argsClasses) {
        Method method = null;//from  w  ww.  j av a 2  s  .  c  om
        for (final Method m : methods) {
            final String name = m.getName();
            final Class<?>[] argTypes = m.getParameterTypes();
            if (argsClasses.length != argTypes.length) {
                continue;
            }
            if (name.equalsIgnoreCase(methodName)) {
                if (Arrays.equals(argTypes, argsClasses)) {
                    return m;
                }
                int i = 0;
                for (final Class<?> argType : argTypes) {
                    if (argType.isAssignableFrom(argsClasses[i])) {
                        ++i;
                    } else {
                        break;
                    }
                }
                if (i == argsClasses.length) {
                    method = m;
                }
            }
        }
        return method;
    }
}

Related Tutorials