Search in sources :

Example 31 with MetaMethod

use of groovy.lang.MetaMethod in project groovy by apache.

the class ClosureMetaMethod method createMethodList.

public static List<MetaMethod> createMethodList(final String name, final Class declaringClass, final Closure closure) {
    List<MetaMethod> res = new ArrayList<MetaMethod>();
    if (closure instanceof MethodClosure) {
        MethodClosure methodClosure = (MethodClosure) closure;
        Object owner = closure.getOwner();
        Class ownerClass = (Class) (owner instanceof Class ? owner : owner.getClass());
        for (CachedMethod method : ReflectionCache.getCachedClass(ownerClass).getMethods()) {
            if (method.getName().equals(methodClosure.getMethod())) {
                MetaMethod metaMethod = new MethodClosureMetaMethod(name, declaringClass, closure, method);
                res.add(adjustParamTypesForStdMethods(metaMethod, name));
            }
        }
    } else {
        if (closure instanceof GeneratedClosure) {
            for (CachedMethod method : ReflectionCache.getCachedClass(closure.getClass()).getMethods()) {
                if (method.getName().equals("doCall")) {
                    MetaMethod metaMethod = new ClosureMetaMethod(name, declaringClass, closure, method);
                    res.add(adjustParamTypesForStdMethods(metaMethod, name));
                }
            }
        } else {
            MetaMethod metaMethod = new AnonymousMetaMethod(closure, name, declaringClass);
            res.add(adjustParamTypesForStdMethods(metaMethod, name));
        }
    }
    return res;
}
Also used : MetaMethod(groovy.lang.MetaMethod) ArrayList(java.util.ArrayList) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) GeneratedClosure(org.codehaus.groovy.runtime.GeneratedClosure)

Example 32 with MetaMethod

use of groovy.lang.MetaMethod 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)

Example 33 with MetaMethod

use of groovy.lang.MetaMethod in project groovy by apache.

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 34 with MetaMethod

use of groovy.lang.MetaMethod in project groovy by apache.

the class MethodSelectionException method appendMethods.

private void appendMethods(StringBuilder buffer) {
    for (int i = 0; i < methods.size; i++) {
        buffer.append("\n  ");
        Object methodOrConstructor = methods.get(i);
        if (methodOrConstructor instanceof MetaMethod) {
            MetaMethod method = (MetaMethod) methodOrConstructor;
            buffer.append(Modifier.toString(method.getModifiers()));
            buffer.append(" ").append(method.getReturnType().getName());
            buffer.append(" ").append(method.getDeclaringClass().getName());
            buffer.append("#");
            buffer.append(method.getName());
            appendClassNames(buffer, method.getNativeParameterTypes());
        } else {
            CachedConstructor method = (CachedConstructor) methodOrConstructor;
            buffer.append(Modifier.toString(method.cachedConstructor.getModifiers()));
            buffer.append(" ").append(method.cachedConstructor.getDeclaringClass().getName());
            buffer.append("#<init>");
            appendClassNames(buffer, method.getNativeParameterTypes());
        }
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) CachedConstructor(org.codehaus.groovy.reflection.CachedConstructor)

Example 35 with MetaMethod

use of groovy.lang.MetaMethod in project groovy by apache.

the class MethodRankHelper method rankMethods.

/**
     * Returns a sorted(ranked) list of a selection of the methods among candidates which
     * most closely resembles original.
     *
     * @param name
     * @param original
     * @param methods
     * @return a sorted lists of Methods
     */
private static List<MetaMethod> rankMethods(String name, Object[] original, List<MetaMethod> methods) {
    List<RankableMethod> rm = new ArrayList<RankableMethod>(methods.size());
    if (original == null)
        original = EMPTY_OBJECT_ARRAY;
    Class[] ta = new Class[original.length];
    Class nullC = NullObject.class;
    for (int i = 0; i < original.length; i++) {
        //All nulls have to be wrapped so that they can be compared
        ta[i] = original[i] == null ? nullC : original[i].getClass();
    }
    for (MetaMethod m : methods) {
        rm.add(new RankableMethod(name, ta, m));
    }
    Collections.sort(rm);
    List<MetaMethod> l = new ArrayList<MetaMethod>(rm.size());
    for (RankableMethod m : rm) {
        if (l.size() > MAX_RECOMENDATIONS)
            break;
        if (m.score > MAX_METHOD_SCORE)
            break;
        l.add(m.m);
    }
    return l;
}
Also used : MetaMethod(groovy.lang.MetaMethod) ArrayList(java.util.ArrayList) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Aggregations

MetaMethod (groovy.lang.MetaMethod)35 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)14 CachedClass (org.codehaus.groovy.reflection.CachedClass)13 FastArray (org.codehaus.groovy.util.FastArray)12 ArrayList (java.util.ArrayList)8 MetaClass (groovy.lang.MetaClass)5 LinkedList (java.util.LinkedList)4 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)4 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)3 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)3 GroovyObject (groovy.lang.GroovyObject)2 MetaProperty (groovy.lang.MetaProperty)2 File (java.io.File)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashSet (java.util.HashSet)2 CachedConstructor (org.codehaus.groovy.reflection.CachedConstructor)2 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)2 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)2