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

geb.transform.implicitassertions.Runtime Maven / Gradle / Ivy

Go to download

Geb (pronounced "jeb") implicit assertion transform (used in conjunction with geb-core).

The newest version!
package geb.transform.implicitassertions;

import groovy.lang.MetaClass;
import groovy.lang.MetaMethod;
import org.codehaus.groovy.runtime.InvokerHelper;

public abstract class Runtime {

    private static ThreadLocal recordedValue = new ThreadLocal();

    @SuppressWarnings("UnusedDeclaration")
    public static boolean isVoidMethod(Object target, String method, Object... args) {

        if (target == null) {
            return false;
        }

        Class[] argTypes = new Class[args.length];
        int i = 0;
        for (Object arg : args) {
            argTypes[i++] = arg == null ? null : arg.getClass();
        }

        MetaClass metaClass = target instanceof Class ? InvokerHelper.getMetaClass((Class) target) : InvokerHelper.getMetaClass(target);

        MetaMethod metaMethod = metaClass.pickMethod(method, argTypes);
        if (metaMethod == null) {
            return false;
        }

        Class returnType = metaMethod.getReturnType();
        return returnType == void.class || returnType == Void.class;
    }

    @SuppressWarnings("UnusedDeclaration")
    public static Object recordValue(Object value) {
        recordedValue.set(value);
        return value;
    }

    @SuppressWarnings("UnusedDeclaration")
    public static Object retrieveRecordedValue() {
        Object value = recordedValue.get();
        recordedValue.set(null);
        return value;
    }

}