Java Class New Instance newInstance(Class type, Class[] parameterTypes, Object[] args)

Here you can find the source of newInstance(Class type, Class[] parameterTypes, Object[] args)

Description

new Instance

License

Apache License

Declaration

public static <T> T newInstance(Class<T> type,
            Class<?>[] parameterTypes, Object[] args) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Constructor;

public class Main {
    public static <T> T newInstance(Class<T> type,
            Class<?>[] parameterTypes, Object[] args) throws Exception {
        Constructor<T> constructor = getConstructor(type, parameterTypes);
        constructor.setAccessible(true);
        return constructor.newInstance(args);
    }//  w w w  . j a v a 2 s.  c  o  m

    private static <T> Constructor<T> getConstructor(Class<T> type,
            Class<?>[] parameterTypes) throws Exception {
        try {
            return type.getConstructor(parameterTypes);
        } catch (NoSuchMethodException e1) {
            try {
                return type.getDeclaredConstructor(parameterTypes);
            } catch (NoSuchMethodException e2) {
                throw e2;
            }
        }
    }
}

Related

  1. newInstance(Class type)
  2. newInstance(Class type)
  3. newInstance(Class type)
  4. newInstance(Class type)
  5. newInstance(Class type, Class declarator, Annotation annotation)
  6. newInstance(Class type, Object... args)
  7. newInstance(Class type, Object... params)
  8. newInstance(Constructor c, List parameters)
  9. newInstance(Constructor constructor, Object... args)

  10. HOME | Copyright © www.java2s.com 2016