Created
August 2, 2013 21:01
-
-
Save jvmvik/6143429 to your computer and use it in GitHub Desktop.
Simple Bean Mapper
---- Turn a Map<String, String> into an instance of any object an object. * key is the property name * value is the value associate to the property A key "class" must refer to the class name that must be instantiated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.milkyway.spendtracker.servlet; | |
import java.beans.*; | |
import java.io.IOException; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Type; | |
import java.util.Map; | |
/** | |
* Simple Bean Mapper | |
* | |
* Convert Map<String, String> to an object where the map : | |
* - key is the property name | |
* - value is the value associate to the property | |
* | |
* The clazz of the object must be defined: | |
* | |
* Learn by example: | |
* <code> | |
* class Cat | |
* { | |
* String name; | |
* boolean male; | |
* } | |
* | |
* // test | |
* Map params = new Map(); | |
* params.put("clazz", Cat.class.getName()); | |
* params.put("name", "Bob"); | |
* params.put("male", "true"); | |
* | |
* Cat cat = (Cat)BeanMapper.create(params); | |
* assert cat.name == "Bob"; | |
* </code> | |
* | |
* @author vicben01 | |
*/ | |
public class SimpleBeanMapper | |
{ | |
public static Object create(Map<String, String> params) throws IOException, ClassNotFoundException, IntrospectionException, InvocationTargetException, IllegalAccessException | |
{ | |
if (params.get("clazz") == null) | |
throw new NullPointerException("Bean is not defined, clazz == null"); | |
ClassLoader cl = Thread.currentThread().getContextClassLoader(); | |
Object instance = Beans.instantiate(cl, params.get("clazz")); | |
BeanInfo bean = Introspector.getBeanInfo(instance.getClass()); | |
for (PropertyDescriptor property : bean.getPropertyDescriptors()) | |
{ | |
Method method = property.getWriteMethod(); | |
if (method != null) | |
{ | |
String name = toVarName(method.getName()); | |
String value = params.get(name); | |
Type type = method.getGenericParameterTypes()[0]; | |
String s = type.toString(); | |
if ("boolean".equals(s)) | |
{ | |
method.invoke(instance, new Boolean(value)); | |
} | |
else if ("int".equals(s)) | |
{ | |
method.invoke(instance, new Integer(value)); | |
} | |
else if(s.contains("float")) | |
{ | |
method.invoke(instance, new Float(value)); | |
} | |
else if(s.contains("long")) | |
{ | |
method.invoke(instance, new Long(value.replace("L",""))); | |
} | |
else if(s.contains("double")) | |
{ | |
method.invoke(instance, new Double(value)); | |
} | |
else if(s.contains("String")) | |
{ | |
method.invoke(instance, value); | |
} | |
else | |
{ | |
// do nothing | |
} | |
} | |
} | |
return instance; | |
} | |
private static String toVarName(String methodName) | |
{ | |
String s = methodName.replace("set", ""); | |
return s.substring(0,1).toLowerCase() + s.substring(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment