Example usage for org.apache.commons.lang3.reflect ConstructorUtils invokeConstructor

List of usage examples for org.apache.commons.lang3.reflect ConstructorUtils invokeConstructor

Introduction

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

Prototype

public static <T> T invokeConstructor(final Class<T> cls, Object... args) throws NoSuchMethodException,
        IllegalAccessException, InvocationTargetException, InstantiationException 

Source Link

Document

Returns a new instance of the specified class inferring the right constructor from the types of the arguments.

This locates and calls a constructor.

Usage

From source file:pt.ua.tm.neji.context.ContextConfiguration.java

private static Module invokeModuleWithParams(final String moduleName,
        final Collection<Pair<String>> moduleParams, final Parser parser) throws ReflectiveOperationException {

    List<Class<?>> classes = new ArrayList<>();
    List<Object> objects = new ArrayList<>();

    for (Pair<String> pair : moduleParams) {

        Class paramClass = Class.forName(pair.getX());

        if (paramClass == Parser.class) {
            if (parser == null) {
                logger.warn(/*  w w  w .  j av a2s . c  o m*/
                        "Module that required a parser in constructor was skipped during ContextDescriptor fetching: no parser was provided.");
                continue;
            } else {
                objects.add(parser);
            }

        } else if (paramClass.isEnum()) {
            Enum paramEnum = Enum.valueOf(paramClass, pair.getY());
            objects.add(paramEnum);

        } else {
            objects.add(ConstructorUtils.invokeConstructor(paramClass, pair.getY()));

            if (Primitives.isWrapperType(paramClass)) {
                paramClass = Primitives.unwrap(paramClass);
            }
        }
        classes.add(paramClass);
    }

    return (Module) ConstructorUtils.invokeConstructor(Class.forName(moduleName), objects.toArray(),
            classes.toArray(new Class[classes.size()]));
}