Search in sources :

Example 26 with Closure

use of groovy.lang.Closure in project gradle by gradle.

the class CompositeFileTreeTest method visitsEachTreeWithClosure.

@Test
public void visitsEachTreeWithClosure() {
    final Closure visitor = TestUtil.TEST_CLOSURE;
    final FileVisitor closureAsVisitor = DefaultGroovyMethods.asType(visitor, FileVisitor.class);
    context.checking(new Expectations() {

        {
            oneOf(source1).visit(closureAsVisitor);
            oneOf(source2).visit(closureAsVisitor);
        }
    });
    tree.visit(visitor);
}
Also used : Expectations(org.jmock.Expectations) Closure(groovy.lang.Closure) FileVisitor(org.gradle.api.file.FileVisitor) Test(org.junit.Test)

Example 27 with Closure

use of groovy.lang.Closure in project gradle by gradle.

the class AntJacocoCheck method configureReport.

@Override
protected void configureReport(final GroovyObjectSupport antBuilder, final JacocoViolationRulesContainer violationRules) {
    if (!violationRules.getRules().isEmpty()) {
        Map<String, Object> checkArgs = ImmutableMap.<String, Object>of("failonviolation", violationRules.isFailOnViolation(), "violationsproperty", VIOLATIONS_ANT_PROPERTY);
        antBuilder.invokeMethod("check", new Object[] { checkArgs, new Closure<Object>(this, this) {

            @SuppressWarnings("UnusedDeclaration")
            public Object doCall(Object ignore) {
                for (final JacocoViolationRule rule : filter(violationRules.getRules(), RULE_ENABLED_PREDICATE)) {
                    Map<String, Object> ruleArgs = ImmutableMap.<String, Object>of("element", rule.getElement(), "includes", Joiner.on(':').join(rule.getIncludes()), "excludes", Joiner.on(':').join(rule.getExcludes()));
                    antBuilder.invokeMethod("rule", new Object[] { ruleArgs, new Closure<Object>(this, this) {

                        @SuppressWarnings("UnusedDeclaration")
                        public Object doCall(Object ignore) {
                            for (JacocoLimit limit : rule.getLimits()) {
                                Map<String, Object> limitArgs = new HashMap<String, Object>();
                                limitArgs.put("counter", limit.getCounter());
                                limitArgs.put("value", limit.getValue());
                                if (limit.getMinimum() != null) {
                                    limitArgs.put("minimum", limit.getMinimum());
                                }
                                if (limit.getMaximum() != null) {
                                    limitArgs.put("maximum", limit.getMaximum());
                                }
                                antBuilder.invokeMethod("limit", new Object[] { ImmutableMap.copyOf(limitArgs) });
                            }
                            return null;
                        }
                    } });
                }
                return null;
            }
        } });
    }
}
Also used : JacocoLimit(org.gradle.testing.jacoco.tasks.rules.JacocoLimit) Closure(groovy.lang.Closure) HashMap(java.util.HashMap) JacocoViolationRule(org.gradle.testing.jacoco.tasks.rules.JacocoViolationRule) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with Closure

use of groovy.lang.Closure in project gradle by gradle.

the class TestFile method copyFrom.

