Search in sources :

Example 6 with MetaClassRegistryImpl

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

the class InvokerHelper method setProperty.

public static void setProperty(Object object, String property, Object newValue) {
    if (object == null) {
        object = NullObject.getNullObject();
    }
    if (object instanceof GroovyObject) {
        GroovyObject pogo = (GroovyObject) object;
        pogo.setProperty(property, newValue);
    } else if (object instanceof Class) {
        metaRegistry.getMetaClass((Class) object).setProperty((Class) object, property, newValue);
    } else {
        ((MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry()).getMetaClass(object).setProperty(object, property, newValue);
    }
}
Also used : MetaClassRegistryImpl(org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 7 with MetaClassRegistryImpl

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

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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap)

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