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

com.github.rutledgepaulv.qbuilders.utilities.ObjectUtils Maven / Gradle / Ivy

There is a newer version: 1.6
Show newest version
package com.github.rutledgepaulv.qbuilders.utilities;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.IntStream;

@SuppressWarnings({"unchecked", "Convert2MethodRef"})
public final class ObjectUtils {

    /**
     * Instantiate a class for the provided constructor arguments.
     *
     * @param clazz The class to instantiate
     *
     * @param args The arguments for the constructor.
     *             The constructor used will be determined from the arguments provided.
     *
     * @param  The type of the provided class and resulting instance.
     *
     * @return The new instance.
     */
    public static  T init(Class clazz, Object... args) {
        try {
            final Object[] arguments = args != null ? args : new Object[]{};

            return (T) Arrays.stream(clazz.getConstructors())
                    .filter(construct -> Objects.equals(arguments.length, construct.getParameterCount()))
                    .filter(construct -> IntStream.range(0, arguments.length)
                            .allMatch(val -> construct.getParameterTypes()[val].isAssignableFrom(arguments[val].getClass())))
                    .findFirst().orElseThrow(() -> new InstantiationException("Could not find compatible constructor."))
                    .newInstance(arguments);
        } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException("Could not instantiate class for provided arguments.", e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy