集册 Java实例教程 通过调用默认构造函数,返回给定类名称的实例。

通过调用默认构造函数,返回给定类名称的实例。

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

437
通过调用默认构造函数,返回给定类名称的实例。

/*

 * $Id: ClassUtil.java 709153 2008-10-30 12:54:10Z apetrelli $

 *

 * Licensed to the Apache Software Foundation (ASF) under one

 * or more contributor license agreements.  See the NOTICE file

 * distributed with this work for additional information

 * regarding copyright ownership.  The ASF licenses this file

 * to you under the Apache License, Version 2.0 (the

 * "License"); you may not use this file except in compliance

 * with the License.  You may obtain a copy of the License at

 *

 * http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing,

 * software distributed under the License is distributed on an

 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

 * KIND, either express or implied.  See the License for the

 * specific language governing permissions and limitations

 * under the License.

 *//* 来 自 N o w  J a v a  . c o m*/

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;


public class Main{


    /**

     * Returns an instance of the given class name, by calling the default

     * constructor.

     *

     * @param className The class name to load and to instantiate.

     * @return The new instance of the class name.

     * @throws CannotInstantiateObjectException If something goes wrong during

     * instantiation.

     * @since 2.0.7

     */

    public static Object instantiate(String className) {

        return instantiate(className, false);

    }

    /**

     * Returns an instance of the given class name, by calling the default

     * constructor.

     *

     * @param className The class name to load and to instantiate.

     * @param returnNull If <code>true</code>, if the class is not found it

     * returns <code>true</code>, otherwise it throws a

     * <code>TilesException</code>.

     * @return The new instance of the class name.

     * @throws CannotInstantiateObjectException If something goes wrong during instantiation.

     * @since 2.0.7

     */

    public static Object instantiate(String className, boolean returnNull) {

        ClassLoader original = Thread.currentThread()

                .getContextClassLoader();

        if (original == null) {

            Thread.currentThread().setContextClassLoader(

                    ClassUtil.class.getClassLoader());
                    /**
                    来 自 时 代 J a v a 公 众 号 - nowjava.com
                    **/

        }

        try {

            Class<?> namedClass = Class.forName(className);

            return namedClass.newInstance();

        } catch (ClassNotFoundException e) {

            if (returnNull) {

                return null;

            }

            throw new CannotInstantiateObjectException(

                    "Unable to resolve factory class: '" + className + "'",

                    e);

        } catch (IllegalAccessException e) {

            throw new CannotInstantiateObjectException(

                    "Unable to access factory class: '" + className + "'",

                    e);

        } catch (InstantiationException e) {

            
展开阅读全文