com.cloudbees.groovy.cps.impl.Caller Maven / Gradle / Ivy
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