1 package org.shiftone.cache.config; 2 3 4 5 import org.shiftone.cache.util.Log; 6 7 import java.lang.reflect.InvocationTargetException ; 8 import java.lang.reflect.Method ; 9 import java.util.HashMap ; 10 import java.util.Map ; 11 12 13 19 public class BeanWrapper 20 { 21 22 private static final Log LOG = new Log(BeanWrapper.class); 23 private final Object object; 24 private final Class klass; 25 private Method [] methods; 26 private Map setters = new HashMap (); 27 28 public BeanWrapper(Object object) 29 { 30 31 this.object = object; 32 this.klass = object.getClass(); 33 this.methods = klass.getMethods(); 34 35 for (int i = 0; i < methods.length; i++) 36 { 37 Method method = methods[i]; 38 String name = method.getName().toLowerCase(); 39 40 if ((name.startsWith("set")) && (method.getParameterTypes().length == 1) && (method.getReturnType().equals(Void.TYPE))) 43 { 44 setters.put(name.substring(3), method); 45 46 } 48 } 49 } 50 51 52 public Class getWrappedObjectClass() 53 { 54 return klass; 55 } 56 57 58 public Object getWrappedObject() 59 { 60 return object; 61 } 62 63 64 public Method getSetter(String name) throws NoSuchMethodException 65 { 66 67 Method method = (Method ) setters.get(name.toLowerCase()); 68 69 if (method == null) 70 { 71 throw new NoSuchMethodException ("no setter for : " + name); 72 } 73 74 return method; 75 } 76 77 78 public Class getType(String name) throws NoSuchMethodException 79 { 80 return getSetter(name).getParameterTypes()[0]; 81 } 82 83 84 public void setProperty(String name, String value) throws NoSuchMethodException , IllegalAccessException , IllegalArgumentException , InvocationTargetException , ClassNotFoundException 85 { 86 setProperty(name, convert(value, getType(name))); 87 } 88 89 90 public void setProperty(String name, Object objectValue) throws NoSuchMethodException , IllegalAccessException , IllegalArgumentException , InvocationTargetException 91 { 92 LOG.debug(klass.getName() + " SET " + name + " -> " + objectValue); 93 getSetter(name).invoke(object, new Object []{ objectValue }); 94 } 95 96 97 private Object convert(String val, Class type) throws IllegalArgumentException , ClassNotFoundException 98 { 99 100 String v = val.trim(); 101 102 if (val == null) 103 { 104 return null; 105 } 106 else if (Class .class.isAssignableFrom(type)) 107 { 108 return Class.forName(val); 109 } 110 else if (String .class.isAssignableFrom(type)) 111 { 112 return val; 113 } 114 else if (Integer.TYPE.isAssignableFrom(type) || Integer .class.isAssignableFrom(type)) 115 { 116 return new Integer (v); 117 } 118 else if (Long.TYPE.isAssignableFrom(type) || Long .class.isAssignableFrom(type)) 119 { 120 return new Long (v); 121 } 122 else if (Boolean.TYPE.isAssignableFrom(type) || Boolean .class.isAssignableFrom(type)) 123 { 124 if ("true".equalsIgnoreCase(v)) 125 { 126 return Boolean.TRUE; 127 } 128 else if ("false".equalsIgnoreCase(v)) 129 { 130 return Boolean.FALSE; 131 } 132 } 133 134 throw new IllegalArgumentException ("unable to convert '" + v + "' to '" + type.getName() + "' on class '" + klass.getName() + "'"); 135 } 136 } 137 | Popular Tags |