Example usage for com.badlogic.gdx.utils.reflect Constructor newInstance

List of usage examples for com.badlogic.gdx.utils.reflect Constructor newInstance

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils.reflect Constructor newInstance.

Prototype

public Object newInstance(Object... args) throws ReflectionException 

Source Link

Document

Uses the constructor to create and initialize a new instance of the constructor's declaring class, with the supplied initialization parameters.

Usage

From source file:es.eucm.ead.editor.view.controllers.OptionsController.java

License:Open Source License

/**
 * /*from   w  w  w .  j  a v a 2  s  .  c o  m*/
 * @param clazz
 *            class of the value
 * @param widget
 *            the widget for the value
 * @param args
 *            arguments are interpreted as a list of objects paired in
 *            class, value ([class, value, class, value]), and they would be
 *            use to find the right constructor
 */
@SuppressWarnings("unchecked")
private <V extends ValueController<T, S>, T extends Actor, W extends T, S> V newValueController(Class<V> clazz,
        W widget, Object... args) {
    try {
        V value;
        if (args.length == 0) {
            value = ClassReflection.newInstance(clazz);
        } else {
            Class[] argsClass = new Class[args.length / 2];
            Object[] argsObject = new Object[args.length / 2];
            for (int i = 0; i < args.length; i += 2) {
                argsClass[i] = (Class) args[i];
                argsObject[i] = args[i + 1];
            }
            Constructor constructor = ClassReflection.getDeclaredConstructor(clazz, argsClass);
            value = (V) constructor.newInstance(argsObject);
        }
        value.build(controller, widget);
        return value;
    } catch (ReflectionException e) {
        Gdx.app.error("OptionsController", "No value", e);
    }
    return null;
}