Description
Searches for the correct constructor and instantiates a new object with the given parameters.
License
Open Source License
Parameter
Parameter | Description |
---|
constructorClass | the class that contains the constructor |
parameters | the parameters for the constructor |
Exception
Parameter | Description |
---|
IllegalAccessException | an exception |
InstantiationException | an exception |
InvocationTargetException | an exception |
IllegalArgumentException | an exception |
Return
a new instance created with the correct constructor
Declaration
public static Object newInstance(Class constructorClass, Object[] parameters) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException
Method Source Code
//package com.java2s;
/*//from ww w. j a va2 s . c om
* Created on 16-Jul-2004 at 02:57:35.
*
* Copyright (c) 2004-2005 Robert Virkus / Enough Software
*
* This file is part of J2ME Polish.
*
* J2ME Polish is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* J2ME Polish is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with J2ME Polish; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Commercial licenses are also available, please
* refer to the accompanying LICENSE.txt or visit
* http://www.j2mepolish.org for details.
*/
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
public class Main {
private final static HashMap PRIMITIVE_CLASS_WRAPPERS = new HashMap();
/**
* Searches for the correct constructor and instantiates a new object with the given parameters.
* @param constructorClass the class that contains the constructor
* @param parameters the parameters for the constructor
* @return a new instance created with the correct constructor
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static Object newInstance(Class constructorClass, Object[] parameters) throws InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException {
if (parameters == null || parameters.length == 0) {
return constructorClass.newInstance();
}
Constructor[] constructors = constructorClass.getConstructors();
for (int i = 0; i < constructors.length; i++) {
Constructor constructor = constructors[i];
if (constructorFits(constructor, parameters)) {
return constructor.newInstance(parameters);
}
}
throw new IllegalArgumentException("Found no matching constructor in class " + constructorClass.getName()
+ " for parameters " + parameters.length);
}
/**
* @param constructor
* @param parameters
* @return true when the contructor fits
*/
public static boolean constructorFits(Constructor constructor, Object[] parameters) {
return signatureFits(constructor.getParameterTypes(), parameters);
}
/**
* @param parameterTypes
* @param parameters
* @return true when the signature fits
*/
private static boolean signatureFits(Class[] parameterTypes, Object[] parameters) {
if ((parameterTypes == null && parameters != null) || (parameterTypes != null && parameters == null)) {
return false;
}
if (parameterTypes != null && parameters != null && parameterTypes.length != parameters.length) {
return false;
}
if (parameterTypes == null && parameters == null) {
return true;
}
for (int i = 0; i < parameters.length; i++) {
Object parameter = parameters[i];
Class parameterType = parameterTypes[i];
if (parameter == null) {
if (parameterType.isPrimitive()) {
return false;
}
continue; // null fits any class
}
Class parameterClass = parameter.getClass();
if (!(parameterType.isAssignableFrom(parameterClass)
|| (parameterType.isPrimitive() && isCompatible(parameterType, parameterClass)))) {
// System.out.println("- signature-mismatch: wanted=" + parameterType + ", have=" + parameter.getClass() );
return false;
// } else {
// System.out.println("+ signature-match: wanted=" + parameterType + ", have=" + parameter.getClass() );
}
}
return true;
}
/**
* @param parameterType
* @param parameterClass
* @return
*/
private static boolean isCompatible(Class parameterType, Class parameterClass) {
Class wrapperClass = (Class) PRIMITIVE_CLASS_WRAPPERS.get(parameterType);
if (wrapperClass != null) {
return wrapperClass.isAssignableFrom(parameterClass);
}
return false;
}
}
Related
- newInstance(Class clazz)
- newInstance(Class clazzName)
- newInstance(Class cls)
- newInstance(Class cls)
- newInstance(Class cmdClass)
- newInstance(Class klass)
- newInstance(Class targetClass)
- newInstance(Class extends Object> cl, Object[] args)
- newInstance(Class extends T> clazz)