public void copyFrom(final URL resource) {
    final TestFile testFile = this;
    RetryUtil.retry(new Closure(null, null) {

        @SuppressWarnings("UnusedDeclaration")
        void doCall() {
            try {
                FileUtils.copyURLToFile(resource, testFile);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    });
}
Also used : Closure(groovy.lang.Closure) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException)

Example 29 with Closure

use of groovy.lang.Closure in project grails-core by grails.

the class JspInvokeGrailsTagLibTag method doStartTagInternal.

@SuppressWarnings("rawtypes")
protected int doStartTagInternal() {
    GroovyObject tagLib = getTagLib(getTagName());
    if (tagLib == null) {
        throw new GrailsTagException("Tag [" + getTagName() + "] does not exist. No tag library found.");
    }
    sw = FastStringPrintWriter.newInstance();
    out = sw;
    tagLib.setProperty(OUT_PROPERTY, out);
    Object tagLibProp;
    final Map tagLibProperties = DefaultGroovyMethods.getProperties(tagLib);
    if (tagLibProperties.containsKey(getTagName())) {
        tagLibProp = tagLibProperties.get(getTagName());
    } else {
        throw new GrailsTagException("Tag [" + getTagName() + "] does not exist in tag library [" + tagLib.getClass().getName() + "]");
    }
    if (!(tagLibProp instanceof Closure)) {
        throw new GrailsTagException("Tag [" + getTagName() + "] does not exist in tag library [" + tagLib.getClass().getName() + "]");
    }
    Closure body = new Closure(this) {

        private static final long serialVersionUID = 1861498565854341886L;

        @SuppressWarnings("unused")
        public Object doCall() {
            return call();
        }

        @SuppressWarnings("unused")
        public Object doCall(Object o) {
            return call(new Object[] { o });
        }

        @SuppressWarnings("unused")
        public Object doCall(Object[] args) {
            return call(args);
        }

        @Override
        public Object call(Object... args) {
            invocationCount++;
            if (args.length > 0) {
                invocationArgs.add(args[0]);
            } else {
                invocationArgs.add(ZERO_ARGUMENTS);
            }
            out.print("<jsp-body-gen" + invocationCount + ">");
            return "";
        }
    };
    Closure tag = (Closure) tagLibProp;
    if (tag.getParameterTypes().length == 1) {
        tag.call(new Object[] { attributes });
        if (body != null) {
            body.call();
        }
    }
    if (tag.getParameterTypes().length == 2) {
        tag.call(new Object[] { attributes, body });
    }
    Collections.reverse(invocationArgs);
    setCurrentArgument();
    return EVAL_BODY_BUFFERED;
}
Also used : GrailsTagException(org.grails.taglib.GrailsTagException) Closure(groovy.lang.Closure) GroovyObject(groovy.lang.GroovyObject) HashMap(java.util.HashMap) Map(java.util.Map) GroovyObject(groovy.lang.GroovyObject)

Example 30 with Closure

use of groovy.lang.Closure in project gradle by gradle.

the class TaskFactory method createTask.

public TaskInternal createTask(Map<String, ?> args) {
    Map<String, Object> actualArgs = new HashMap<String, Object>(args);
    checkTaskArgsAndCreateDefaultValues(actualArgs);
    String name = actualArgs.get(Task.TASK_NAME).toString();
    if (!GUtil.isTrue(name)) {
        throw new InvalidUserDataException("The task name must be provided.");
    }
    Class<? extends TaskInternal> type = (Class) actualArgs.get(Task.TASK_TYPE);
    TaskInternal task = create(name, type);
    Object dependsOnTasks = actualArgs.get(Task.TASK_DEPENDS_ON);
    if (dependsOnTasks != null) {
        task.dependsOn(dependsOnTasks);
    }
    Object description = actualArgs.get(Task.TASK_DESCRIPTION);
    if (description != null) {
        task.setDescription(description.toString());
    }
    Object group = actualArgs.get(Task.TASK_GROUP);
    if (group != null) {
        task.setGroup(group.toString());
    }
    Object action = actualArgs.get(Task.TASK_ACTION);
    if (action instanceof Action) {
        Action<? super Task> taskAction = (Action<? super Task>) action;
        task.doFirst(taskAction);
    } else if (action != null) {
        Closure closure = (Closure) action;
        task.doFirst(closure);
    }
    return task;
}
Also used : Action(org.gradle.api.Action) AbstractTask(org.gradle.api.internal.AbstractTask) Task(org.gradle.api.Task) DefaultTask(org.gradle.api.DefaultTask) Closure(groovy.lang.Closure) HashMap(java.util.HashMap) InvalidUserDataException(org.gradle.api.InvalidUserDataException) TaskInternal(org.gradle.api.internal.TaskInternal)

Aggregations

Closure (groovy.lang.Closure)211 Map (java.util.Map)40 GroovyObject (groovy.lang.GroovyObject)27 HashMap (java.util.HashMap)27 Binding (groovy.lang.Binding)21 ArrayList (java.util.ArrayList)21 List (java.util.List)19 GString (groovy.lang.GString)17 GroovyShell (groovy.lang.GroovyShell)13 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)13 LinkedHashMap (java.util.LinkedHashMap)12 Test (org.junit.Test)12 File (java.io.File)10 LinkedList (java.util.LinkedList)10 FileType (groovy.io.FileType)8 FileVisitResult (groovy.io.FileVisitResult)8 MetaClass (groovy.lang.MetaClass)8 Collection (java.util.Collection)8 Script (groovy.lang.Script)7 IOException (java.io.IOException)7