Java Class New Instance newInstance(String clazz)

Here you can find the source of newInstance(String clazz)

Description

new Instance

License

Open Source License

Declaration

public static Object newInstance(String clazz) 

Method Source Code

//package com.java2s;
/**/*from  ww  w . ja v a 2 s. c  o m*/
 *     Copyright (C) 2013-2014  the original author or authors.
 *
 *     This program 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,
 *     any later version.
 *
 *     This program 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/>
 */

import java.lang.reflect.Constructor;

import com.google.common.base.Strings;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

public class Main {
    public static Object newInstance(String clazz) {
        checkState(!Strings.isNullOrEmpty(clazz));

        try {
            return Class.forName(clazz).newInstance();
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        }
    }

    public static Object newInstance(final String clazzName, final Object... params) {
        try {
            Constructor<?> method = Class.forName(clazzName).getDeclaredConstructor(getArgsType(params));
            checkNotNull(method).setAccessible(true);
            return method.newInstance(params);
        } catch (Exception exception) {
            throw new RuntimeException("Error in create instance of class " + clazzName);
        }
    }

    /**
     * @param params
     * @return
     */
    private static Class<?>[] getArgsType(Object... params) {
        Class<?>[] paramsTypes = null;
        if (params != null && params.length > 0) {
            paramsTypes = new Class[params.length];
            for (int i = 0; i < params.length; i++) {
                Class<?> superClassType = params[i].getClass().getSuperclass();
                if (superClassType != null && !superClassType.equals(Object.class)) {
                    paramsTypes[i] = superClassType;
                } else {
                    paramsTypes[i] = params[i].getClass();
                }
            }
        }
        return paramsTypes;
    }
}

Related

  1. newInstance(String className, Object... params)
  2. newInstance(String className, Object[] args)
  3. newInstance(String className, Object[] args)
  4. newInstance(String className, Object[] args)
  5. newInstance(String classname, Properties properties)
  6. newInstance(String clazzStr)
  7. newInstance(String cls, Class interfaceType)
  8. newInstance(String klass)
  9. newInstance(String name)