Example usage for org.apache.commons.lang.reflect ConstructorUtils getMatchingAccessibleConstructor

List of usage examples for org.apache.commons.lang.reflect ConstructorUtils getMatchingAccessibleConstructor

Introduction

In this page you can find the example usage for org.apache.commons.lang.reflect ConstructorUtils getMatchingAccessibleConstructor.

Prototype

public static Constructor getMatchingAccessibleConstructor(Class cls, Class[] parameterTypes) 

Source Link

Document

Find an accessible constructor with compatible parameters.

Usage

From source file:com.haulmont.bali.util.ReflectionHelper.java

/**
 * Instantiates an object by appropriate constructor.
 * @param cls       class/*from   w w w  . j av  a  2s.c  om*/
 * @param params    constructor arguments
 * @return          created object instance
 * @throws NoSuchMethodException    if the class has no constructor matching the given arguments
 */
@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> cls, Object... params) throws NoSuchMethodException {
    Class[] paramTypes = getParamTypes(params);

    Constructor<T> constructor = ConstructorUtils.getMatchingAccessibleConstructor(cls, paramTypes);
    if (constructor == null)
        throw new NoSuchMethodException(
                "Cannot find a matching constructor for " + cls.getName() + " and given parameters");
    try {
        return constructor.newInstance(params);
    } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.extensiblecatalog.ncip.v2.common.BaseComponentFactory.java

/**
 * Construct a <COMPONENT> using the provided configuration.
 *
 * @param configuration the <CONFIG>
 * @return the new <COMPONENT> object
 * @throws ToolkitException/*from   ww w. j  a  va2 s .c o m*/
 */
@SuppressWarnings(value = { "unchecked" }) // Because Constructor.newInstance returns Object.
protected static ToolkitComponent buildComponent(ToolkitConfiguration configuration) throws ToolkitException {

    ToolkitComponent component;

    String componentClassName = configuration.getComponentClassName();
    if (componentClassName != null) {

        try {

            Class<? extends ToolkitComponent> componentClass = Class.forName(componentClassName)
                    .asSubclass(ToolkitComponent.class);

            Constructor<? extends ToolkitComponent> ctor = ConstructorUtils
                    .getMatchingAccessibleConstructor(componentClass, new Class[] { configuration.getClass() });

            if (ctor != null) {

                component = ctor.newInstance(configuration);

            } else {

                throw new ToolkitException("Exception constructing " + componentClassName
                        + " class: No matching constructor found for '" + configuration.getClass().getName()
                        + "'.");

            }

        } catch (ClassNotFoundException e) {

            throw new ToolkitException("Exception loading " + componentClassName + " class.", e);

        } catch (InstantiationException e) {

            throw new ToolkitException("Exception constructing " + componentClassName + " class.", e);

        } catch (IllegalAccessException e) {

            throw new ToolkitException("Exception constructing " + componentClassName + " class.", e);

        } catch (InvocationTargetException e) {

            throw new ToolkitException("Exception constructing " + componentClassName + " class.", e);

        }

    } else {

        throw new ToolkitException("Component class name not set in component configuration.");

    }

    return component;

}