Java Class New Instance newInstance(Class clazz)

Here you can find the source of newInstance(Class clazz)

Description

Create an object for the given class using its default constructor.

License

Apache License

Declaration

public static <T> T newInstance(Class<T> clazz) 

Method Source Code

//package com.java2s;
/**/*from   w w w  .jav  a 2  s .  c o m*/
 * 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.
 */

import java.lang.reflect.Constructor;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    public static final Class<?>[] EMPTY_CLASSES = {};
    /**
     * Cache of constructors for each class. Pins the classes so they
     * can't be garbage collected until ReflectionUtils can be collected.
     */
    private static final Map<List<Class<?>>, Constructor<?>> CONSTRUCTOR_CACHE = new ConcurrentHashMap<>();

    /**
     * Create an object for the given class using its default constructor.
     */
    public static <T> T newInstance(Class<T> clazz) {
        return newInstance(clazz, EMPTY_CLASSES);
    }

    /**
     * Create an object for the given class using the specified constructor.
     *
     * @param clazz class of which an object is created
     * @param argClasses argument classes of the constructor
     * @param args actual arguments to be passed to the constructor
     * @param <T> class type of clazz
     * @return a new object
     */
    public static <T> T newInstance(Class<T> clazz, Class<?>[] argClasses,
            Object... args) {
        Objects.requireNonNull(clazz, "clazz == null");
        try {
            final List<Class<?>> key = new ArrayList<>();
            key.add(clazz);
            key.addAll(Arrays.asList(argClasses));

            @SuppressWarnings("unchecked")
            Constructor<T> ctor = (Constructor<T>) CONSTRUCTOR_CACHE
                    .get(key);
            if (ctor == null) {
                ctor = clazz.getDeclaredConstructor(argClasses);
                ctor.setAccessible(true);
                CONSTRUCTOR_CACHE.put(key, ctor);
            }
            return ctor.newInstance(args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. newInstance(Class clazz)
  2. newInstance(Class clazz)
  3. newInstance(Class clazz)
  4. newInstance(Class clazz)
  5. newInstance(Class clazz)
  6. newInstance(Class clazz)
  7. newInstance(Class clazz, Class[] argumentTypes, Object[] arguments)
  8. newInstance(Class clazz, Class[] parameterTypes, Object[] initargs)
  9. newInstance(Class cls)