Java Class New Instance newInstance(Class clazz, Object... args)

Here you can find the source of newInstance(Class clazz, Object... args)

Description

Creates a new instance of the specified class

License

Creative Commons License

Parameter

Parameter Description
clazz The class object

Exception

Parameter Description
SecurityException an exception
NoSuchMethodException an exception
InvocationTargetException an exception
IllegalArgumentException an exception
Exception an exception

Return

The new instance of the class

Declaration

public static Object newInstance(Class<?> clazz, Object... args)
        throws InstantiationException, IllegalAccessException,
        NoSuchMethodException, SecurityException,
        IllegalArgumentException, InvocationTargetException 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

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

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static Map<String, Class<?>> classes = new HashMap<String, Class<?>>();

    /**/*from  w w w  .  ja  v a  2s  . c  o m*/
     * Creates a new instance of the specified class name
     * 
     * @param clazz The class name, either fully qualified or just the simple name
     * @return The new instance of the class
     * @throws Exception
     */
    public static Object newInstance(String clazz) throws Exception {
        Class<?> realClazz = getClass(clazz);

        if (realClazz != null) {
            return realClazz.newInstance();
        }

        return null;
    }

    /**
     * Creates a new instance of the specified class
     * 
     * @param clazz The class object
     * @return The new instance of the class
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws Exception
     */
    public static Object newInstance(Class<?> clazz, Object... args)
            throws InstantiationException, IllegalAccessException,
            NoSuchMethodException, SecurityException,
            IllegalArgumentException, InvocationTargetException {
        return newInstance(clazz, getArgTypes(args), args);
    }

    /**
     * Creates a new instance of the specified class, with argument types specified
     * 
     * @param clazz The class object
     * @param argTypes The argument types of the class's constructor
     * @return The new instance of the class
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws Exception
     */
    public static Object newInstance(Class<?> clazz, Class<?>[] argTypes,
            Object... args) throws InstantiationException,
            IllegalAccessException, NoSuchMethodException,
            SecurityException, IllegalArgumentException,
            InvocationTargetException {
        Constructor<?> constructor = clazz
                .getConstructor(getArgTypes(args));

        return constructor.newInstance(args);
    }

    /**
     * Returns a Java class object for the specified class name.
     * 
     * @param name The class name, either fully qualified or just the simple name
     * @return The class, if found, otherwise throws exception
     * @throws Exception
     */
    public static Class<?> getClass(String name) throws Exception {
        for (String clazz : classes.keySet()) {
            if (clazz.toLowerCase().contains(name.toLowerCase())) {
                return classes.get(clazz);
            }
        }

        throw new Exception(
                "Class "
                        + name
                        + " not found, try including the package name, for instance: net.minecraft.server._version_."
                        + name);
    }

    private static Class<?>[] getArgTypes(Object... args) {
        if (args != null) {
            Class<?>[] argTypes = new Class<?>[args.length];

            for (int i = 0; i < args.length; i++) {
                argTypes[i] = args[i] != null ? args[i].getClass() : null;
            }

            return argTypes;
        }

        return null;
    }
}

Related

  1. newInstance(Class clazz)
  2. newInstance(Class clazz)
  3. newInstance(Class clazz)
  4. newInstance(Class clazz, Class[] args, Object[] objects)
  5. newInstance(Class clazz, Object... args)
  6. newInstance(Class cls)
  7. newInstance(Class cls)
  8. newInstance(Class cls)
  9. newInstance(Class cls, Map, Constructor> cache)