Search in sources :

Example 21 with FastArray

use of org.codehaus.groovy.util.FastArray in project groovy-core by groovy.

the class MetaMethodIndex method copyNonPrivateNonNewMetaMethods.

private void copyNonPrivateNonNewMetaMethods(Entry from, Header to) {
    Object oldListOrMethod = from.methods;
    if (oldListOrMethod == null)
        return;
    if (oldListOrMethod instanceof FastArray) {
        FastArray oldList = (FastArray) oldListOrMethod;
        Entry e = null;
        int len1 = oldList.size();
        Object[] list = oldList.getArray();
        for (int j = 0; j != len1; ++j) {
            MetaMethod method = (MetaMethod) list[j];
            if (method instanceof NewMetaMethod || method.isPrivate())
                continue;
            if (e == null)
                e = getOrPutMethods(from.name, to);
            e.methods = addMethodToList(e.methods, method);
        }
    } else {
        MetaMethod method = (MetaMethod) oldListOrMethod;
        if (method instanceof NewMetaMethod || method.isPrivate())
            return;
        Entry e = getOrPutMethods(from.name, to);
        e.methods = addMethodToList(e.methods, method);
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) FastArray(org.codehaus.groovy.util.FastArray)

Example 22 with FastArray

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

the class MetaClassImpl method findMethod.

/**
     * @return the matching method which should be found
     */
private MetaMethod findMethod(CachedMethod aMethod) {
    Object methods = getMethods(theClass, aMethod.getName(), false);
    if (methods instanceof FastArray) {
        FastArray m = (FastArray) methods;
        final int len = m.size;
        final Object[] data = m.getArray();
        for (int i = 0; i != len; ++i) {
            MetaMethod method = (MetaMethod) data[i];
            if (method.isMethod(aMethod)) {
                return method;
            }
        }
    } else {
        MetaMethod method = (MetaMethod) methods;
        if (method.getName().equals(aMethod.getName()) && //                    && method.getModifiers() == aMethod.getModifiers()
        method.getReturnType().equals(aMethod.getReturnType()) && MetaMethod.equal(method.getParameterTypes(), aMethod.getParameterTypes())) {
            return method;
        }
    }
    return aMethod;
}
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) FastArray(org.codehaus.groovy.util.FastArray)

Example 23 with FastArray

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

the class ExpandoMetaClass method registerSubclassInstanceMethod.

public void registerSubclassInstanceMethod(MetaMethod metaMethod) {
    modified = true;
    final String name = metaMethod.getName();
    Object methodOrList = expandoSubclassMethods.get(name);
    if (methodOrList == null) {
        expandoSubclassMethods.put(name, metaMethod);
    } else {
        if (methodOrList instanceof MetaMethod) {
            FastArray arr = new FastArray(2);
            arr.add(methodOrList);
            arr.add(metaMethod);
            expandoSubclassMethods.put(name, arr);
        } else {
            ((FastArray) methodOrList).add(metaMethod);
        }
    }
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) FastArray(org.codehaus.groovy.util.FastArray)

Example 24 with FastArray

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

the class MetaClassImpl method filterPropertyMethod.

/**
     * return null if nothing valid has been found, a MetaMethod (for getter always the case if not null) or
     * a LinkedList<MetaMethod> if there are multiple setter
     */
private static Object filterPropertyMethod(Object methodOrList, boolean isGetter, boolean booleanGetter) {
    // Method has been optimized to reach a target of 325 bytecode size, making it JIT'able
    Object ret = null;
    if (methodOrList instanceof MetaMethod) {
        MetaMethod element = (MetaMethod) methodOrList;
        int parameterCount = element.getParameterTypes().length;
        if (!isGetter && //(element.getReturnType() == Void.class || element.getReturnType() == Void.TYPE) &&
        parameterCount == 1) {
            ret = element;
        }
        Class returnType = element.getReturnType();
        if (isGetter && !(returnType == Void.class || returnType == Void.TYPE) && (!booleanGetter || returnType == Boolean.class || returnType == Boolean.TYPE) && parameterCount == 0) {
            ret = element;
        }
    }
    if (methodOrList instanceof FastArray) {
        FastArray methods = (FastArray) methodOrList;
        final int len = methods.size();
        final Object[] data = methods.getArray();
        for (int i = 0; i != len; ++i) {
            MetaMethod element = (MetaMethod) data[i];
            int parameterCount = element.getParameterTypes().length;
            if (!isGetter && //(element.getReturnType() == Void.class || element.getReturnType() == Void.TYPE) &&
            parameterCount == 1) {
                ret = addElementToList(ret, element);
            }
            Class returnType = element.getReturnType();
            if (isGetter && !(returnType == Void.class || returnType == Void.TYPE) && parameterCount == 0) {
                ret = addElementToList(ret, element);
            }
        }
    }
    if (ret == null || (ret instanceof MetaMethod) || !isGetter) {
        return ret;
    }
    // we found multiple matching methods
    // this is a problem, because we can use only one
    // if it is a getter, then use the most general return
    // type to decide which method to use. If it is a setter
    // we use the type of the first parameter
    MetaMethod method = null;
    int distance = -1;
    for (final Object o : ((List) ret)) {
        MetaMethod element = (MetaMethod) o;
        int localDistance = distanceToObject(element.getReturnType());
        //TODO: maybe implement the case localDistance==distance
        if (distance == -1 || distance > localDistance) {
            distance = localDistance;
            method = element;
        }
    }
    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) CachedClass(org.codehaus.groovy.reflection.CachedClass) FastArray(org.codehaus.groovy.util.FastArray)

Example 25 with FastArray

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

the class MetaMethodIndex method copyAllMethodsToSuper.

private void copyAllMethodsToSuper(Entry from, Header to) {
    Object oldListOrMethod = from.methods;
    if (oldListOrMethod instanceof FastArray) {
        FastArray oldList = (FastArray) oldListOrMethod;
        Entry e = null;
        int len1 = oldList.size();
        Object[] list = oldList.getArray();
        for (int j = 0; j != len1; ++j) {
            MetaMethod method = (MetaMethod) list[j];
            if (e == null)
                e = getOrPutMethods(from.name, to);
            e.methodsForSuper = addMethodToList(e.methodsForSuper, method);
        }
    } else {
        MetaMethod method = (MetaMethod) oldListOrMethod;
        Entry e = getOrPutMethods(from.name, to);
        e.methodsForSuper = addMethodToList(e.methodsForSuper, method);
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) 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