Search in sources :

Example 16 with MetaMethod

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

use of groovy.lang.MetaMethod in project groovy-core by groovy.

the class MetaMethodIndex method copyNonPrivateMethodsFromSuper.

private void copyNonPrivateMethodsFromSuper(Entry e) {
    Object oldListOrMethod = e.methodsForSuper;
    if (oldListOrMethod == null)
        return;
    if (oldListOrMethod instanceof FastArray) {
        FastArray oldList = (FastArray) oldListOrMethod;
        int len1 = oldList.size();
        Object[] list = oldList.getArray();
        for (int j = 0; j != len1; ++j) {
            MetaMethod method = (MetaMethod) list[j];
            if (method.isPrivate())
                continue;
            e.methods = addMethodToList(e.methods, method);
        }
    } else {
        MetaMethod method = (MetaMethod) oldListOrMethod;
        if (!method.isPrivate()) {
            e.methods = addMethodToList(e.methods, method);
        }
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) FastArray(org.codehaus.groovy.util.FastArray)

Example 18 with MetaMethod

use of groovy.lang.MetaMethod in project groovy-core by groovy.

the class MetaMethodIndex method copyAllMethods.

private void copyAllMethods(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.methods = addMethodToList(e.methods, method);
        }
    } else {
        MetaMethod method = (MetaMethod) oldListOrMethod;
        if (!method.isPrivate()) {
            Entry e = getOrPutMethods(from.name, to);
            e.methods = addMethodToList(e.methods, method);
        }
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) FastArray(org.codehaus.groovy.util.FastArray)

Example 19 with MetaMethod

use of groovy.lang.MetaMethod in project groovy-core by groovy.

the class MetaMethodIndex method copyNonPrivateMethods.

private void copyNonPrivateMethods(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 (method.isPrivate())
                continue;
            if (e == null)
                e = getOrPutMethods(from.name, to);
            e.methods = addMethodToList(e.methods, method);
        }
    } else {
        MetaMethod method = (MetaMethod) oldListOrMethod;
        if (!method.isPrivate()) {
            Entry e = getOrPutMethods(from.name, to);
            e.methods = addMethodToList(e.methods, method);
        }
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) FastArray(org.codehaus.groovy.util.FastArray)

Example 20 with MetaMethod

use of groovy.lang.MetaMethod in project groovy-core by groovy.

the class MethodRankHelper method getConflictClasses.

private static List<Pair<Class, Class>> getConflictClasses(List<MetaMethod> sugg, Class[] argumentClasses) {
    List<Pair<Class, Class>> ret = new LinkedList<Pair<Class, Class>>();
    Set<Class> recordedClasses = new HashSet<Class>();
    for (MetaMethod method : sugg) {
        Class[] para = method.getNativeParameterTypes();
        for (Class aPara : para) {
            if (recordedClasses.contains(aPara))
                continue;
            for (Class argumentClass : argumentClasses) {
                if (argumentClass == null)
                    continue;
                if (argumentClass == aPara)
                    continue;
                if (argumentClass.getName().equals(aPara.getName())) {
                    ret.add(new Pair<Class, Class>(argumentClass, aPara));
                }
            }
            recordedClasses.add(aPara);
        }
    }
    return ret;
}
Also used : MetaMethod(groovy.lang.MetaMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

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