Search in sources :

Example 1 with CachedClass

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

the class MetaClassImpl method populateMethods.

private void populateMethods(LinkedList<CachedClass> superClasses, CachedClass firstGroovySuper) {
    MetaMethodIndex.Header header = metaMethodIndex.getHeader(firstGroovySuper.getTheClass());
    CachedClass c;
    Iterator<CachedClass> iter = superClasses.iterator();
    for (; iter.hasNext(); ) {
        c = iter.next();
        CachedMethod[] cachedMethods = c.getMethods();
        for (CachedMethod metaMethod : cachedMethods) {
            addToAllMethodsIfPublic(metaMethod);
            if (!metaMethod.isPrivate() || c == firstGroovySuper)
                addMetaMethodToIndex(metaMethod, header);
        }
        MetaMethod[] cachedMethods1 = getNewMetaMethods(c);
        for (final MetaMethod method : cachedMethods1) {
            if (!newGroovyMethodsSet.contains(method)) {
                newGroovyMethodsSet.add(method);
                addMetaMethodToIndex(method, header);
            }
        }
        if (c == firstGroovySuper)
            break;
    }
    MetaMethodIndex.Header last = header;
    for (; iter.hasNext(); ) {
        c = iter.next();
        header = metaMethodIndex.getHeader(c.getTheClass());
        if (last != null) {
            metaMethodIndex.copyNonPrivateMethods(last, header);
        }
        last = header;
        for (CachedMethod metaMethod : c.getMethods()) {
            addToAllMethodsIfPublic(metaMethod);
            addMetaMethodToIndex(metaMethod, header);
        }
        for (final MetaMethod method : getNewMetaMethods(c)) {
            if (method.getName().equals("<init>") && !method.getDeclaringClass().equals(theCachedClass))
                continue;
            if (!newGroovyMethodsSet.contains(method)) {
                newGroovyMethodsSet.add(method);
                addMetaMethodToIndex(method, header);
            }
        }
    }
}
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) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 2 with CachedClass

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

the class MetaClassImpl method fillMethodIndex.

/**
     * Fills the method index
     */
private void fillMethodIndex() {
    mainClassMethodHeader = metaMethodIndex.getHeader(theClass);
    LinkedList<CachedClass> superClasses = getSuperClasses();
    CachedClass firstGroovySuper = calcFirstGroovySuperClass(superClasses);
    Set<CachedClass> interfaces = theCachedClass.getInterfaces();
    addInterfaceMethods(interfaces);
    populateMethods(superClasses, firstGroovySuper);
    inheritInterfaceNewMetaMethods(interfaces);
    if (isGroovyObject) {
        metaMethodIndex.copyMethodsToSuper();
        connectMultimethods(superClasses, firstGroovySuper);
        removeMultimethodsOverloadedWithPrivateMethods();
        replaceWithMOPCalls(theCachedClass.mopMethods);
    }
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 3 with CachedClass

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

the class MetaClassImpl method getMatchKindForCategory.

/**
     * return false: add method
     *        null:  ignore method
     *        true:  replace
     */
private static 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 4 with CachedClass

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

the class MetaClassImpl method getCategoryMethodSetter.

private static MetaMethod getCategoryMethodSetter(Class sender, String name, boolean useLongVersion) {
    List possibleGenericMethods = GroovyCategorySupport.getCategoryMethods(name);
    if (possibleGenericMethods != null) {
        for (Iterator iter = possibleGenericMethods.iterator(); iter.hasNext(); ) {
            MetaMethod mmethod = (MetaMethod) iter.next();
            if (!mmethod.getDeclaringClass().getTheClass().isAssignableFrom(sender))
                continue;
            CachedClass[] paramTypes = mmethod.getParameterTypes();
            if (useLongVersion) {
                if (paramTypes.length == 2 && paramTypes[0].getTheClass() == String.class) {
                    return mmethod;
                }
            } else {
                if (paramTypes.length == 1)
                    return mmethod;
            }
        }
    }
    return null;
}
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)

Example 5 with CachedClass

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

the class MetaClassImpl method applyStrayPropertyMethods.

private void applyStrayPropertyMethods(LinkedList<CachedClass> superClasses, Index classPropertyIndex, boolean isThis) {
    // now look for any stray getters that may be used to define a property
    for (CachedClass klass : superClasses) {
        MetaMethodIndex.Header header = metaMethodIndex.getHeader(klass.getTheClass());
        SingleKeyHashMap propertyIndex = classPropertyIndex.getNotNull(klass);
        for (MetaMethodIndex.Entry e = header.head; e != null; e = e.nextClassEntry) {
            String methodName = e.name;
            // name too short?
            if (methodName.length() < 3 || (!methodName.startsWith("is") && methodName.length() < 4))
                continue;
            // possible getter/setter?
            boolean isGetter = methodName.startsWith("get") || methodName.startsWith("is");
            boolean isBooleanGetter = methodName.startsWith("is");
            boolean isSetter = methodName.startsWith("set");
            if (!isGetter && !isSetter)
                continue;
            Object propertyMethods = filterPropertyMethod(isThis ? e.methods : e.methodsForSuper, isGetter, isBooleanGetter);
            if (propertyMethods == null)
                continue;
            String propName = getPropName(methodName);
            if (propertyMethods instanceof MetaMethod) {
                createMetaBeanProperty(propertyIndex, propName, isGetter, (MetaMethod) propertyMethods);
            } else {
                LinkedList<MetaMethod> methods = (LinkedList<MetaMethod>) propertyMethods;
                for (MetaMethod m : methods) {
                    createMetaBeanProperty(propertyIndex, propName, isGetter, m);
                }
            }
        }
    }
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) 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) 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