Search in sources :

Example 6 with BeanDynamicObject

use of org.gradle.internal.metaobject.BeanDynamicObject in project gradle by gradle.

the class MixInClosurePropertiesAsMethodsDynamicObject method tryInvokeMethod.

@Override
public DynamicInvokeResult tryInvokeMethod(String name, Object... arguments) {
    DynamicInvokeResult result = super.tryInvokeMethod(name, arguments);
    if (result.isFound()) {
        return result;
    }
    DynamicInvokeResult propertyResult = tryGetProperty(name);
    if (propertyResult.isFound()) {
        Object property = propertyResult.getValue();
        if (property instanceof Closure) {
            Closure closure = (Closure) property;
            closure.setResolveStrategy(Closure.DELEGATE_FIRST);
            BeanDynamicObject dynamicObject = new BeanDynamicObject(closure);
            result = dynamicObject.tryInvokeMethod("doCall", arguments);
            if (!result.isFound() && !(closure instanceof GeneratedClosure)) {
                return DynamicInvokeResult.found(closure.call(arguments));
            }
            return result;
        }
        if (property instanceof NamedDomainObjectContainer && arguments.length == 1 && arguments[0] instanceof Closure) {
            ((NamedDomainObjectContainer) property).configure((Closure) arguments[0]);
            return DynamicInvokeResult.found();
        }
    }
    return DynamicInvokeResult.notFound();
}
Also used : NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) DynamicInvokeResult(org.gradle.internal.metaobject.DynamicInvokeResult) GeneratedClosure(org.codehaus.groovy.runtime.GeneratedClosure) Closure(groovy.lang.Closure) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) CompositeDynamicObject(org.gradle.internal.metaobject.CompositeDynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) GeneratedClosure(org.codehaus.groovy.runtime.GeneratedClosure)

Aggregations

BeanDynamicObject (org.gradle.internal.metaobject.BeanDynamicObject)6 DynamicObject (org.gradle.internal.metaobject.DynamicObject)5 Closure (groovy.lang.Closure)4 File (java.io.File)2 Test (org.junit.Test)2 Predicate (com.google.common.base.Predicate)1 Script (groovy.lang.Script)1 GeneratedClosure (org.codehaus.groovy.runtime.GeneratedClosure)1 NamedDomainObjectContainer (org.gradle.api.NamedDomainObjectContainer)1 DirectoryTree (org.gradle.api.file.DirectoryTree)1 CompositeDynamicObject (org.gradle.internal.metaobject.CompositeDynamicObject)1 DynamicInvokeResult (org.gradle.internal.metaobject.DynamicInvokeResult)1