public static Constructor lookupConstructor()

in src/main/java/org/apache/commons/jxpath/util/MethodLookupUtils.java [43:101]


    public static Constructor lookupConstructor(
        final Class targetClass,
        final Object[] parameters) {
        boolean tryExact = true;
        final int count = parameters == null ? 0 : parameters.length;
        final Class[] types = new Class[count];
        for (int i = 0; i < count; i++) {
            final Object param = parameters[i];
            if (param != null) {
                types[i] = param.getClass();
            }
            else {
                types[i] = null;
                tryExact = false;
            }
        }

        Constructor constructor = null;

        if (tryExact) {
            // First - without type conversion
            try {
                constructor = targetClass.getConstructor(types);
                if (constructor != null) {
                    return constructor;
                }
            }
            catch (final NoSuchMethodException ignore) { // NOPMD
                // Ignore
            }
        }

        int currentMatch = 0;
        boolean ambiguous = false;

        // Then - with type conversion
        final Constructor[] constructors = targetClass.getConstructors();
        for (final Constructor constructor2 : constructors) {
            final int match =
                matchParameterTypes(
                    constructor2.getParameterTypes(),
                    parameters);
            if (match != NO_MATCH) {
                if (match > currentMatch) {
                    constructor = constructor2;
                    currentMatch = match;
                    ambiguous = false;
                }
                else if (match == currentMatch) {
                    ambiguous = true;
                }
            }
        }
        if (ambiguous) {
            throw new JXPathException(
                "Ambiguous constructor " + Arrays.asList(parameters));
        }
        return constructor;
    }