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

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

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

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * As a crude way to distinguish asynchronous caller vs synchronous caller,
 * remember the method call about to happen so that the callee can check
 * if it is invoked by asynchronous caller or not.
 *
 * @author Kohsuke Kawaguchi
 */
public class Caller {
    /**
     * Caller information needs to be recorded per thread.
     */
    private static final ThreadLocal store = new ThreadLocal() {
        @Override
        protected Info initialValue() {
            return new Info();
        }
    };

    /**
     * Checks if the method is called from asynchronous CPS transformed code.
     *
     * 

* This method must be the first call in the function body. */ public static boolean isAsynchronous(Object receiver, String method, Object... args) { Info i = store.get(); return i.receiver != null && receiver==i.receiver.get() && method.equals(i.method) && arrayShallowEquals(i.args, args); } private static boolean arrayShallowEquals(Reference[] a, Object[] b) { if (a.length!=b.length) return false; for (int i=0; i receiver; private String method; private Reference[] args; } @SuppressWarnings({"unchecked", "rawtypes"}) // generic array creation static void record(Object receiver, String method, Object[] args) { Info c = store.get(); c.receiver = new WeakReference(receiver); c.method = method; c.args = new WeakReference[args.length]; for (int i = 0; i < args.length; i++) { c.args[i] = new WeakReference(args[i]); } } }