1 21 24 package org.lobobrowser.util; 25 26 28 import java.beans.*; 29 import java.lang.reflect.Method ; 30 import java.util.*; 31 32 35 public class Bean { 36 private final Class clazz; 38 39 public Bean(Class clazz) { 40 this.clazz = clazz; 41 } 42 43 private Map propertyDescriptors = null; 44 45 private void populateDescriptors(Map map, Class clazz) throws IntrospectionException { 46 BeanInfo beanInfo = Introspector.getBeanInfo(clazz); 47 PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); 48 for(int i = 0; i < pds.length; i++) { 49 map.put(pds[i].getName(), pds[i]); 50 } 51 if(clazz.isInterface()) { 52 java.lang.reflect.Type [] interfaces = clazz.getGenericInterfaces(); 53 for(int i = 0; i < interfaces.length; i++) { 54 this.populateDescriptors(map, (Class ) interfaces[i]); 55 } 56 } 57 } 58 59 public PropertyDescriptor getPropertyDescriptor(String propertyName) throws IntrospectionException { 60 synchronized(this) { 61 if(this.propertyDescriptors == null) { 62 this.propertyDescriptors = new HashMap(); 63 this.populateDescriptors(this.propertyDescriptors, this.clazz); 64 } 65 return (PropertyDescriptor) this.propertyDescriptors.get(propertyName); 66 } 67 } 68 69 public Map getPropertyDescriptorsMap() throws IntrospectionException { 70 synchronized(this) { 71 if(this.propertyDescriptors == null) { 72 this.propertyDescriptors = new HashMap(); 73 this.populateDescriptors(this.propertyDescriptors, this.clazz); 74 } 75 return this.propertyDescriptors; 76 } 77 } 78 79 public PropertyDescriptor[] getPropertyDescriptors() throws IntrospectionException { 80 synchronized(this) { 81 return (PropertyDescriptor[]) this.getPropertyDescriptorsMap().values().toArray(new PropertyDescriptor[0]); 82 } 83 } 84 85 public void setPropertyForFQN(Object receiver, String fullyQualifiedPropertyName, Object value) throws Exception { 86 int idx = fullyQualifiedPropertyName.indexOf('.'); 87 if(idx == -1) { 88 PropertyDescriptor pd = this.getPropertyDescriptor(fullyQualifiedPropertyName); 89 if(pd == null) { 90 throw new IllegalStateException ("Property '" + fullyQualifiedPropertyName + "' unknown"); 91 } 92 Method method = pd.getWriteMethod(); 93 if(method == null) { 94 throw new IllegalStateException ("Property '" + fullyQualifiedPropertyName + "' not settable"); 95 } 96 Object actualValue = convertValue(value, pd.getPropertyType()); 97 method.invoke(receiver, new Object [] { actualValue }); 98 } 99 else { 100 String prefix = fullyQualifiedPropertyName.substring(0, idx); 101 PropertyDescriptor pinfo = this.getPropertyDescriptor(prefix); 102 if(pinfo == null) { 103 throw new IllegalStateException ("Property '" + prefix + "' unknown"); 104 } 105 Method readMethod = pinfo.getReadMethod(); 106 if(readMethod == null) { 107 throw new IllegalStateException ("Property '" + prefix + "' not readable"); 108 } 109 Object newReceiver = readMethod.invoke(receiver, new Object [0]); 110 String nameRest = fullyQualifiedPropertyName.substring(idx + 1); 112 this.setPropertyForFQN(newReceiver, nameRest, value); 113 } 114 } 115 116 private static Object convertValue(Object value, Class targetType) { 117 boolean targetString = targetType.isAssignableFrom(String .class); 118 if(value instanceof String && targetString) { 119 } 121 else if(targetString) { 122 value = String.valueOf(value); 123 } 124 else if(!(value instanceof Byte ) && (targetType == Byte .class || targetType == byte.class)) { 125 value = Byte.valueOf(String.valueOf(value)); 126 } 127 else if(!(value instanceof Boolean ) && (targetType == Boolean .class || targetType == boolean.class)) { 128 value = Boolean.valueOf(String.valueOf(value)); 129 } 130 else if(!(value instanceof Short ) && (targetType == Short .class || targetType == short.class)) { 131 value = Short.valueOf(String.valueOf(value)); 132 } 133 else if(!(value instanceof Integer ) && (targetType == Integer .class || targetType == int.class)) { 134 value = Integer.valueOf(String.valueOf(value)); 135 } 136 else if(!(value instanceof Long ) && (targetType == Long .class || targetType == long.class)) { 137 value = Long.valueOf(String.valueOf(value)); 138 } 139 return value; 140 } 141 142 } 143 | Popular Tags |