Example usage for java.lang.reflect Executable getParameterTypes

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

Introduction

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

Prototype

public abstract Class<?>[] getParameterTypes();

Source Link

Document

Returns an array of Class objects that represent the formal parameter types, in declaration order, of the executable represented by this object.

Usage

From source file:org.springframework.beans.factory.support.ConstructorResolver.java

/**
 * Resolve the prepared arguments stored in the given bean definition.
 *//*  w ww  .ja  va  2s  . co m*/
private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
        Executable executable, Object[] argsToResolve, boolean fallback) {

    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = (customConverter != null ? customConverter : bw);
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd,
            converter);
    Class<?>[] paramTypes = executable.getParameterTypes();

    Object[] resolvedArgs = new Object[argsToResolve.length];
    for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
        Object argValue = argsToResolve[argIndex];
        MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
        GenericTypeResolver.resolveParameterType(methodParam, executable.getDeclaringClass());
        if (argValue instanceof AutowiredArgumentMarker) {
            argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, fallback);
        } else if (argValue instanceof BeanMetadataElement) {
            argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
        } else if (argValue instanceof String) {
            argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd);
        }
        Class<?> paramType = paramTypes[argIndex];
        try {
            resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
        } catch (TypeMismatchException ex) {
            throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName,
                    new InjectionPoint(methodParam),
                    "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue)
                            + "] to required type [" + paramType.getName() + "]: " + ex.getMessage());
        }
    }
    return resolvedArgs;
}

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

/**
 * Transform the arguments//from w  w w.  j  a v  a2  s. c  o  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  w  w.ja v a  2  s  .com
 *
 * @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;
}