construct Instances from Constructor - Java Reflection

Java examples for Reflection:Constructor

Description

construct Instances from Constructor

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static <T> List<T> constructInstances(
            Class<? extends T>[] classes, Class<? extends T> def) {

        List<T> result = new ArrayList<T>();
        if (classes == null || classes.length == 0) {
            if (def != null) {
                result.add(createObject(def));
            }/*from   w w w  .j a  v a  2 s.com*/
        } else {
            for (Class<? extends T> cls : classes) {
                result.add(createObject(cls));
            }
        }
        return result;
    }

    public static <T> T createObject(Class<? extends T> cls, Class<?> arg) {

        try {
            return cls.getConstructor(Class.class).newInstance(arg);
        } catch (Exception exc) {
            throw new RuntimeException(exc);
        }
    }

    static <T> T createObject(Class<? extends T> cls) {

        try {
            return cls.getConstructor().newInstance();
        } catch (Exception exc) {
            throw new RuntimeException(exc);
        }
    }
}

Related Tutorials