名称类

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

444
名称类


//package com.nowjava;

import java.lang.reflect.Array;

import java.lang.reflect.Constructor;
/* 
*来 自
 n o w j a v a . c o m - 时  代  Java
*/

import java.lang.reflect.InvocationTargetException;


import java.util.Map;


public class Main {



    public static final String ARRAY_SUFFIX = "[]";


    private static Map primitiveClassNameMap;


    private static Map primitiveClassWrapperMap;
/* 来 自 时代Java公众号*/

    public static Class forName(String className)

            throws ClassNotFoundException {

        return forName(className, Thread.currentThread()

                .getContextClassLoader());

    }


    public static Class forName(String className, ClassLoader classLoader)

            throws ClassNotFoundException {

        Class result = (Class) primitiveClassNameMap.get(className);

        if (null != result) {

            return result;

        }


        if (className.endsWith(ARRAY_SUFFIX)) {

            // special handling for array class names

            String elementClassName = className.substring(0,

                    className.length() - ARRAY_SUFFIX.length());

            Class elementClass = forName(elementClassName, classLoader);

            return Array.newInstance(elementClass, 0).getClass();

        } else {

            return Class.forName(className, true, classLoader);

        }

    }


    public static Object newInstance(Class theClass)

            throws InstantiationException, IllegalAccessException {

        if (null != primitiveClassWrapperMap.get(theClass)) {

            theClass = (Class) primitiveClassWrapperMap.get(theClass);

        }

        return theClass.newInstance();

    }


    public static Object newInstance(Class theClass, String param)

            throws InstantiationException, IllegalAccessException,

            SecurityException, NoSuchMethodException,

            
展开阅读全文