Java Code Examples for groovy.lang.Closure#clone()

The following examples show how to use groovy.lang.Closure#clone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamingJsonBuilder.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void cloneDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c, boolean first, JsonGenerator generator) {
    StreamingJsonDelegate delegate = new StreamingJsonDelegate(w, first, generator);
    Closure cloned = (Closure) c.clone();
    cloned.setDelegate(delegate);
    cloned.setResolveStrategy(Closure.DELEGATE_FIRST);
    cloned.call();
}
 
Example 2
Source File: JsonDelegate.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method for creating <code>JsonDelegate</code>s from closures.
 *
 * @param c closure representing JSON objects
 * @return an instance of <code>JsonDelegate</code>
 */
public static Map<String, Object> cloneDelegateAndGetContent(Closure<?> c) {
    JsonDelegate delegate = new JsonDelegate();
    Closure<?> cloned = (Closure<?>) c.clone();
    cloned.setDelegate(delegate);
    cloned.setResolveStrategy(Closure.DELEGATE_FIRST);
    cloned.call();

    return delegate.getContent();
}
 
Example 3
Source File: MatcherUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static <T> Closure<T> cloneWithDelegate(final Closure<T> predicate, final Object delegate) {
    Closure<T> clone = (Closure<T>) predicate.clone();
    clone.setDelegate(delegate);
    clone.setResolveStrategy(Closure.DELEGATE_FIRST);
    return clone;
}
 
Example 4
Source File: SecureASTCustomizerFactory.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onNodeChildren(final FactoryBuilderSupport builder, final Object node, final Closure childContent) {
    if (node instanceof SecureASTCustomizer) {
        Closure clone = (Closure) childContent.clone();
        clone.setDelegate(node);
        clone.setResolveStrategy(Closure.DELEGATE_FIRST);
        clone.call();
    }
    return false;
}
 
Example 5
Source File: ImportCustomizerFactory.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onNodeChildren(final FactoryBuilderSupport builder, final Object node, final Closure childContent) {
    if (node instanceof ImportCustomizer) {
        Closure clone = (Closure) childContent.clone();
        clone.setDelegate(new ImportHelper((ImportCustomizer) node));
        clone.call();
    }
    return false;
}
 
Example 6
Source File: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <R> R withTypeChecker(Closure<R> code) {
    Closure<R> clone = (Closure<R>) code.clone();
    clone.setDelegate(typeCheckingVisitor);
    clone.setResolveStrategy(Closure.DELEGATE_FIRST);
    return clone.call();
}
 
Example 7
Source File: CurriedClosure.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the curried closure.
 *
 * @param index the position where the parameters should be injected (-ve for lazy)
 * @param uncurriedClosure the closure to be called after the curried parameters are injected
 * @param arguments the supplied parameters
 */
public CurriedClosure(int index, Closure<V> uncurriedClosure, Object... arguments) {
    super(uncurriedClosure.clone());
    curriedParams = arguments;
    this.index = index;
    final int origMaxLen = uncurriedClosure.getMaximumNumberOfParameters();
    maximumNumberOfParameters = origMaxLen - arguments.length;
    Class[] classes = uncurriedClosure.getParameterTypes();
    Class lastType = classes.length == 0 ? null : classes[classes.length-1];
    if (lastType != null && lastType.isArray()) {
        varargType = lastType;
    }

    if (!isVararg()) {
        // perform some early param checking for non-vararg case
        if (index < 0) {
            // normalise
            this.index += origMaxLen;
            minParamsExpected = 0;
        } else {
            minParamsExpected = index + arguments.length;
        }
        if (maximumNumberOfParameters < 0) {
            throw new IllegalArgumentException("Can't curry " + arguments.length + " arguments for a closure with " + origMaxLen + " parameters.");
        }
        if (index < 0) {
            if (index < -origMaxLen || index > -arguments.length)
                throw new IllegalArgumentException("To curry " + arguments.length + " argument(s) expect index range " +
                        (-origMaxLen) + ".." + (-arguments.length) + " but found " + index);
        } else if (index > maximumNumberOfParameters) {
            throw new IllegalArgumentException("To curry " + arguments.length + " argument(s) expect index range 0.." +
                    maximumNumberOfParameters + " but found " + index);
        }
    } else {
        minParamsExpected = 0;
    }
}
 
Example 8
Source File: ComposedClosure.java    From groovy with Apache License 2.0 4 votes vote down vote up
public ComposedClosure(Closure first, Closure<V> second) {
    super(first.clone());
    this.first = (Closure) getOwner();
    this.second = (Closure<V>) second.clone();
    maximumNumberOfParameters = first.getMaximumNumberOfParameters();
}
 
Example 9
Source File: Builder.java    From groovy with Apache License 2.0 3 votes vote down vote up
public Built(final Closure root, final Map namespaceTagMap) {
    this.namespaceSpecificTags.putAll(namespaceTagMap);

    this.root = (Closure)root.clone();
    
    this.root.setDelegate(this);
}