Java Class New Instance newInstance(String className, Object[] args)

Here you can find the source of newInstance(String className, Object[] args)

Description

new Instance

License

Apache License

Declaration

public static Object newInstance(String className, Object[] args) 

Method Source Code

//package com.java2s;
/**//  w ww. j  ava2s  .c  o  m
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

public class Main {
    public static Object newInstance(String className, Object[] args) {
        try {
            Class newoneClass = Class.forName(className);
            Class[] argsClass = new Class[args.length];
            for (int i = 0, j = args.length; i < j; i++) {
                argsClass[i] = args[i].getClass();
            }
            Constructor cons = newoneClass.getConstructor(argsClass);
            return cons.newInstance(args);
        } catch (Exception e) {
            throw convertReflectionExceptionToUnchecked(e);
        }
    }

    public static Object newInstance(Class clazz, Object[] args) {
        try {
            Class[] argsClass = new Class[args.length];
            for (int i = 0, j = args.length; i < j; i++) {
                argsClass[i] = args[i].getClass();
            }
            Constructor cons = clazz.getConstructor(argsClass);
            return cons.newInstance(args);
        } catch (Exception e) {
            throw convertReflectionExceptionToUnchecked(e);
        }
    }

    public static Object newInstance(Class clazz, Object[] args, Class[] argsClass) {
        try {
            Constructor cons = clazz.getConstructor(argsClass);
            return cons.newInstance(args);
        } catch (Exception e) {
            throw convertReflectionExceptionToUnchecked(e);
        }
    }

    public static Object newInstance(Class clazz) {
        try {
            Constructor cons = clazz.getConstructor();
            return cons.newInstance();
        } catch (Exception e) {
            throw convertReflectionExceptionToUnchecked(e);
        }
    }

    public static Object newInstance(String className) {
        try {
            Class newoneClass = Class.forName(className);
            Constructor cons = newoneClass.getConstructor();
            return cons.newInstance();
        } catch (Exception e) {
            throw convertReflectionExceptionToUnchecked(e);
        }
    }

    public static RuntimeException convertReflectionExceptionToUnchecked(Exception e) {
        if (e instanceof IllegalAccessException || e instanceof IllegalArgumentException
                || e instanceof NoSuchMethodException) {
            return new IllegalArgumentException(e);
        } else if (e instanceof InvocationTargetException) {
            return new RuntimeException(((InvocationTargetException) e).getTargetException());
        } else if (e instanceof RuntimeException) {
            return (RuntimeException) e;
        }
        return new RuntimeException("Unexpected Checked Exception.", e);
    }
}

Related

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