Search in sources :

Example 11 with MetaMethod

use of groovy.lang.MetaMethod in project spock by spockframework.

the class AddSlotFactory method create.

public ISlot create(Object owner, Type ownerType, String name) {
    String addMethodName = GroovyRuntimeUtil.propertyToMethodName("add", name);
    MetaMethod addMethod = GroovyRuntimeUtil.getMetaClass(owner).pickMethod(addMethodName, new Class[] { Object.class });
    return addMethod != null ? new SetterLikeSlot(owner, ownerType, addMethod) : null;
}
Also used : MetaMethod(groovy.lang.MetaMethod)

Example 12 with MetaMethod

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

the class GrailsMetaClassUtils method invokeMethodIfExists.

/**
     * Invokes a method if it exists otherwise returns null
     *
     * @param instance The instance
     * @param methodName The method name
     * @param args The arguments
     *
     * @return The result of the method call or null
     */
public static Object invokeMethodIfExists(Object instance, String methodName, Object[] args) {
    MetaClass metaClass = getMetaClass(instance);
    List<MetaMethod> methodList = metaClass.respondsTo(instance, methodName, args);
    if (methodList != null && !methodList.isEmpty()) {
        return metaClass.invokeMethod(instance, methodName, args);
    }
    return null;
}
Also used : MetaMethod(groovy.lang.MetaMethod) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass)

Example 13 with MetaMethod

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

the class DefaultGrailsApplication method initialiseGroovyExtensionModules.

protected static void initialiseGroovyExtensionModules() {
    if (extensionMethodsInitialized)
        return;
    extensionMethodsInitialized = true;
    Map<CachedClass, List<MetaMethod>> map = new HashMap<CachedClass, List<MetaMethod>>();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {
        Enumeration<URL> resources = classLoader.getResources(ExtensionModuleScanner.MODULE_META_INF_FILE);
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            if (url.getPath().contains("groovy-all")) {
                // already registered
                continue;
            }
            Properties properties = new Properties();
            InputStream inStream = null;
            try {
                inStream = url.openStream();
                properties.load(inStream);
                ((MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry()).registerExtensionModuleFromProperties(properties, classLoader, map);
            } catch (IOException e) {
                throw new GroovyRuntimeException("Unable to load module META-INF descriptor", e);
            } finally {
                if (inStream != null) {
                    inStream.close();
                }
            }
        }
    } catch (IOException ignored) {
    }
    for (Map.Entry<CachedClass, List<MetaMethod>> moduleMethods : map.entrySet()) {
        CachedClass cls = moduleMethods.getKey();
        cls.addNewMopMethods(moduleMethods.getValue());
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) InputStream(java.io.InputStream) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) CachedClass(org.codehaus.groovy.reflection.CachedClass) IOException(java.io.IOException) URL(java.net.URL) MetaClassRegistryImpl(org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl) GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 14 with MetaMethod

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

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

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

the class MetaMethodIndex method findMatchingMethod.

private int findMatchingMethod(FastArray list, MetaMethod method) {
    int len = list.size();
    Object[] data = list.getArray();
    for (int j = 0; j != len; ++j) {
        MetaMethod aMethod = (MetaMethod) data[j];
        if (isMatchingMethod(aMethod, method))
            return j;
    }
    return -1;
}
Also used : MetaMethod(groovy.lang.MetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod)

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