public static Method lookupMethod()

in src/main/java/org/apache/commons/jxpath/util/MethodLookupUtils.java [99:155]


    public static Method lookupMethod(Class targetClass, final String name, final Object[] parameters) {
        if (parameters == null || parameters.length < 1 || parameters[0] == null) {
            return null;
        }
        if (matchType(targetClass, parameters[0]) == NO_MATCH) {
            return null;
        }
        targetClass = TypeUtils.convert(parameters[0], targetClass).getClass();
        boolean tryExact = true;
        final int count = parameters.length - 1;
        final Class[] types = new Class[count];
        final Object[] arguments = new Object[count];
        for (int i = 0; i < count; i++) {
            final Object param = parameters[i + 1];
            arguments[i] = param;
            if (param != null) {
                types[i] = param.getClass();
            } else {
                types[i] = null;
                tryExact = false;
            }
        }
        Method method = null;
        if (tryExact) {
            // First - without type conversion
            try {
                method = targetClass.getMethod(name, types);
                if (method != null && !Modifier.isStatic(method.getModifiers())) {
                    return method;
                }
            } catch (final NoSuchMethodException ignore) { // NOPMD
                // Ignore
            }
        }
        int currentMatch = 0;
        boolean ambiguous = false;
        // Then - with type conversion
        final Method[] methods = targetClass.getMethods();
        for (final Method method2 : methods) {
            if (!Modifier.isStatic(method2.getModifiers()) && method2.getName().equals(name)) {
                final int match = matchParameterTypes(method2.getParameterTypes(), arguments);
                if (match != NO_MATCH) {
                    if (match > currentMatch) {
                        method = method2;
                        currentMatch = match;
                        ambiguous = false;
                    } else if (match == currentMatch) {
                        ambiguous = true;
                    }
                }
            }
        }
        if (ambiguous) {
            throw new JXPathException("Ambiguous method call: " + name);
        }
        return method;
    }