Java Class New Instance newInstance(String className, Object arg1)

Here you can find the source of newInstance(String className, Object arg1)

Description

new Instance

License

Open Source License

Declaration

public static Object newInstance(String className, Object arg1)
            throws ClassNotFoundException, ClassCastException, IllegalAccessException, InvocationTargetException,
            InstantiationException, NoSuchMethodException 

Method Source Code


//package com.java2s;
import java.lang.reflect.InvocationTargetException;

public class Main {
    public static Object newInstance(String className, Object arg1)
            throws ClassNotFoundException, ClassCastException, IllegalAccessException, InvocationTargetException,
            InstantiationException, NoSuchMethodException {

        Class[] argTypes = { arg1.getClass() };

        Object[] args = { arg1 };

        return newInstance(className, argTypes, args);
    }/*from w ww  .  j  a  v  a2s . c  o  m*/

    public static Object newInstance(String className, Object arg1, Object arg2)
            throws ClassNotFoundException, ClassCastException, IllegalAccessException, InvocationTargetException,
            InstantiationException, NoSuchMethodException {

        Class[] argTypes = { arg1.getClass(), arg2.getClass() };

        Object[] args = { arg1, arg2 };
        //System.out.println("@i " +(arg2 instanceof IApplication));
        //System.out.println("@t " +StringUtils.toString(argTypes));
        //System.out.println("@a " +StringUtils.toString(args));
        //System.out.println("@n " +className);
        //System.out.println("@ " +StringUtils.toString(Class.forName(className).getConstructors()));
        return newInstance(className, argTypes, args);
    }

    public static Object newInstance(String className, Object arg1, Object arg2, Object arg3)
            throws ClassNotFoundException, ClassCastException, IllegalAccessException, InvocationTargetException,
            InstantiationException, NoSuchMethodException {

        Class[] argTypes = { arg1.getClass(), arg2.getClass(), arg3.getClass() };

        Object[] args = { arg1, arg2, arg3 };

        return newInstance(className, argTypes, args);
    }

    public static Object newInstance(String className, Object arg1, Object arg2, Object arg3, Object arg4)
            throws ClassNotFoundException, ClassCastException, IllegalAccessException, InvocationTargetException,
            InstantiationException, NoSuchMethodException {

        Class[] argTypes = { arg1.getClass(), arg2.getClass(), arg3.getClass(), arg4.getClass() };

        Object[] args = { arg1, arg2, arg3, arg4 };

        return newInstance(className, argTypes, args);
    }

    public static Object newInstance(String className, Object arg1, Object arg2, Object arg3, Object arg4,
            Object arg5) throws ClassNotFoundException, ClassCastException, IllegalAccessException,
            InvocationTargetException, InstantiationException, NoSuchMethodException {

        Class[] argTypes = { arg1.getClass(), arg2.getClass(), arg3.getClass(), arg4.getClass(), arg5.getClass() };

        Object[] args = { arg1, arg2, arg3, arg4, arg5 };

        return newInstance(className, argTypes, args);
    }

    protected static Object newInstance(String className, Class[] argTypes, Object[] args)
            throws ClassNotFoundException, ClassCastException, IllegalAccessException, InvocationTargetException,
            InstantiationException, NoSuchMethodException {

        return Class.forName(className).getConstructor(argTypes).newInstance(args);
    }
}

Related

  1. newInstance(String className, Class instanceClazz)
  2. newInstance(String className, Class cls)
  3. newInstance(String className, Class context)
  4. newInstance(String className, Class castTo)
  5. newInstance(String className, Class[] parmsCls, Object[] parms)
  6. newInstance(String className, Object... args)
  7. newInstance(String className, Object... args)
  8. newInstance(String className, Object... parameters)
  9. newInstance(String className, Object... params)