Java Class New Instance newInstanceForName(String fullClassName, Object... args)

Here you can find the source of newInstanceForName(String fullClassName, Object... args)

Description

Create a instance of a given class.

License

Open Source License

Parameter

Parameter Description
fullClassName The name of the class to be instantiate.
args Arguments to use when invoking this constructor
T The type of the given class.

Return

An instance of the given class. Can be null if the class wasn't found.

Declaration

@SuppressWarnings({ "unchecked" })
public static <T> T newInstanceForName(String fullClassName, Object... args) 

Method Source Code

//package com.java2s;
/**/*www . j a  va 2  s .  c  o  m*/
 * Copyright (C) 2013 Contributors.
 *
 * This file is part of FormulaJ.
 *
 * FormulaJ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FormulaJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
 *
 * Contact: formulaj-user-list@googlegroups.com.
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Main {
    /**
     * Create a instance of a given class.
     * 
     * @param fullClassName
     *            The name of the class to be instantiate.
     * @param args
     *            Arguments to use when invoking this constructor
     * @param <T>
     *            The type of the given class.
     * @return An instance of the given class. Can be <code>null</code> if the class wasn't found.
     */
    @SuppressWarnings({ "unchecked" })
    public static <T> T newInstanceForName(String fullClassName, Object... args) {
        try {
            return (T) newInstanceForName(Class.forName(fullClassName), args);
        } catch (ClassNotFoundException exception) {
            return null;
        }
    }

    /**
     * Create a instance of a given class.
     * 
     * @param clazz
     *            The class to be instantiate.
     * @param args
     *            Arguments to use when invoking this constructor
     * @param <T>
     *            The type of the given class.
     * @return An instance of the given class.
     */
    @SuppressWarnings("unchecked")
    public static <T> T newInstanceForName(Class<T> clazz, Object... args) {
        T instance = null;
        if (args == null || args.length == 0) {
            Constructor<T> constructor;
            try {
                constructor = clazz.getConstructor();
                constructor.setAccessible(true);
                instance = constructor.newInstance();
            } catch (NoSuchMethodException | SecurityException | InvocationTargetException | IllegalAccessException
                    | InstantiationException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        } else {
            external: for (Constructor constructor : clazz.getConstructors()) {
                Class[] parameterTypes = constructor.getParameterTypes();
                if (parameterTypes != null && parameterTypes.length == args.length) {
                    for (int i = 0; i < parameterTypes.length; i++) {
                        if (!parameterTypes[i].isAssignableFrom(args[i].getClass())) {
                            continue external;
                        }
                    }

                    try {
                        constructor.setAccessible(true);
                        instance = (T) constructor.newInstance(args);
                    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                            | InvocationTargetException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                }
            }
        }
        return instance;
    }
}

Related

  1. newInstance(Type type)
  2. newInstance(Type type)
  3. newInstanceByName(String className)
  4. newInstanceByName(String className)
  5. newInstanceForClass(Class type)
  6. newInstanceFromClass(final Class clazz)
  7. newInstanceFromClassName(String className, Class classType)
  8. newInstanceFromUnknownArgumentTypes(Class cls, Object[] args)
  9. newInstanceHard(String name)