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

org.tools4j.groovytables.ExecutableConstructionCall.groovy Maven / Gradle / Ivy

Go to download

A groovy API which allows you to create lists of objects using a table like grammar.

There is a newer version: 1.6
Show newest version
package org.tools4j.groovytables;

import java.lang.reflect.Executable;

/**
 * User: ben
 * Date: 11/03/2016
 * Time: 9:42 AM
 */
trait ExecutableConstructionCall implements Callable {
    abstract T construct(Object[] args);
    abstract Executable getExecutable();

    @Override
    ExecutableCallPrecursor getCallPrecursor(final List columnHeadings, final Object ... rawArgs){
        //column headings are not used so disregard...
        return getCallPrecursor(rawArgs)
    }

    @Override
    ExecutableCallPrecursor getCallPrecursor(final Object ... rawArgs){
        final Executable executable = getExecutable()

        Logger.info("    Getting CreationMethodPrecursor for exectuable of name $executable.name with args ${executable.parameterTypes.collect{it.simpleName}}")
        if(executable.getParameterCount() != rawArgs.size()){
            Logger.info("        Mismatched number of args, executable:$executable.parameterCount != args:$rawArgs.length")
            return new ExecutableCallPrecursor(Suitability.NOT_SUITABLE, rawArgs, null, null)
        } else {
            Logger.info("        Correct number of args: $rawArgs.length")
        }
        Suitability worstSuitabilityOfThisExecutable = Suitability.SUITABLE;
        final List coercedResults = new ArrayList<>(executable.getParameterCount());
        for (int i = 0; i < executable.getParameterCount(); i++) {
            final Class constructorParamType = executable.getParameterTypes()[i];
            Object rawArg = rawArgs[i]
            final ValueCoercionResult coercionResult = ValueCoerser.coerceToType(rawArg, constructorParamType);
            worstSuitabilityOfThisExecutable = worstSuitabilityOfThisExecutable.worseOf(coercionResult.getSuitability())
            Logger.info("        Coercion result for arg:${rawArg.getClass().simpleName}[$rawArg] to:$constructorParamType.simpleName : $coercionResult")
            coercedResults.add(coercionResult);
            if (coercionResult.getSuitability() == Suitability.NOT_SUITABLE) {
                break
            }
        }
        return new ExecutableCallPrecursor(
                worstSuitabilityOfThisExecutable,
                rawArgs,
                coercedResults.collect{it.result}.toArray(),
                this
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy