Java Class New Instance newInstance(String className)

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

Description

New instance.

License

Apache License

Parameter

Parameter Description
className the class name

Return

the object

Declaration

public static Object newInstance(String className) 

Method Source Code

//package com.java2s;
/** /*  ww  w.jav  a 2s  . co m*/
 * Copyright 2011 The Apache Software Foundation
 *
 * Licensed 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.
 * 
 * @author Felipe Oliveira (http://mashup.fm)
 * 
 */

import java.lang.reflect.Constructor;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class Main {
    /** Constructor cache */
    private static final ConcurrentMap<Class<?>, Constructor<?>> classConstructorCache = new ConcurrentHashMap<Class<?>, Constructor<?>>();

    /**
     * New instance.
     * 
     * @param className
     *            the class name
     * @return the object
     */
    public static Object newInstance(String className) {
        Class<?> clazz;
        try {
            clazz = Class.forName(className);
            return newInstance(clazz);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * New instance.
     * 
     * @param <T>
     *            the generic type
     * @param clazz
     *            the clazz
     * @return the t
     */
    public static <T> T newInstance(Class<T> clazz) {
        Constructor<T> ctor = null;

        if (classConstructorCache.containsKey(clazz)) {
            ctor = (Constructor<T>) classConstructorCache.get(clazz);
        } else {
            // Try public ctor first
            try {
                ctor = clazz.getConstructor();
            } catch (Exception e) {
                // Swallow, no public ctor
            }

            // Next, try non-public ctor
            try {
                ctor = clazz.getDeclaredConstructor();
                ctor.setAccessible(true);
            } catch (Exception e) {
                // Swallow, no non-public ctor
            }

            classConstructorCache.put(clazz, ctor);
        }

        if (ctor != null) {
            try {
                return ctor.newInstance();
            } catch (Exception e) {
                throw new RuntimeException("Cannot instantiate " + clazz, e);
            }
        } else {
            throw new RuntimeException("No default constructor for " + clazz);
        }
    }
}

Related

  1. newInstance(String className)
  2. newInstance(String className)
  3. newInstance(String className)
  4. newInstance(String className)
  5. newInstance(String className)
  6. newInstance(String className)
  7. newInstance(String className, Class instanceClazz)
  8. newInstance(String className, Class cls)
  9. newInstance(String className, Class context)