Description
Creates a new instance of the given clazz, paramTypes, params
License
Open Source License
Parameter
Parameter | Description |
---|
clazz | clazz |
paramTypes | paramTypes |
params | params |
Exception
Parameter | Description |
---|
IllegalAccessException | exception |
InvocationTargetException | exception |
InstantiationException | exception |
NoSuchMethodException | exception |
Return
instance
Declaration
public static Object invokeConstructor(Class<?> clazz, Class<?>[] paramTypes, Object[] params)
throws IllegalAccessException, InvocationTargetException, InstantiationException,
NoSuchMethodException
Method Source Code
//package com.java2s;
/**/*from ww w . j a v a 2s. c o m*/
* Copyright 2017 Shynixn
* <p>
* Do not remove this header!
* <p>
* Version 1.0
* <p>
* MIT License
* <p>
* Copyright (c) 2016
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Main {
/**
* Creates a new instance of the given clazz, paramTypes, params
*
* @param clazz clazz
* @param paramTypes paramTypes
* @param params params
* @return instance
* @throws IllegalAccessException exception
* @throws InvocationTargetException exception
* @throws InstantiationException exception
* @throws NoSuchMethodException exception
*/
public static Object invokeConstructor(Class<?> clazz, Class<?>[] paramTypes, Object[] params)
throws IllegalAccessException, InvocationTargetException, InstantiationException,
NoSuchMethodException {
if (clazz == null)
throw new IllegalArgumentException("Class cannot be null!");
if (paramTypes == null)
throw new IllegalArgumentException("ParamTypes cannot be null");
if (params == null)
throw new IllegalArgumentException("Params cannot be null!");
final Constructor constructor = clazz.getDeclaredConstructor(paramTypes);
constructor.setAccessible(true);
return constructor.newInstance(params);
}
}
Related
- invoke(Constructor> constructor)
- invoke(Constructor constructor, Object... args)
- invoke(Constructor constructor, Object... args)
- invokeConstructor(Class> clazz, Object... parameters)
- invokeConstructor(Class> objectClass, Class>[] classes, Object... params)
- invokeConstructor(Class clazz, Class>[] argTypes, Object[] args)
- invokeConstructor(Class cls)