Search in sources :

Example 1 with MetaClassRegistryImpl

use of org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl in project groovy by apache.

the class TestDgmConverter method testRegistry.

public void testRegistry() {
    final MetaClassRegistryImpl metaClassRegistry = new MetaClassRegistryImpl();
    final Object[] instanceMethods = metaClassRegistry.getInstanceMethods().getArray();
    assertTrue(instanceMethods.length > 0);
}
Also used : MetaClassRegistryImpl(org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl)

Example 2 with MetaClassRegistryImpl

use of org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl 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 3 with MetaClassRegistryImpl

use of org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl in project groovy-core by groovy.

the class MetaClassImpl method invokePropertyOrMissing.

private Object invokePropertyOrMissing(Object object, String methodName, Object[] originalArguments, boolean fromInsideClass, boolean isCallToSuper) {
    // if no method was found, try to find a closure defined as a field of the class and run it
    Object value = null;
    final MetaProperty metaProperty = this.getMetaProperty(methodName, false);
    if (metaProperty != null)
        value = metaProperty.getProperty(object);
    else {
        if (object instanceof Map)
            value = ((Map) object).get(methodName);
    }
    if (value instanceof Closure) {
        // This test ensures that value != this If you ever change this ensure that value != this
        Closure closure = (Closure) value;
        MetaClass delegateMetaClass = closure.getMetaClass();
        return delegateMetaClass.invokeMethod(closure.getClass(), closure, CLOSURE_DO_CALL_METHOD, originalArguments, false, fromInsideClass);
    }
    if (object instanceof Script) {
        Object bindingVar = ((Script) object).getBinding().getVariables().get(methodName);
        if (bindingVar != null) {
            MetaClass bindingVarMC = ((MetaClassRegistryImpl) registry).getMetaClass(bindingVar);
            return bindingVarMC.invokeMethod(bindingVar, CLOSURE_CALL_METHOD, originalArguments);
        }
    }
    return invokeMissingMethod(object, methodName, originalArguments, null, isCallToSuper);
}
Also used : MetaClassRegistryImpl(org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) GeneratedClosure(org.codehaus.groovy.runtime.GeneratedClosure) CurriedClosure(org.codehaus.groovy.runtime.CurriedClosure) ConvertedClosure(org.codehaus.groovy.runtime.ConvertedClosure) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap)

Example 4 with MetaClassRegistryImpl

use of org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl in project groovy-core by groovy.

the class TestDgmConverter method testRegistry.

public void testRegistry() {
    final MetaClassRegistryImpl metaClassRegistry = new MetaClassRegistryImpl();
    final Object[] instanceMethods = metaClassRegistry.getInstanceMethods().getArray();
    for (int i = 0; i < instanceMethods.length; i++) {
        System.out.println(instanceMethods[i]);
    }
}
Also used : MetaClassRegistryImpl(org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl)

Example 5 with MetaClassRegistryImpl

use of org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl in project grails-core by grails.

the class MetaClassRegistryCleaner method clean.

public synchronized void clean() {
    try {
        cleaning = true;
        MetaClassRegistryImpl registry = (MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry();
        cleanMetaClassOfClass(registry);
        cleanMetaClassOfInstance(registry);
    } finally {
        cleaning = false;
    }
}
Also used : MetaClassRegistryImpl(org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl)

Aggregations

MetaClassRegistryImpl (org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl)7 ConvertedClosure (org.codehaus.groovy.runtime.ConvertedClosure)2 CurriedClosure (org.codehaus.groovy.runtime.CurriedClosure)2 GeneratedClosure (org.codehaus.groovy.runtime.GeneratedClosure)2 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)2 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)2 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)2 ComplexKeyHashMap (org.codehaus.groovy.util.ComplexKeyHashMap)2 SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)2 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 GroovyObject (groovy.lang.GroovyObject)1 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)1 MetaClass (groovy.lang.MetaClass)1 MetaMethod (groovy.lang.MetaMethod)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CachedClass (org.codehaus.groovy.reflection.CachedClass)1