Java Class New Instance newInstance(final Class clazz, final Object[] parameters)

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

Description

new Instance

License

Open Source License

Declaration

public static Object newInstance(final Class<?> clazz, final Object[] parameters)
            throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
            InstantiationException, IllegalAccessException, InvocationTargetException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 SAP AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   ww  w.  j av a  2  s  .  co  m
 *     SAP AG - initial API and implementation
 *******************************************************************************/

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static Object newInstance(final Class<?> clazz, final Object[] parameters)
            throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
            InstantiationException, IllegalAccessException, InvocationTargetException {
        assert clazz != null && parameters != null;
        return newInstance(clazz, createParameterTypes(parameters), parameters);
    }

    public static Object newInstance(final Class<?> clazz, final Class<?>[] parTypes, final Object[] parameters)
            throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
            InstantiationException, IllegalAccessException, InvocationTargetException {
        assert clazz != null && parTypes != null && parameters != null;
        final Constructor<?> constr = clazz.getDeclaredConstructor(parTypes);
        constr.setAccessible(true);
        return constr.newInstance(parameters);
    }

    private static Class<?>[] createParameterTypes(final Object[] parameters) {
        assert parameters != null;
        final List<Class<?>> types = new ArrayList<Class<?>>();

        for (final Object object : parameters) {
            types.add(object.getClass());
        }
        return types.toArray(new Class[types.size()]);
    }
}

Related

  1. newInstance(Constructor constructor, Object... args)
  2. newInstance(Constructor constructor, Object... arguments)
  3. newInstance(Constructor ctor, Object... params)
  4. newInstance(Field field)
  5. newInstance(final Class clazz)
  6. newInstance(final Class arrayClass, final int length)
  7. newInstance(final Class clazz)
  8. newInstance(final Class clazz)
  9. newInstance(final Class clazz)