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

net.thucydides.core.csv.InstanceBuilder Maven / Gradle / Ivy

There is a newer version: 4.2.8
Show newest version
package net.thucydides.core.csv;

import net.thucydides.core.csv.converters.TypeConverter;
import net.thucydides.core.csv.converters.TypeConverters;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Helper class for finding and invoking constructors.
 */
public final class InstanceBuilder {

    private Object targetObject;

    private   InstanceBuilder(final T newObject) {
        this.targetObject = newObject;
    }

    public static  T newInstanceOf(final Class clazz,
                                      final Object... constructorArgs)
            throws InstantiationException, IllegalAccessException, InvocationTargetException {

        if ((constructorArgs.length == 0) &&(thereIsADefaultConstructorFor(clazz))) {
            return clazz.newInstance();
        } else {
            return invokeConstructorFor(clazz, constructorArgs);
        }
    }

    @SuppressWarnings("unchecked")
    public static  T invokeConstructorFor(final Class clazz, final Object[] constructorArgs)
            throws InvocationTargetException, IllegalAccessException, InstantiationException {

        Constructor[] constructors = clazz.getDeclaredConstructors();

        for(Constructor constructor : constructors) {
            if (constructor.getParameterTypes().length == constructorArgs.length) {
                return (T) constructor.newInstance(constructorArgs);
            }
        }
        throw new IllegalStateException("No matching constructor found for " + clazz
                + " with arguments: " + ArrayUtils.toString(constructorArgs));

    }

    private static  boolean thereIsADefaultConstructorFor(final Class clazz) {

        Constructor[] constructors = clazz.getDeclaredConstructors();
        for(Constructor constructor : constructors) {
            if (constructor.getParameterTypes().length == 0) {
                return true;
            }
        }
        return false;
    }

    public void setPropertyValue(final String property,
                                 final String value) {
        if (!setProperty(property, value)) {
            throw new FailedToInitializeTestData("Could not find property field " + property);
        }
    }

    private boolean setProperty(String property, String value) {
        try {
            Method setter = findSetter(property);
            if (setter != null) {
               return setViaSetter(setter, value);
            }
            Field field = findField(property);
            if (field != null) {
                return setViaField(field, value);
            }
            return false;
        } catch (Exception e) {
            throw new FailedToInitializeTestData("Could not assign property value", e);
        }
    }

    private boolean setViaField(Field field, String value) throws IllegalAccessException {
        TypeConverter converter = TypeConverters.getTypeConverterFor(field.getType());
        Object valueToSet = converter.valueOf(value);
        field.set(targetObject, valueToSet);
        return true;
    }

    private boolean setViaSetter(Method setter, String value) throws InvocationTargetException, IllegalAccessException {
        TypeConverter converter = TypeConverters.getTypeConverterFor(setter.getParameterTypes()[0]);
        Object valueToSet = converter.valueOf(value);
        setter.invoke(targetObject, valueToSet);
        return true;
    }

    private Method findSetter(final String property) {
        Method[] methods = targetObject.getClass().getMethods();
        String setterMethod = "set" + StringUtils.capitalize(property);
        for(Method method : methods) {
            if (method.getName().equals(setterMethod)) {
                return method;
            }
        }
        return null;
    }

    private Field findField(final String property) {
        List fields = getAllDeclaredFieldsIn(targetObject.getClass());
        for(Field field :fields) {
            if (field.getName().compareToIgnoreCase(property) == 0) {
                field.setAccessible(true);
                return field;
            }
        }
        return null;
    }

    private List getAllDeclaredFieldsIn(Class targetClass) {
        List parentFields
                = (targetClass.getSuperclass() != null) ? getAllDeclaredFieldsIn(targetClass.getSuperclass()) : Collections.EMPTY_LIST;

        List localFields = Arrays.asList(targetClass.getDeclaredFields());
        List allFields = new ArrayList(localFields);
        allFields.addAll(parentFields);
        return allFields;
    }

    public static  InstanceBuilder inObject(final T newObject) {
        return new InstanceBuilder(newObject);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy