Search in sources :

Example 16 with CachedClass

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

the class MetaMethod method getMopName.

public String getMopName() {
    if (mopName == null) {
        String name = getName();
        CachedClass declaringClass = getDeclaringClass();
        if (Modifier.isPrivate(getModifiers()))
            mopName = new StringBuffer().append("this$").append(declaringClass.getSuperClassDistance()).append("$").append(name).toString();
        else
            mopName = new StringBuffer().append("super$").append(declaringClass.getSuperClassDistance()).append("$").append(name).toString();
    }
    return mopName;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 17 with CachedClass

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

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 18 with CachedClass

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

the class CallSiteGenerator method compilePogoMethod.

public static Constructor compilePogoMethod(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 = genPogoMetaMethodSite(cachedMethod, cw, name);
    return callSiteLoader.defineClassAndGetConstructor(name, bytes);
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) ClassWriter(org.objectweb.asm.ClassWriter)

Example 19 with CachedClass

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

the class MetaClassHelper method chooseMostGeneralMethodWith1NullParam.

/**
     * Warning: this method does not choose properly if multiple methods with
     * the same distance are encountered
     * @param methods the methods to choose from
     * @return the method with 1 parameter which takes the most general type of
     *         object (e.g. Object) ignoring primitive types
     * @deprecated
     */
public static Object chooseMostGeneralMethodWith1NullParam(FastArray methods) {
    // let's look for methods with 1 argument which matches the type of the
    // arguments
    CachedClass closestClass = null;
    CachedClass closestVargsClass = null;
    Object answer = null;
    int closestDist = -1;
    final int len = methods.size();
    for (int i = 0; i != len; ++i) {
        final Object[] data = methods.getArray();
        Object method = data[i];
        final ParameterTypes pt = (ParameterTypes) method;
        CachedClass[] paramTypes = pt.getParameterTypes();
        int paramLength = paramTypes.length;
        if (paramLength == 0 || paramLength > 2)
            continue;
        CachedClass theType = paramTypes[0];
        if (theType.isPrimitive)
            continue;
        if (paramLength == 2) {
            if (!pt.isVargsMethod(ARRAY_WITH_NULL))
                continue;
            if (closestClass == null) {
                closestVargsClass = paramTypes[1];
                closestClass = theType;
                answer = method;
            } else if (closestClass.getTheClass() == theType.getTheClass()) {
                if (closestVargsClass == null)
                    continue;
                CachedClass newVargsClass = paramTypes[1];
                if (isAssignableFrom(newVargsClass.getTheClass(), closestVargsClass.getTheClass())) {
                    closestVargsClass = newVargsClass;
                    answer = method;
                }
            } else if (isAssignableFrom(theType.getTheClass(), closestClass.getTheClass())) {
                closestVargsClass = paramTypes[1];
                closestClass = theType;
                answer = method;
            }
        } else {
            if (closestClass == null || isAssignableFrom(theType.getTheClass(), closestClass.getTheClass())) {
                closestVargsClass = null;
                closestClass = theType;
                answer = method;
                closestDist = -1;
            } else {
                // to check the distance to Object
                if (closestDist == -1)
                    closestDist = closestClass.getSuperClassDistance();
                int newDist = theType.getSuperClassDistance();
                if (newDist < closestDist) {
                    closestDist = newDist;
                    closestVargsClass = null;
                    closestClass = theType;
                    answer = method;
                }
            }
        }
    }
    return answer;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) ParameterTypes(org.codehaus.groovy.reflection.ParameterTypes)

Example 20 with CachedClass

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

the class MetaClassHelper method chooseEmptyMethodParams.

/**
     * @param methods the methods to choose from
     * @return the method with 1 parameter which takes the most general type of
     *         object (e.g. Object)
     */
public static Object chooseEmptyMethodParams(FastArray methods) {
    Object vargsMethod = null;
    final int len = methods.size();
    final Object[] data = methods.getArray();
    for (int i = 0; i != len; ++i) {
        Object method = data[i];
        final ParameterTypes pt = (ParameterTypes) method;
        CachedClass[] paramTypes = pt.getParameterTypes();
        int paramLength = paramTypes.length;
        if (paramLength == 0) {
            return method;
        } else if (paramLength == 1 && pt.isVargsMethod(EMPTY_ARRAY)) {
            vargsMethod = method;
        }
    }
    return vargsMethod;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) ParameterTypes(org.codehaus.groovy.reflection.ParameterTypes)

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