[Lutinutil-commits] r1058 - trunk/lutinutil/src/main/java/org/codelutin/util
Author: bpoussin Date: 2008-08-22 22:49:37 +0000 (Fri, 22 Aug 2008) New Revision: 1058 Modified: trunk/lutinutil/src/main/java/org/codelutin/util/ObjectUtil.java Log: - ajout de methode permettant de creer de nouveau objet facilement newInstance(String classWithParams) newInstance(Class, String ... params) Modified: trunk/lutinutil/src/main/java/org/codelutin/util/ObjectUtil.java =================================================================== --- trunk/lutinutil/src/main/java/org/codelutin/util/ObjectUtil.java 2008-08-22 22:48:21 UTC (rev 1057) +++ trunk/lutinutil/src/main/java/org/codelutin/util/ObjectUtil.java 2008-08-22 22:49:37 UTC (rev 1058) @@ -32,6 +32,7 @@ package org.codelutin.util; import java.lang.reflect.Array; +import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import static org.codelutin.i18n.I18nf._; @@ -190,7 +191,111 @@ return result; } + public static Object newInstance(String constructorWithParams) throws ClassNotFoundException { + int p = constructorWithParams.indexOf('('); + int l = constructorWithParams.lastIndexOf(')'); + String [] params = null; + String classname = null; + if (p != -1) { + String tmp = constructorWithParams.substring(p + 1, l); + params = StringUtil.split(tmp, ","); + classname = constructorWithParams.substring(0, p); + } else { + classname = constructorWithParams; + } + Class clazz = Thread.currentThread().getContextClassLoader().loadClass(classname); + Object result = newInstance(clazz, params); + return result; + } /** + * Create new instance of clazz, call constructor with params as String. + * Each param is converted to required type for + * constructor with beanutils converter, first constructor that permit + * instanciation is used + * + * @param clazz class to instanciated + * @param params parameters for constructor call + * @return new instance of clazz + */ + public static <T> T newInstance(Class<T> clazz, String ... params) + throws IllegalArgumentException { + if (params == null) { + params = new String[0]; + } + List<Constructor<T>> constructors = getConstructor(clazz, params.length); + + for (Constructor<T> c : constructors) { + try { + Class[] types = c.getParameterTypes(); + + int last = types.length; + if (c.isVarArgs()) { + // on traite le dernier differement + last--; + } + + Object[] parameters = new Object[types.length]; + for (int i = 0; i < last; i++) { + String v = params[i]; + Class argClazz = types[i]; + Object t = convert(v, argClazz); + parameters[i] = t; + } + + if (c.isVarArgs()) { + Class argClazz = types[last]; // get var args type + argClazz = argClazz.getComponentType(); // get array component type + List tmp = new ArrayList(); + for (int i = last; i < params.length; i++) { + String v = params[i]; + Object t = convert(v, argClazz); + tmp.add(t); + } + parameters[last] = tmp.toArray((Object[]) Array.newInstance(argClazz, tmp.size())); + } + + if (log.isDebugEnabled()) { + log.debug(_("Try to create %s with %s", clazz, Arrays.toString(parameters))); + } + T result = c.newInstance(parameters); + + return result; + } catch(Exception eee) { + // this constructors don't work, try next + if (log.isDebugEnabled()) { + log.debug("Creation failed try with next constructor"); + } + } + } + throw new IllegalArgumentException(_("Can't instantiate %s with params %s", + clazz, Arrays.toString(params))); + } + + /** + * Get all constructors that support paramNumber as parameters numbers. + * Varargs is supported + * + * @param clazz la classe sur lequel rechercher le constructeur + * @param paramNumber le nombre de parametre souhaite pour le constructeur, + * -1 indique que tous les constructeur sont souhaite. + * @return + */ + public static <T> List<Constructor<T>> getConstructor(Class<T> clazz, int paramNumber) { + List<Constructor<T>> result = new ArrayList<Constructor<T>>(); + + Constructor[] constructors = clazz.getConstructors(); + for (Constructor c : constructors) { + if (paramNumber < 0 || + (c.isVarArgs() && c.getParameterTypes().length <= paramNumber - 1) || + c.getParameterTypes().length == paramNumber) { + result.add(c); + } + } + + return result; + } + + /** * Method toObject * * @param o Object to transform
participants (1)
-
bpoussin@users.labs.libre-entreprise.org