Search in sources :

Example 1 with DynamicInvokeResult

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

the class ConfigureUtil method configureByMap.

public static <T> T configureByMap(Map<?, ?> properties, T delegate) {
    if (properties.isEmpty()) {
        return delegate;
    }
    DynamicObject dynamicObject = DynamicObjectUtil.asDynamicObject(delegate);
    for (Map.Entry<?, ?> entry : properties.entrySet()) {
        String name = entry.getKey().toString();
        Object value = entry.getValue();
        DynamicInvokeResult result = dynamicObject.trySetProperty(name, value);
        if (result.isFound()) {
            continue;
        }
        result = dynamicObject.tryInvokeMethod(name, value);
        if (!result.isFound()) {
            throw dynamicObject.setMissingProperty(name);
        }
    }
    return delegate;
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) DynamicInvokeResult(org.gradle.internal.metaobject.DynamicInvokeResult) DynamicObject(org.gradle.internal.metaobject.DynamicObject) Map(java.util.Map)

Example 2 with DynamicInvokeResult

use of org.gradle.internal.metaobject.DynamicInvokeResult 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)

Example 3 with DynamicInvokeResult

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

the class ConfigureUtil method configureByMap.

public static <T> T configureByMap(Map<?, ?> properties, T delegate) {
    if (properties.isEmpty()) {
        return delegate;
    }
    DynamicObject dynamicObject = DynamicObjectUtil.asDynamicObject(delegate);
    for (Map.Entry<?, ?> entry : properties.entrySet()) {
        String name = entry.getKey().toString();
        Object value = entry.getValue();
        DynamicInvokeResult result = dynamicObject.trySetProperty(name, value);
        if (result.isFound()) {
            continue;
        }
        result = dynamicObject.tryInvokeMethod(name, value);
        if (!result.isFound()) {
            throw dynamicObject.setMissingProperty(name);
        }
    }
    return delegate;
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) DynamicInvokeResult(org.gradle.internal.metaobject.DynamicInvokeResult) DynamicObject(org.gradle.internal.metaobject.DynamicObject) Map(java.util.Map)

Aggregations

DynamicInvokeResult (org.gradle.internal.metaobject.DynamicInvokeResult)3 Map (java.util.Map)2 DynamicObject (org.gradle.internal.metaobject.DynamicObject)2 Closure (groovy.lang.Closure)1 GeneratedClosure (org.codehaus.groovy.runtime.GeneratedClosure)1 NamedDomainObjectContainer (org.gradle.api.NamedDomainObjectContainer)1 BeanDynamicObject (org.gradle.internal.metaobject.BeanDynamicObject)1 CompositeDynamicObject (org.gradle.internal.metaobject.CompositeDynamicObject)1