Search in sources :

Example 6 with MetaMethod

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

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

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

the class MetaMethodIndex method copyNonPrivateNonNewMetaMethods.

private void copyNonPrivateNonNewMetaMethods(Entry from, Header to) {
    Object oldListOrMethod = from.methods;
    if (oldListOrMethod == null)
        return;
    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 instanceof NewMetaMethod || method.isPrivate())
                continue;
            if (e == null)
                e = getOrPutMethods(from.name, to);
            e.methods = addMethodToList(e.methods, method);
        }
    } else {
        MetaMethod method = (MetaMethod) oldListOrMethod;
        if (method instanceof NewMetaMethod || method.isPrivate())
            return;
        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 8 with MetaMethod

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

the class MethodRankHelper method getMethodSuggestionString.

/**
     * Returns a string detailing possible solutions to a missing method
     * if no good solutions can be found a empty string is returned.
     *
     * @param methodName the name of the method that doesn't exist
     * @param type the class on which the method is invoked
     * @param arguments the arguments passed to the method
     * @return a string with probable solutions to the exception
     */
public static String getMethodSuggestionString(String methodName, Class type, Object[] arguments) {
    ClassInfo ci = ClassInfo.getClassInfo(type);
    List<MetaMethod> methods = new ArrayList<MetaMethod>(ci.getMetaClass().getMethods());
    methods.addAll(ci.getMetaClass().getMetaMethods());
    List<MetaMethod> sugg = rankMethods(methodName, arguments, methods);
    StringBuilder sb = new StringBuilder();
    if (!sugg.isEmpty()) {
        sb.append("\nPossible solutions: ");
        for (int i = 0; i < sugg.size(); i++) {
            if (i != 0)
                sb.append(", ");
            sb.append(sugg.get(i).getName()).append("(");
            sb.append(listParameterNames(sugg.get(i).getParameterTypes()));
            sb.append(")");
        }
    }
    Class[] argumentClasses = getArgumentClasses(arguments);
    List<Pair<Class, Class>> conflictClasses = getConflictClasses(sugg, argumentClasses);
    if (!conflictClasses.isEmpty()) {
        sb.append("\nThe following classes appear as argument class and as parameter class, ");
        sb.append("but are defined by different class loader:\n");
        boolean first = true;
        for (Pair<Class, Class> pair : conflictClasses) {
            if (!first) {
                sb.append(", ");
            } else {
                first = false;
            }
            sb.append(pair.u.getName()).append(" (defined by '");
            sb.append(pair.u.getClassLoader());
            sb.append("' and '");
            sb.append(pair.v.getClassLoader());
            sb.append("')");
        }
        sb.append("\nIf one of the method suggestions matches the method you wanted to call, ");
        sb.append("\nthen check your class loader setup.");
    }
    return sb.toString();
}
Also used : MetaMethod(groovy.lang.MetaMethod) ArrayList(java.util.ArrayList) CachedClass(org.codehaus.groovy.reflection.CachedClass) ClassInfo(org.codehaus.groovy.reflection.ClassInfo)

Example 9 with MetaMethod

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

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)

Example 10 with MetaMethod

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

the class SimpleExtensionModule method getMetaMethods.

@Override
public List<MetaMethod> getMetaMethods() {
    List<MetaMethod> metaMethods = new LinkedList<MetaMethod>();
    List<Class> extensionClasses = getInstanceMethodsExtensionClasses();
    for (Class extensionClass : extensionClasses) {
        try {
            createMetaMethods(extensionClass, metaMethods, false);
        } catch (LinkageError e) {
            LOG.warning("Module [" + getName() + "] - Unable to load extension class [" + extensionClass + "] due to [" + e.getMessage() + "]. Maybe this module is not supported by your JVM version.");
        }
    }
    extensionClasses = getStaticMethodsExtensionClasses();
    for (Class extensionClass : extensionClasses) {
        try {
            createMetaMethods(extensionClass, metaMethods, true);
        } catch (LinkageError e) {
            LOG.warning("Module [" + getName() + "] - Unable to load extension class [" + extensionClass + "] due to [" + e.getMessage() + "]. Maybe this module is not supported by your JVM version.");
        }
    }
    return metaMethods;
}
Also used : MetaMethod(groovy.lang.MetaMethod) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) LinkedList(java.util.LinkedList)

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