Search in sources :

Example 6 with ParameterTypes

use of org.codehaus.groovy.reflection.ParameterTypes in project groovy-core by groovy.

the class MetaClassHelper method chooseEmptyMethodParams.

/**
     * @param methods the methods to choose from
     * @return the method with 1 parameter which takes the most general type of
     *         object (e.g. Object)
     */
public static Object chooseEmptyMethodParams(FastArray methods) {
    Object vargsMethod = null;
    final int len = methods.size();
    final Object[] data = methods.getArray();
    for (int i = 0; i != len; ++i) {
        Object method = data[i];
        final ParameterTypes pt = (ParameterTypes) method;
        CachedClass[] paramTypes = pt.getParameterTypes();
        int paramLength = paramTypes.length;
        if (paramLength == 0) {
            return method;
        } else if (paramLength == 1 && pt.isVargsMethod(EMPTY_ARRAY)) {
            vargsMethod = method;
        }
    }
    return vargsMethod;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) ParameterTypes(org.codehaus.groovy.reflection.ParameterTypes)

Example 7 with ParameterTypes

use of org.codehaus.groovy.reflection.ParameterTypes in project groovy-core by groovy.

the class MetaClassImpl method chooseMethodInternal.

private Object chooseMethodInternal(String methodName, Object methodOrList, Class[] arguments) {
    if (methodOrList instanceof MetaMethod) {
        if (((ParameterTypes) methodOrList).isValidMethod(arguments)) {
            return methodOrList;
        }
        return null;
    }
    FastArray methods = (FastArray) methodOrList;
    if (methods == null)
        return null;
    int methodCount = methods.size();
    if (methodCount <= 0) {
        return null;
    } else if (methodCount == 1) {
        Object method = methods.get(0);
        if (((ParameterTypes) method).isValidMethod(arguments)) {
            return method;
        }
        return null;
    }
    Object answer;
    if (arguments == null || arguments.length == 0) {
        answer = MetaClassHelper.chooseEmptyMethodParams(methods);
    } else {
        Object matchingMethods = null;
        final int len = methods.size;
        Object[] data = methods.getArray();
        for (int i = 0; i != len; ++i) {
            Object method = data[i];
            // making this false helps find matches
            if (((ParameterTypes) method).isValidMethod(arguments)) {
                if (matchingMethods == null)
                    matchingMethods = method;
                else if (matchingMethods instanceof ArrayList)
                    ((ArrayList) matchingMethods).add(method);
                else {
                    List arr = new ArrayList(4);
                    arr.add(matchingMethods);
                    arr.add(method);
                    matchingMethods = arr;
                }
            }
        }
        if (matchingMethods == null) {
            return null;
        } else if (!(matchingMethods instanceof ArrayList)) {
            return matchingMethods;
        }
        return chooseMostSpecificParams(methodName, (List) matchingMethods, arguments);
    }
    if (answer != null) {
        return answer;
    }
    throw new MethodSelectionException(methodName, methods, arguments);
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) MethodSelectionException(org.codehaus.groovy.runtime.metaclass.MethodSelectionException) ParameterTypes(org.codehaus.groovy.reflection.ParameterTypes) FastArray(org.codehaus.groovy.util.FastArray)

Example 8 with ParameterTypes

use of org.codehaus.groovy.reflection.ParameterTypes in project groovy-core by groovy.

the class MetaClassImpl method chooseMostSpecificParams.

private Object chooseMostSpecificParams(String name, List matchingMethods, Class[] arguments) {
    long matchesDistance = -1;
    LinkedList matches = new LinkedList();
    for (Object method : matchingMethods) {
        ParameterTypes paramTypes = (ParameterTypes) method;
        long dist = MetaClassHelper.calculateParameterDistance(arguments, paramTypes);
        if (dist == 0)
            return method;
        if (matches.size() == 0) {
            matches.add(method);
            matchesDistance = dist;
        } else if (dist < matchesDistance) {
            matchesDistance = dist;
            matches.clear();
            matches.add(method);
        } else if (dist == matchesDistance) {
            matches.add(method);
        }
    }
    if (matches.size() == 1) {
        return matches.getFirst();
    }
    if (matches.size() == 0) {
        return null;
    }
    //more than one matching method found --> ambiguous!
    StringBuilder msg = new StringBuilder("Ambiguous method overloading for method ");
    msg.append(theClass.getName()).append("#").append(name).append(".\nCannot resolve which method to invoke for ").append(InvokerHelper.toString(arguments)).append(" due to overlapping prototypes between:");
    for (final Object matche : matches) {
        Class[] types = ((ParameterTypes) matche).getNativeParameterTypes();
        msg.append("\n\t").append(InvokerHelper.toString(types));
    }
    throw new GroovyRuntimeException(msg.toString());
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) ParameterTypes(org.codehaus.groovy.reflection.ParameterTypes)

Aggregations

ParameterTypes (org.codehaus.groovy.reflection.ParameterTypes)8 CachedClass (org.codehaus.groovy.reflection.CachedClass)6 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MethodSelectionException (org.codehaus.groovy.runtime.metaclass.MethodSelectionException)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)2 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)2 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)2 FastArray (org.codehaus.groovy.util.FastArray)2