Java Class New Instance newInstance(Class clazz, Class[] argumentTypes, Object[] arguments)

Here you can find the source of newInstance(Class clazz, Class[] argumentTypes, Object[] arguments)

Description

new Instance

License

Apache License

Declaration

public static <T> T newInstance(Class<T> clazz, Class<?>[] argumentTypes, Object[] arguments) 

Method Source Code

//package com.java2s;
/**/*from   w w w  . ja  v 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;

public class Main {
    public static <T> T newInstance(Class<T> clazz, Class<?>[] argumentTypes, Object[] arguments) {
        if (argumentTypes == null || argumentTypes.length < 1) {
            return newDefaultInstance(clazz);
        }
        try {
            Constructor<T> ctr = clazz.getDeclaredConstructor(argumentTypes);
            ctr.setAccessible(true);
            return ctr.newInstance(arguments);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T newInstance(String className, Class<?>[] argumentTypes, Object[] arguments) {
        if (argumentTypes == null || argumentTypes.length < 1) {
            return newDefaultInstance(className);
        }
        try {
            Class<T> clazz = (Class<T>) Class.forName(className, false,
                    Thread.currentThread().getContextClassLoader());
            return newInstance(clazz, argumentTypes, arguments);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    public static <T> T newDefaultInstance(Class<T> clazz) {
        try {
            Constructor<T> ctr = clazz.getDeclaredConstructor();
            ctr.setAccessible(true);
            return ctr.newInstance();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T newDefaultInstance(String className) {
        try {
            Class<T> clazz = (Class<T>) Class.forName(className, false,
                    Thread.currentThread().getContextClassLoader());
            return newDefaultInstance(clazz);
        } catch (Exception e) {
            throw new IllegalStateException(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, Class[] parameterTypes, Object[] initargs)
  7. newInstance(Class cls)
  8. newInstance(Class cls)
  9. newInstance(Class cls, Object... args)