Example usage for java.lang.reflect Executable getAnnotation

List of usage examples for java.lang.reflect Executable getAnnotation

Introduction

In this page you can find the example usage for java.lang.reflect Executable getAnnotation.

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:sf.net.experimaestro.manager.scripting.GenericFunction.java

/**
 * Transform the arguments//w  ww.  ja  v  a 2 s  .c om
 *
 * @param cx          The script context
 * @param lcx
 * @param declaration
 * @param args
 * @param offset      The offset within the target parameters    @return
 */
static Object[] transform(LanguageContext lcx, ScriptContext cx, Declaration declaration, Object[] args,
        Function[] converters, int offset) {
    final Executable executable = declaration.executable();
    final Class<?>[] types = executable.getParameterTypes();
    Object methodArgs[] = new Object[types.length];

    // --- Add context and scope if needed
    Expose annotation = executable.getAnnotation(Expose.class);
    if (annotation != null && (annotation.scope() && annotation.context())) {
        throw new UnsupportedOperationException(
                "Annotations scope and context cannot be used at the same time");
    }

    if (annotation == null ? false : annotation.scope()) {
        JavaScriptContext jcx = (JavaScriptContext) lcx;
        methodArgs[0] = jcx.context();
        methodArgs[1] = jcx.scope();
    }

    if (annotation == null ? false : annotation.context()) {
        methodArgs[0] = lcx;
        methodArgs[1] = cx;
    }

    // --- Copy the non vararg parameters
    final int length = types.length - (executable.isVarArgs() ? 1 : 0) - offset;
    int size = min(length, args.length);
    for (int i = 0; i < size; i++) {
        methodArgs[i + offset] = converters[i].apply(args[i]);
    }

    // --- Deals with the vararg pararameters
    if (executable.isVarArgs()) {
        final Class<?> varargType = types[types.length - 1].getComponentType();
        int nbVarargs = args.length - length;
        final Object array[] = (Object[]) Array.newInstance(varargType, nbVarargs);
        for (int i = 0; i < nbVarargs; i++) {
            array[i] = converters[i + length].apply(args[i + length]);
        }
        methodArgs[methodArgs.length - 1] = array;
    }

    return methodArgs;
}