Here you can find the source of invokeConstructor(final Constructor constructor, final Object... args)
Parameter | Description |
---|---|
constructor | The method to invoke |
args | The invocation arguments (may be <code>null</code>) |
Parameter | Description |
---|---|
IllegalAccessException | when unable to access the specified constructor because access modifiers prevent it |
InstantiationException | when an instantiation fails |
public static Object invokeConstructor(final Constructor constructor, final Object... args) throws InvocationTargetException, IllegalAccessException, InstantiationException
//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); } }