Search in sources :

Example 1 with MethodSelectionException

use of org.codehaus.groovy.runtime.metaclass.MethodSelectionException in project groovy by apache.

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 2 with MethodSelectionException

use of org.codehaus.groovy.runtime.metaclass.MethodSelectionException in project groovy by apache.

the class ExpandoMetaClass method findMixinMethod.

public MetaMethod findMixinMethod(String methodName, Class[] arguments) {
    for (MixinInMetaClass mixin : mixinClasses) {
        final CachedClass mixinClass = mixin.getMixinClass();
        MetaClass metaClass = mixinClass.classInfo.getMetaClassForClass();
        if (metaClass == null) {
            metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(mixinClass.getTheClass());
        }
        MetaMethod metaMethod = metaClass.pickMethod(methodName, arguments);
        if (metaMethod == null && metaClass instanceof MetaClassImpl) {
            MetaClassImpl mc = (MetaClassImpl) metaClass;
            for (CachedClass cl = mc.getTheCachedClass().getCachedSuperClass(); cl != null; cl = cl.getCachedSuperClass()) {
                metaMethod = mc.getMethodWithoutCaching(cl.getTheClass(), methodName, arguments, false);
                if (metaMethod != null)
                    break;
            }
        }
        if (metaMethod != null) {
            MetaMethod method = new MixinInstanceMetaMethod(metaMethod, mixin);
            if (method.getParameterTypes().length == 1 && !method.getParameterTypes()[0].isPrimitive) {
                MetaMethod noParam = pickMethod(methodName, EMPTY_CLASS_ARRAY);
                // if the current call itself is with empty arg class array, no need to recurse with 'new Class[0]'
                if (noParam == null && arguments.length != 0) {
                    try {
                        findMixinMethod(methodName, EMPTY_CLASS_ARRAY);
                    } catch (MethodSelectionException msex) {
                    /*
                             * Here we just additionally tried to find another no-arg mixin method of the same name and register that as well, if found.
                             * Safe to ignore a MethodSelectionException in this additional exercise. (GROOVY-4999)
                             */
                    }
                }
            }
            registerInstanceMethod(method);
            return method;
        }
    }
    return null;
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) MethodSelectionException(org.codehaus.groovy.runtime.metaclass.MethodSelectionException) OwnedMetaClass(org.codehaus.groovy.runtime.metaclass.OwnedMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)

Example 3 with MethodSelectionException

use of org.codehaus.groovy.runtime.metaclass.MethodSelectionException in project groovy-core by groovy.

the class MetaClassImpl method pickStaticMethod.

private MetaMethod pickStaticMethod(String methodName, Class[] arguments) {
    MetaMethod method = null;
    MethodSelectionException mse = null;
    Object methods = getStaticMethods(theClass, methodName);
    if (!(methods instanceof FastArray) || !((FastArray) methods).isEmpty()) {
        try {
            method = (MetaMethod) chooseMethod(methodName, methods, arguments);
        } catch (MethodSelectionException msex) {
            mse = msex;
        }
    }
    if (method == null && theClass != Class.class) {
        MetaClass classMetaClass = registry.getMetaClass(Class.class);
        method = classMetaClass.pickMethod(methodName, arguments);
    }
    if (method == null) {
        method = (MetaMethod) chooseMethod(methodName, methods, MetaClassHelper.convertToTypeArray(arguments));
    }
    if (method == null && mse != null) {
        throw mse;
    } else {
        return method;
    }
}
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) CachedClass(org.codehaus.groovy.reflection.CachedClass) FastArray(org.codehaus.groovy.util.FastArray)

Example 4 with MethodSelectionException

use of org.codehaus.groovy.runtime.metaclass.MethodSelectionException in project groovy-core by groovy.

the class ExpandoMetaClass method findMixinMethod.

public MetaMethod findMixinMethod(String methodName, Class[] arguments) {
    for (MixinInMetaClass mixin : mixinClasses) {
        final CachedClass mixinClass = mixin.getMixinClass();
        MetaClass metaClass = mixinClass.classInfo.getMetaClassForClass();
        if (metaClass == null) {
            metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(mixinClass.getTheClass());
        }
        MetaMethod metaMethod = metaClass.pickMethod(methodName, arguments);
        if (metaMethod == null && metaClass instanceof MetaClassImpl) {
            MetaClassImpl mc = (MetaClassImpl) metaClass;
            for (CachedClass cl = mc.getTheCachedClass().getCachedSuperClass(); cl != null; cl = cl.getCachedSuperClass()) {
                metaMethod = mc.getMethodWithoutCaching(cl.getTheClass(), methodName, arguments, false);
                if (metaMethod != null)
                    break;
            }
        }
        if (metaMethod != null) {
            MetaMethod method = new MixinInstanceMetaMethod(metaMethod, mixin);
            if (method.getParameterTypes().length == 1 && !method.getParameterTypes()[0].isPrimitive) {
                MetaMethod noParam = pickMethod(methodName, EMPTY_CLASS_ARRAY);
                // if the current call itself is with empty arg class array, no need to recurse with 'new Class[0]'
                if (noParam == null && arguments.length != 0) {
                    try {
                        findMixinMethod(methodName, EMPTY_CLASS_ARRAY);
                    } catch (MethodSelectionException msex) {
                    /*
                             * Here we just additionally tried to find another no-arg mixin method of the same name and register that as well, if found.
                             * Safe to ignore a MethodSelectionException in this additional exercise. (GROOVY-4999)
                             */
                    }
                }
            }
            registerInstanceMethod(method);
            return method;
        }
    }
    return null;
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) MethodSelectionException(org.codehaus.groovy.runtime.metaclass.MethodSelectionException) OwnedMetaClass(org.codehaus.groovy.runtime.metaclass.OwnedMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)

Example 5 with MethodSelectionException

use of org.codehaus.groovy.runtime.metaclass.MethodSelectionException 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)

Aggregations

ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)6 MethodSelectionException (org.codehaus.groovy.runtime.metaclass.MethodSelectionException)6 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)6 CachedClass (org.codehaus.groovy.reflection.CachedClass)4 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)4 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)4 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)4 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)4 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)4 FastArray (org.codehaus.groovy.util.FastArray)4 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)2 ParameterTypes (org.codehaus.groovy.reflection.ParameterTypes)2 ClosureStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod)2 MixedInMetaClass (org.codehaus.groovy.runtime.metaclass.MixedInMetaClass)2 OwnedMetaClass (org.codehaus.groovy.runtime.metaclass.OwnedMetaClass)2