1 34 35 36 package com.micronova.util; 37 38 import java.util.*; 39 import java.io.*; 40 import java.beans.*; 41 import java.lang.reflect.*; 42 43 44 45 public class BeanUtil 46 { 47 public static String encode(Object object) throws Exception 48 { 49 if (object != null) 50 { 51 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 52 53 XMLEncoder encoder = new XMLEncoder(bOut); 54 55 encoder.writeObject(object); 56 57 encoder.close(); 58 59 return bOut.toString(); 60 } 61 62 return null; 63 } 64 65 public static Object decode(String string) throws Exception 66 { 67 Object object = null; 68 69 if (string != null) 70 { 71 ByteArrayInputStream bIn = new ByteArrayInputStream(string.getBytes("utf-8")); 72 XMLDecoder decoder = new XMLDecoder(bIn); 73 74 object = decoder.readObject(); 75 76 decoder.close(); 77 78 return object; 79 } 80 81 return null; 82 } 83 84 85 86 public static final Object getBeanProperty(Object object, String name) throws Exception 87 { 88 PropertyDescriptor pd[] = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors(); 89 90 for (int i = pd.length; --i >= 0;) 91 { 92 String propertyName = pd[i].getName(); 93 94 if (name.equals(propertyName)) 95 { 96 Method m = pd[i].getReadMethod(); 97 98 return m.invoke(object, (Object [])null); 99 } 100 } 101 102 throw new Exception ("bean property " + name + " not found"); 103 } 104 105 106 107 public static final Object getProperty(Object object, String name) throws Exception 108 { 109 Class objectClass = object.getClass(); 110 111 if (object instanceof Map) 112 { 113 return ((Map)object).get(name); 114 } 115 else if (object instanceof List) 116 { 117 List list = (List)object; 118 119 int index = Integer.parseInt(name); 120 121 if (index < 0) 122 { 123 index = list.size() + index; 124 } 125 126 return list.get(index); 127 } 128 else if (objectClass.isArray()) 129 { 130 int index = Integer.parseInt(name); 131 132 if (index < 0) 133 { 134 index = Array.getLength(object) + index; 135 } 136 137 return Array.get(object, index); 138 } 139 140 return getBeanProperty(object, name); 141 } 142 143 144 145 public static final Object setBeanProperty(Object object, String name, Object value) throws Exception 146 { 147 PropertyDescriptor pd[] = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors(); 148 149 for (int i = pd.length; --i >= 0;) 150 { 151 String propertyName = pd[i].getName(); 152 153 if (name.equals(propertyName)) 154 { 155 Method m = pd[i].getWriteMethod(); 156 157 Class [] parameterTypes = m.getParameterTypes(); 158 159 Object convertedValue = TypeUtil.isClass(value, parameterTypes[0]); 160 161 return m.invoke(object, new Object []{convertedValue}); 162 } 163 } 164 165 throw new Exception ("bean property " + name + " not found"); 166 } 167 168 169 170 171 public static final Object setProperty(Object object, String name, Object value) throws Exception 172 { 173 Class objectClass = object.getClass(); 174 175 if (object instanceof Map) 176 { 177 return ((Map)object).put(name, value); 178 } 179 else if (object instanceof List) 180 { 181 List list = (List)object; 182 183 Object returnObject = null; 184 185 int listSize = list.size(); 186 187 int index = -1; 188 189 boolean isInsertMode = false; 190 boolean isIgnoreNullMode = false; 191 192 if ("".equals(name)) 193 { 194 name = "*"; 195 } 196 197 if (name.startsWith("*")) 198 { 199 isInsertMode = true; 200 name = name.substring(1); 201 202 if (name.startsWith("*")) 203 { 204 isIgnoreNullMode = true; 205 name = name.substring(1); 206 } 207 } 208 209 if (name.length() == 0) 210 { 211 index = listSize; 212 } 213 else 214 { 215 index = Integer.parseInt(name); 216 217 if (index < 0) 218 { 219 index = listSize + index; 220 } 221 } 222 223 if (isInsertMode) 224 { 225 if (value != null) 226 { 227 if (index == listSize) 228 { 229 list.add(value); 230 } 231 else 232 { 233 list.add(index, value); 234 } 235 } 236 else if ((listSize > 0) && (!isIgnoreNullMode)) 237 { 238 if (index == listSize) 239 { 240 returnObject = list.remove(listSize - 1); 241 } 242 else 243 { 244 returnObject = list.remove(index); 245 } 246 } 247 } 248 else 249 { 250 returnObject = list.set(index, value); 251 } 252 253 return returnObject; 254 } 255 else if (objectClass.isArray()) 256 { 257 Class componentClass = objectClass.getComponentType(); 258 259 int index = Integer.parseInt(name); 260 261 if (index < 0) 262 { 263 index = Array.getLength(object) + index; 264 } 265 266 Object currentValue = Array.get(object, index); 267 268 Array.set(object, index, TypeUtil.isClass(value, componentClass)); 269 270 return currentValue; 271 } 272 273 return setBeanProperty(object, name, value); 274 } 275 276 277 278 public static Object fill(Object object, Map map) throws Exception 279 { 280 if (object != null) 281 { 282 PropertyDescriptor pd[] = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors(); 283 284 Object [] arg = new Object [1]; 285 286 for (int i = 0; i < pd.length; i++) 287 { 288 String name = pd[i].getName(); 289 290 Object propertyValue = map.get(name); 291 292 if (propertyValue != null) 293 { 294 Method m = pd[i].getWriteMethod(); 295 296 Class [] parameterTypes = m.getParameterTypes(); 297 298 Object convertedValue = TypeUtil.isClass(propertyValue, parameterTypes[0]); 299 arg[0] = convertedValue; 300 301 m.invoke(object, arg); 302 } 303 } 304 } 305 306 return object; 307 } 308 309 310 311 public static NestedMap getMap(Object object) throws Exception 312 { 313 PropertyDescriptor pd[] = Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors(); 314 315 NestedMap map = new NestedMap(); 316 317 for (int i = 0; i < pd.length; i++) 318 { 319 String name = pd[i].getName(); 320 321 Object propertyValue = map.get(name); 322 323 if (propertyValue == null) 324 { 325 Method m = pd[i].getReadMethod(); 326 327 if (m != null) 328 { 329 propertyValue = m.invoke(object, (Object [])null); 330 331 map.put(name, propertyValue); 332 } 333 } 334 } 335 336 return map; 337 } 338 } 339 | Popular Tags |