Index: lutinutil/src/java/org/codelutin/util/ObjectUtil.java diff -u lutinutil/src/java/org/codelutin/util/ObjectUtil.java:1.1 lutinutil/src/java/org/codelutin/util/ObjectUtil.java:1.2 --- lutinutil/src/java/org/codelutin/util/ObjectUtil.java:1.1 Thu Nov 4 13:51:54 2004 +++ lutinutil/src/java/org/codelutin/util/ObjectUtil.java Mon Nov 26 15:23:53 2007 @@ -23,14 +23,20 @@ * Created: 2 nov. 2004 * * @author Benjamin Poussin - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * - * Mise a jour: $Date: 2004-11-04 13:51:54 $ + * Mise a jour: $Date: 2007-11-26 15:23:53 $ * par : $Author: bpoussin $ */ package org.codelutin.util; +import java.lang.reflect.InvocationTargetException; + +import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.beanutils.PropertyUtils; +import org.apache.commons.beanutils.WrapDynaBean; + public class ObjectUtil { // ObjectUtil /** @@ -41,6 +47,53 @@ private ObjectUtil() {} /** + * Create new object from string like org.codelutin.Toto(name=machine, int=10) + * where machine and int is properties on org.codelutin.Toto object. + * Conversion between 10 in string and 10 as integer as automaticaly done + * + * For String property you can use ex: + *
  • name="my string with , in string" + *
  • name='my string with , in string' + * + * @param classnameAndProperties + * @return + * @throws ClassNotFoundException + * @throws IllegalAccessException + * @throws InstantiationException + * @throws NoSuchMethodException + * @throws InvocationTargetException + */ + public static Object create(String classnameAndProperties) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { + int p = classnameAndProperties.indexOf('('); + int l = classnameAndProperties.lastIndexOf(')'); + String [] properties = null; + String classname = null; + if (p != -1) { + String tmp = classnameAndProperties.substring(p + 1, l); + properties = StringUtil.split(tmp, ","); + classname = classnameAndProperties.substring(0, p); + } else { + classname = classnameAndProperties; + } + Class clazz = Thread.currentThread().getContextClassLoader().loadClass(classname); + Object o = clazz.newInstance(); + if (properties != null) { + for (String prop : properties) { + int e = prop.indexOf('='); + String propName = prop.substring(0, e).trim(); + String propValue = prop.substring(e+1).trim(); + if (propValue.charAt(0) == '"' && propValue.charAt(propValue.length()-1) == '"') { + propValue = propValue.substring(1, propValue.length()-1); + } else if (propValue.charAt(0) == '\'' && propValue.charAt(propValue.length()-1) == '\'') { + propValue = propValue.substring(1, propValue.length()-1); + } + BeanUtils.setProperty(o, propName, propValue); + } + } + return o; + } + + /** * Method toObject * * @param o Object to transform