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

org.tools4j.groovytables.ClassConstructor.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 groovy.transform.ToString

import java.lang.reflect.Constructor
import java.lang.reflect.Executable

/**
 * User: ben
 * Date: 19/02/2016
 * Time: 5:26 PM
 */

@ToString
class ClassConstructor implements ExecutableConstructionMethod{
    final Constructor constructor;

    ClassConstructor(final Constructor constructor) {
        this.constructor = constructor
    }

    @Override
    T construct(final Object[] args) {
        constructor.setAccessible(true)
        try {
            return constructor.newInstance(args)
        } catch( IllegalArgumentException e ){
            Logger.error("Error whilst constructing object.  This is an error within the DSL.  " +
                    "Arguments should have been coerced into acceptable types before this method was called.  " +
                    "Please notify developers.  " +
                    "Args:" + Arrays.asList(args) +
                    " Constructor:" + constructor +
                    " caught exception: " + e)
            e.printStackTrace(System.err)
            throw e
        }
    }

    @Override
    Executable getExecutable() {
        return constructor
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy