Search in sources :

Example 61 with CachedClass

use of org.codehaus.groovy.reflection.CachedClass in project groovy by apache.

the class MetaClassImpl method findMatchingMethod.

private int findMatchingMethod(CachedMethod[] data, int from, int to, MetaMethod method) {
    for (int j = from; j <= to; ++j) {
        CachedMethod aMethod = data[j];
        CachedClass[] params1 = aMethod.getParameterTypes();
        CachedClass[] params2 = method.getParameterTypes();
        if (params1.length == params2.length) {
            boolean matches = true;
            for (int i = 0; i < params1.length; i++) {
                if (params1[i] != params2[i]) {
                    matches = false;
                    break;
                }
            }
            if (matches) {
                return j;
            }
        }
    }
    return -1;
}
Also used : CachedMethod(org.codehaus.groovy.reflection.CachedMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 62 with CachedClass

use of org.codehaus.groovy.reflection.CachedClass in project groovy by apache.

the class MetaClassImpl method inheritFields.

private void inheritFields(LinkedList<CachedClass> superClasses) {
    SingleKeyHashMap last = null;
    for (CachedClass klass : superClasses) {
        SingleKeyHashMap propertyIndex = classPropertyIndex.getNotNull(klass);
        if (last != null) {
            copyNonPrivateFields(last, propertyIndex, klass);
        }
        last = propertyIndex;
        addFields(klass, propertyIndex);
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 63 with CachedClass

use of org.codehaus.groovy.reflection.CachedClass in project groovy by apache.

the class MetaClassImpl method inheritStaticInterfaceFields.

private void inheritStaticInterfaceFields(LinkedList superClasses, Set interfaces) {
    for (Iterator interfaceIter = interfaces.iterator(); interfaceIter.hasNext(); ) {
        CachedClass iclass = (CachedClass) interfaceIter.next();
        SingleKeyHashMap iPropertyIndex = classPropertyIndex.getNotNull(iclass);
        addFields(iclass, iPropertyIndex);
        for (Iterator classIter = superClasses.iterator(); classIter.hasNext(); ) {
            CachedClass sclass = (CachedClass) classIter.next();
            if (!iclass.getTheClass().isAssignableFrom(sclass.getTheClass()))
                continue;
            SingleKeyHashMap sPropertyIndex = classPropertyIndex.getNotNull(sclass);
            copyNonPrivateFields(iPropertyIndex, sPropertyIndex);
        }
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 64 with CachedClass

use of org.codehaus.groovy.reflection.CachedClass in project groovy by apache.

the class MetaClassHelper method calculateParameterDistance.

public static long calculateParameterDistance(Class[] arguments, ParameterTypes pt) {
    CachedClass[] parameters = pt.getParameterTypes();
    if (parameters.length == 0)
        return 0;
    long ret = 0;
    int noVargsLength = parameters.length - 1;
    // we can safely iterate to this point
    for (int i = 0; i < noVargsLength; i++) {
        ret += calculateParameterDistance(arguments[i], parameters[i]);
    }
    if (arguments.length == parameters.length) {
        // case C&D, we use baseType to calculate and set it
        // to the value we need according to case C and D
        // case C
        CachedClass baseType = parameters[noVargsLength];
        if (!parameters[noVargsLength].isAssignableFrom(arguments[noVargsLength])) {
            // case D
            baseType = ReflectionCache.getCachedClass(baseType.getTheClass().getComponentType());
            // penalty for vargs
            ret += 2L << VARGS_SHIFT;
        }
        ret += calculateParameterDistance(arguments[noVargsLength], baseType);
    } else if (arguments.length > parameters.length) {
        // case B
        // we give our a vargs penalty for each exceeding argument and iterate
        // by using parameters[noVargsLength].getComponentType()
        // penalty for vargs
        ret += (2L + arguments.length - parameters.length) << VARGS_SHIFT;
        CachedClass vargsType = ReflectionCache.getCachedClass(parameters[noVargsLength].getTheClass().getComponentType());
        for (int i = noVargsLength; i < arguments.length; i++) {
            ret += calculateParameterDistance(arguments[i], vargsType);
        }
    } else {
        // case A
        // we give a penalty for vargs, since we have no direct
        // match for the last argument
        ret += 1L << VARGS_SHIFT;
    }
    return ret;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 65 with CachedClass

use of org.codehaus.groovy.reflection.CachedClass in project groovy by apache.

the class CallSiteGenerator method compilePojoMethod.

public static Constructor compilePojoMethod(CachedMethod cachedMethod) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    final CachedClass declClass = cachedMethod.getDeclaringClass();
    final CallSiteClassLoader callSiteLoader = declClass.getCallSiteLoader();
    final String name = callSiteLoader.createClassName(cachedMethod.setAccessible());
    final byte[] bytes = genPojoMetaMethodSite(cachedMethod, cw, name);
    return callSiteLoader.defineClassAndGetConstructor(name, bytes);
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

CachedClass (org.codehaus.groovy.reflection.CachedClass)68 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)17 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)17 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)15 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)15 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)15 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)13 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)13 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)13 SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)10 MetaMethodIndex (org.codehaus.groovy.runtime.metaclass.MetaMethodIndex)8 ClassWriter (org.objectweb.asm.ClassWriter)6 ParameterTypes (org.codehaus.groovy.reflection.ParameterTypes)4 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)4 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)4 MetaMethod (groovy.lang.MetaMethod)3 FastArray (org.codehaus.groovy.util.FastArray)3 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)2 ConvertedClosure (org.codehaus.groovy.runtime.ConvertedClosure)2 CurriedClosure (org.codehaus.groovy.runtime.CurriedClosure)2