Search in sources :

Example 51 with CachedClass

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

the class MetaClassImpl method getMetaProperty.

private MetaProperty getMetaProperty(String name, boolean useStatic) {
    CachedClass clazz = theCachedClass;
    SingleKeyHashMap propertyMap;
    if (useStatic) {
        propertyMap = staticPropertyIndex;
    } else {
        propertyMap = classPropertyIndex.getNullable(clazz);
    }
    if (propertyMap == null) {
        return null;
    }
    return (MetaProperty) propertyMap.get(name);
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) CachedClass(org.codehaus.groovy.reflection.CachedClass) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)

Example 52 with CachedClass

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

the class MetaClassImpl method getMatchKindForCategory.

/**
     * return false: add method
     *        null:  ignore method
     *        true:  replace
     */
private Boolean getMatchKindForCategory(MetaMethod aMethod, MetaMethod categoryMethod) {
    CachedClass[] params1 = aMethod.getParameterTypes();
    CachedClass[] params2 = categoryMethod.getParameterTypes();
    if (params1.length != params2.length)
        return Boolean.FALSE;
    for (int i = 0; i < params1.length; i++) {
        if (params1[i] != params2[i])
            return Boolean.FALSE;
    }
    Class aMethodClass = aMethod.getDeclaringClass().getTheClass();
    Class categoryMethodClass = categoryMethod.getDeclaringClass().getTheClass();
    if (aMethodClass == categoryMethodClass)
        return Boolean.TRUE;
    boolean match = aMethodClass.isAssignableFrom(categoryMethodClass);
    if (match)
        return Boolean.TRUE;
    return null;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 53 with CachedClass

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

the class MetaClassImpl method addMetaMethod.

// Implementation methods
//-------------------------------------------------------------------------
/**
     * adds a MetaMethod to this class. WARNING: this method will not
     * do the neccessary steps for multimethod logic and using this
     * method doesn't mean, that a method added here is replacing another
     * method from a parent class completely. These steps are usually done
     * by initialize, which means if you need these steps, you have to add
     * the method before running initialize the first time.
     *
     * @param method the MetaMethod
     * @see #initialize()
     */
public void addMetaMethod(MetaMethod method) {
    if (isInitialized()) {
        throw new RuntimeException("Already initialized, cannot add new method: " + method);
    }
    final CachedClass declaringClass = method.getDeclaringClass();
    addMetaMethodToIndex(method, metaMethodIndex.getHeader(declaringClass.getTheClass()));
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 54 with CachedClass

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

the class DgmConverter method createDoMethodInvokeMethod.

private static void createDoMethodInvokeMethod(CachedMethod method, ClassWriter cw, String className, Class returnType, String methodDescriptor) {
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL, "doMethodInvoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    mv.visitCode();
    if (method.getParamsCount() == 2 && method.getParameterTypes()[0].isNumber && method.getParameterTypes()[1].isNumber) {
        mv.visitVarInsn(ALOAD, 1);
        BytecodeHelper.doCast(mv, method.getParameterTypes()[0].getTheClass());
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, className, "getParameterTypes", "()[Lorg/codehaus/groovy/reflection/CachedClass;", false);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(AALOAD);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(AALOAD);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/CachedClass", "coerceArgument", "(Ljava/lang/Object;)Ljava/lang/Object;", false);
        // cast argument to parameter class, inclusive unboxing
        // for methods with primitive types
        Class type = method.getParameterTypes()[1].getTheClass();
        BytecodeHelper.doCast(mv, type);
    } else {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEVIRTUAL, className, "coerceArgumentsToClasses", "([Ljava/lang/Object;)[Ljava/lang/Object;", false);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 1);
        BytecodeHelper.doCast(mv, method.getParameterTypes()[0].getTheClass());
        loadParameters(method, 2, mv);
    }
    mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(method.getDeclaringClass().getTheClass()), method.getName(), methodDescriptor, false);
    BytecodeHelper.box(mv, returnType);
    if (method.getReturnType() == void.class) {
        mv.visitInsn(ACONST_NULL);
    }
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 55 with CachedClass

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

the class DgmConverter method loadParameters.

protected static void loadParameters(CachedMethod method, int argumentIndex, MethodVisitor mv) {
    CachedClass[] parameters = method.getParameterTypes();
    int size = parameters.length - 1;
    for (int i = 0; i < size; i++) {
        // unpack argument from Object[]
        mv.visitVarInsn(ALOAD, argumentIndex);
        BytecodeHelper.pushConstant(mv, i);
        mv.visitInsn(AALOAD);
        // cast argument to parameter class, inclusive unboxing
        // for methods with primitive types
        Class type = parameters[i + 1].getTheClass();
        BytecodeHelper.doCast(mv, type);
    }
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) CachedClass(org.codehaus.groovy.reflection.CachedClass)

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