Example usage for java.lang.reflect Executable isVarArgs

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

Introduction

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

Prototype

public boolean isVarArgs() 

Source Link

Document

Returns true if this executable was declared to take a variable number of arguments; returns false otherwise.

Usage

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

/**
 * Transform the arguments//  www  .ja  v a2 s .  co  m
 *
 * @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;
}

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

/**
 * Gives a score to a given declaration//w  ww .java2 s  .  c om
 *
 * @param cx          The script context
 * @param declaration The underlying method or constructor
 * @param args        The arguments
 * @param converters  A list of converters that will be filled by this method
 * @param offset      The offset for the converters
 * @return A score (minimum integer if no conversion is possible)
 */
static int score(LanguageContext lcx, ScriptContext cx, Declaration declaration, Object[] args,
        Function[] converters, MutableInt offset) {

    final Executable executable = declaration.executable();
    final Class<?>[] types = executable.getParameterTypes();
    final boolean isVarArgs = executable.isVarArgs();

    // Get the annotations
    Expose annotation = declaration.executable.getAnnotation(Expose.class);
    final boolean contextAnnotation = annotation == null ? false : annotation.context();
    final boolean scopeAnnotation = annotation == null ? false : annotation.scope();
    int optional = annotation == null ? 0 : annotation.optional();

    // Start the scoring
    Converter converter = new Converter();

    // Offset in the types
    offset.setValue(contextAnnotation || scopeAnnotation ? 2 : 0);

    // Number of "true" arguments (not scope, not vararg)
    final int nbArgs = types.length - offset.intValue() - (isVarArgs ? 1 : 0);

    // The number of arguments should be in:
    // [nbArgs - optional, ...] if varargs
    // [nbArgs - optional, nbArgs] otherwise

    if (args.length < nbArgs - optional)
        return Integer.MIN_VALUE;

    if (!isVarArgs && args.length > nbArgs)
        return Integer.MIN_VALUE;

    // If the optional arguments are at the beginning, then shift
    if (annotation != null && annotation.optionalsAtStart()) {
        offset.add(max(nbArgs - args.length, 0));
    }

    // Normal arguments
    for (int i = 0; i < args.length && i < nbArgs && converter.isOK(); i++) {
        final Object o = args[i];
        converters[i] = converter.converter(lcx, cx, o, types[i + offset.intValue()]);
    }

    // Var args
    if (isVarArgs) {
        Class<?> type = ClassUtils.primitiveToWrapper(types[types.length - 1].getComponentType());
        int nbVarArgs = args.length - nbArgs;
        for (int i = 0; i < nbVarArgs && converter.isOK(); i++) {
            final Object o = args[nbArgs + i];
            converters[nbArgs + i] = converter.converter(lcx, cx, o, type);
        }
    }

    return converter.score;
}