Create an object of the obj Class by calling the constructor that matches the parameters. - Java Reflection

Java examples for Reflection:Constructor

Description

Create an object of the obj Class by calling the constructor that matches the parameters.

Demo Code

/**/*from   w w  w.j a  va  2 s.  c o m*/
 * Copyright (c) 2011 eXtensible Catalog Organization
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the MIT/X11 license. The text of the license can be
 * found at http://www.opensource.org/licenses/mit-license.php.
 */
import org.apache.log4j.Logger;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;

public class Main{
    /**
     * Create an object of the objClass by calling the constructor that matches the parameters.
     * @param objClass
     * @param parameters
     * @return
     */
    public static Object createObject(Class objClass, Object... parameters)
            throws ToolkitException {

        Object obj;
        try {

            Class[] parmTypes;
            if (parameters != null) {

                parmTypes = new Class[parameters.length];
                for (int index = 0; index < parameters.length; ++index) {

                    parmTypes[index] = parameters[index].getClass();

                    // Strip quote marks from around strings
                    // Note: this would change the type of objects that are sub-classes of String into String,
                    // but as String is a final class there can't actually be any sub-types of String,
                    // so this is safe to do.
                    if (parameters[index] instanceof String) {

                        String temp = (String) parameters[index];
                        if (temp.startsWith("\"") && temp.endsWith("\"")) {

                            parameters[index] = temp.substring(1,
                                    temp.length() - 1);

                        }
                    }

                }

            } else {

                parmTypes = new Class[0];

            }

            Constructor ctor = objClass.getConstructor(parmTypes);
            obj = ctor.newInstance(parameters);

        } catch (InstantiationException e) {

            throw new ToolkitException(
                    "Exception constructing an instance of " + objClass, e);

        } catch (IllegalAccessException e) {

            throw new ToolkitException(
                    "Exception constructing an instance of " + objClass, e);

        } catch (NoSuchMethodException e) {

            throw new ToolkitException(
                    "Exception constructing an instance of " + objClass, e);

        } catch (InvocationTargetException e) {

            throw new ToolkitException(
                    "Exception constructing an instance of " + objClass, e);

        }

        return obj;

    }
}

Related Tutorials