All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudbees.groovy.cps.impl.ClosureBlock Maven / Gradle / Ivy

There is a newer version: 1.31
Show newest version
package com.cloudbees.groovy.cps.impl;

import com.cloudbees.groovy.cps.Block;
import com.cloudbees.groovy.cps.Continuation;
import com.cloudbees.groovy.cps.Env;
import com.cloudbees.groovy.cps.Next;

import java.lang.reflect.Constructor;
import java.util.List;

/**
 * Closure instantiation: { ... }
 *
 * @author Kohsuke Kawaguchi
 */
public class ClosureBlock implements Block {
    private final List parameters;
    private final List parameterTypes;
    private final Block body;
    // this field would be null if we are deserializing from data saved by old version
    private final Class closureType;
    private final SourceLocation loc;

    public ClosureBlock(SourceLocation loc, List parameterTypes, List parameters, Block body, Class closureType) {
        this.loc = loc;
        this.parameterTypes = parameterTypes;
        this.parameters = parameters;
        this.body = body;
        this.closureType = closureType;
    }

    private Class closureType() {
        if (closureType==null)  return CpsClosure.class; // backward compatibility with persisted form
        return closureType;
    }

    public Next eval(Env e, Continuation k) {
        try {
            Constructor c = closureType().getConstructor(
                Object.class, Object.class, List.class, Block.class, Env.class
            );
            CpsClosure closure = c.newInstance(e.closureOwner(), e.getLocalVariable("this"), parameters, body, e);
            if (parameterTypes!=null) { // backward compatibility with persisted form
                closure.setParameterTypes(parameterTypes);
            }
            return k.receive(closure);
        } catch (Exception x) {
            return new ContinuationGroup() {}.
                throwException(e, x, loc, new ReferenceStackTrace());
        }
    }

    private static final long serialVersionUID = 1L;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy