Search in sources :

Example 1 with BeanDynamicObject

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

the class ExtensibleDynamicObjectTest method canInvokeMethodDefinedByScriptObject.

@Test
public void canInvokeMethodDefinedByScriptObject() {
    Bean bean = new Bean();
    Script script = TestUtil.createScript("def scriptMethod(a, b) { \"script:$a.$b\" } ");
    bean.extensibleDynamicObject.addObject(new BeanDynamicObject(script), ExtensibleDynamicObject.Location.BeforeConvention);
    assertTrue(bean.hasMethod("scriptMethod", "a", "b"));
    assertThat(bean.getAsDynamicObject().invokeMethod("scriptMethod", "a", "b").toString(), equalTo((Object) "script:a.b"));
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Test(org.junit.Test)

Example 2 with BeanDynamicObject

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

the class AntFileCollectionMatchingTaskBuilder method addToAntBuilder.

@Override
public Object addToAntBuilder(final Object node, final String childNodeName) {
    final DynamicObject dynamicObject = new BeanDynamicObject(node);
    final Iterable<DirectoryTree> existing = Lists.newLinkedList(FluentIterable.from(fileTrees).filter(new Predicate<DirectoryTree>() {

        @Override
        public boolean apply(DirectoryTree input) {
            return input.getDir().exists();
        }
    }));
    for (DirectoryTree fileTree : existing) {
        dynamicObject.invokeMethod(childNodeName, Collections.singletonMap("location", fileTree.getDir()));
    }
    dynamicObject.invokeMethod("or", new Closure<Void>(this) {

        public Object doCall(Object ignore) {
            for (final DirectoryTree fileTree : existing) {
                dynamicObject.invokeMethod("and", new Closure<Void>(this) {

                    public Object doCall(Object ignore) {
                        dynamicObject.invokeMethod("gradleBaseDirSelector", Collections.singletonMap("baseDir", fileTree.getDir()));
                        fileTree.getPatterns().addToAntBuilder(node, null);
                        return null;
                    }
                });
            }
            return null;
        }
    });
    return node;
}
Also used : DirectoryTree(org.gradle.api.file.DirectoryTree) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Closure(groovy.lang.Closure) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Predicate(com.google.common.base.Predicate)

Example 3 with BeanDynamicObject

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

the class AntFileTreeBuilder method addToAntBuilder.

@Override
public Object addToAntBuilder(Object node, String childNodeName) {
    final DynamicObject dynamicObject = new BeanDynamicObject(node);
    dynamicObject.invokeMethod(childNodeName == null ? "resources" : childNodeName, new Closure(this) {

        public Object doCall(Object ignore) {
            for (Map.Entry<String, File> entry : files.entrySet()) {
                String name = entry.getKey();
                File file = entry.getValue();
                // gradleFileResource type is mapped to AntFileResource
                dynamicObject.invokeMethod("gradleFileResource", ImmutableMap.of("file", file.getAbsolutePath(), "name", name));
            }
            return null;
        }
    });
    return node;
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Closure(groovy.lang.Closure) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) File(java.io.File)

Example 4 with BeanDynamicObject

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

the class AntFileCollectionBuilder method addToAntBuilder.

@Override
public Object addToAntBuilder(Object node, String childNodeName) {
    final DynamicObject dynamicObject = new BeanDynamicObject(node);
    dynamicObject.invokeMethod(childNodeName == null ? "resources" : childNodeName, new Closure(this) {

        public Object doCall(Object ignore) {
            for (File file : files) {
                dynamicObject.invokeMethod("file", Collections.singletonMap("file", AntUtil.maskFilename(file.getAbsolutePath())));
            }
            return null;
        }
    });
    return node;
}
Also used : DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Closure(groovy.lang.Closure) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) File(java.io.File)

Example 5 with BeanDynamicObject

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

the class ExtensibleDynamicObjectTest method canInvokeMethodDefinedByScriptObject.

@Test
public void canInvokeMethodDefinedByScriptObject() {
    Bean bean = new Bean();
    Script script = TestUtil.createScript("def scriptMethod(a, b) { \"script:$a.$b\" } ");
    bean.extensibleDynamicObject.addObject(new BeanDynamicObject(script), ExtensibleDynamicObject.Location.BeforeConvention);
    assertTrue(bean.hasMethod("scriptMethod", "a", "b"));
    assertThat(bean.getAsDynamicObject().invokeMethod("scriptMethod", "a", "b").toString(), equalTo((Object) "script:a.b"));
}
Also used : Script(groovy.lang.Script) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) DynamicObject(org.gradle.internal.metaobject.DynamicObject) BeanDynamicObject(org.gradle.internal.metaobject.BeanDynamicObject) Test(org.junit.Test)

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