Java Reflection Constructor Invoke invokeConstructor(final Constructor constructor, final Object... args)

Here you can find the source of invokeConstructor(final Constructor constructor, final Object... args)

Description

Invoke the specified Constructor with the supplied arguments.

License

Apache License

Parameter

Parameter Description
constructor The method to invoke
args The invocation arguments (may be <code>null</code>)

Exception

Parameter Description
IllegalAccessException when unable to access the specified constructor because access modifiers prevent it
InstantiationException when an instantiation fails

Return

The invocation result, if any

Declaration

public static Object invokeConstructor(final Constructor constructor, final Object... args)
        throws InvocationTargetException, IllegalAccessException, InstantiationException 

Method Source Code


//package com.java2s;
/*//from   w  w w.  j  a  va 2 s  .  c om
 * Copyright (C) 2011 the original author or authors.
 *
 * 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.
 */

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

public class Main {
    /**
     * Invoke the specified {@link Constructor}  with the supplied arguments.
     *
     * @param constructor The method to invoke
     * @param args The invocation arguments (may be <code>null</code>)
     * @return The invocation result, if any
     * @throws IllegalAccessException when unable to access the specified constructor because access modifiers prevent it
     * @throws java.lang.reflect.InvocationTargetException when a reflection invocation fails
     * @throws InstantiationException when an instantiation fails
     */
    public static Object invokeConstructor(final Constructor constructor, final Object... args)
            throws InvocationTargetException, IllegalAccessException, InstantiationException {
        if (constructor == null) {
            throw new IllegalArgumentException("Constructor must not be null.");
        }
        constructor.setAccessible(true);
        return constructor.newInstance(args);
    }
}

Related

  1. invokeConstructor(Class cls)
  2. invokeConstructor(Class type, Object value)
  3. invokeConstructor(Constructor constructor, Object... arguments)
  4. invokeConstructor(Constructor constructor, Object... parameters)
  5. invokeConstructor(final Class clazz, final Class[] paramTypes, final Object... args)
  6. invokeConstructor(final Constructor constructor, final Object[] args)
  7. invokeConstructor(final Constructor constructor, final Object... args)
  8. invokeConstructor(String className, Class[] paramTypes, Object[] paramValues)
  9. invokeConstructor(String className, Object... arguments)