集册 Java实例教程 获取类实例

获取类实例

欢马劈雪     最近更新时间:2020-01-02 10:19:05

478
获取类实例

/**
时 代 J a v a - N o w J a v a . c o m 提供 
**/

//package com.nowjava;

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;


public class Main {

    public static Object getClassInstance(String className,

            Class[] paramTypes, Object[] params)

            throws ClassNotFoundException, SecurityException,

            NoSuchMethodException, IllegalArgumentException,

            InstantiationException, IllegalAccessException,

            InvocationTargetException {

        Object obj = null;

        Class actionClassObj;

        actionClassObj = Class.forName(className);

        Constructor cons = actionClassObj.getConstructor(paramTypes);

        obj = cons.newInstance(params);

        return obj;

    }


    public static <T> T getClassInstance(String className,

            Class[] paramTypes, Object[] params, Class<T> c)/*from nowjava.com - 时代Java*/

            throws ClassNotFoundException, SecurityException,

            NoSuchMethodException, IllegalArgumentException,

            InstantiationException, IllegalAccessException,

            InvocationTargetException {

        Object obj = null;

        Class actionClassObj;

        actionClassObj = Class.forName(className);

        Constructor cons = actionClassObj.getConstructor(paramTypes);

        obj = cons.newInstance(params);

        return (T) obj;

    }


    public static Object getClassInstance(String className,

            Class[] paramTypes, Object[] params,

            ClassLoader customClassLoader) throws ClassNotFoundException,

            SecurityException, NoSuchMethodException,

            IllegalArgumentException, InstantiationException,

           
展开阅读全文