Search in sources :

Example 26 with FastArray

use of org.codehaus.groovy.util.FastArray in project groovy by apache.

the class MetaMethodIndex method addMethodToList.

public Object addMethodToList(Object o, MetaMethod method) {
    if (o == null) {
        return method;
    }
    if (o instanceof MetaMethod) {
        MetaMethod match = (MetaMethod) o;
        if (!isMatchingMethod(match, method)) {
            FastArray list = new FastArray(2);
            list.add(match);
            list.add(method);
            return list;
        } else {
            if (match.isPrivate() || (!isNonRealMethod(match) && match.getDeclaringClass().isInterface() && !method.getDeclaringClass().isInterface())) {
            // do not overwrite interface methods with instance methods
            // do not overwrite private methods
            // Note: private methods from parent classes are not shown here,
            // but when doing the multimethod connection step, we overwrite
            // methods of the parent class with methods of a subclass and
            // in that case we want to keep the private methods
            } else {
                CachedClass methodC = method.getDeclaringClass();
                CachedClass matchC = match.getDeclaringClass();
                if (methodC == matchC) {
                    if (isNonRealMethod(method)) {
                        return method;
                    }
                } else if (!methodC.isAssignableFrom(matchC.getTheClass())) {
                    return method;
                }
            }
        }
        return o;
    }
    if (o instanceof FastArray) {
        FastArray list = (FastArray) o;
        int found = findMatchingMethod(list, method);
        if (found == -1) {
            list.add(method);
        } else {
            MetaMethod match = (MetaMethod) list.get(found);
            if (match == method)
                return o;
            if (match.isPrivate() || (!isNonRealMethod(match) && match.getDeclaringClass().isInterface() && !method.getDeclaringClass().isInterface())) {
            // do not overwrite interface methods with instance methods
            // do not overwrite private methods
            // Note: private methods from parent classes are not shown here,
            // but when doing the multimethod connection step, we overwrite
            // methods of the parent class with methods of a subclass and
            // in that case we want to keep the private methods
            } else {
                CachedClass methodC = method.getDeclaringClass();
                CachedClass matchC = match.getDeclaringClass();
                if (methodC == matchC) {
                    if (isNonRealMethod(method)) {
                        list.set(found, method);
                    }
                } else if (!methodC.isAssignableFrom(matchC.getTheClass())) {
                    list.set(found, method);
                }
            }
        }
    }
    return o;
}
Also used : MetaMethod(groovy.lang.MetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) FastArray(org.codehaus.groovy.util.FastArray)

Example 27 with FastArray

use of org.codehaus.groovy.util.FastArray in project groovy by apache.

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 28 with FastArray

use of org.codehaus.groovy.util.FastArray in project groovy by apache.

the class MetaClassImpl method getMethods.

/**
     * Gets all instance methods available on this class for the given name
     *
     * @return all the normal instance methods available on this class for the
     *         given name
     */
private Object getMethods(Class sender, String name, boolean isCallToSuper) {
    Object answer;
    final MetaMethodIndex.Entry entry = metaMethodIndex.getMethods(sender, name);
    if (entry == null)
        answer = FastArray.EMPTY_LIST;
    else if (isCallToSuper) {
        answer = entry.methodsForSuper;
    } else {
        answer = entry.methods;
    }
    if (answer == null)
        answer = FastArray.EMPTY_LIST;
    if (!isCallToSuper) {
        List used = GroovyCategorySupport.getCategoryMethods(name);
        if (used != null) {
            FastArray arr;
            if (answer instanceof MetaMethod) {
                arr = new FastArray();
                arr.add(answer);
            } else
                arr = ((FastArray) answer).copy();
            for (Iterator iter = used.iterator(); iter.hasNext(); ) {
                MetaMethod element = (MetaMethod) iter.next();
                if (!element.getDeclaringClass().getTheClass().isAssignableFrom(sender))
                    continue;
                filterMatchingMethodForCategory(arr, element);
            }
            answer = arr;
        }
    }
    return answer;
}
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) MetaMethodIndex(org.codehaus.groovy.runtime.metaclass.MetaMethodIndex) FastArray(org.codehaus.groovy.util.FastArray)

Aggregations

FastArray (org.codehaus.groovy.util.FastArray)28 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)26 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)16 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)16 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)14 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)14 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)14 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)14 MetaMethod (groovy.lang.MetaMethod)12 CachedClass (org.codehaus.groovy.reflection.CachedClass)10 MethodSelectionException (org.codehaus.groovy.runtime.metaclass.MethodSelectionException)4 ParameterTypes (org.codehaus.groovy.reflection.ParameterTypes)2 ClosureStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod)2 MetaMethodIndex (org.codehaus.groovy.runtime.metaclass.MetaMethodIndex)2