1 48 49 package com.caucho.hessian.io; 50 51 import java.io.IOException ; 52 import java.lang.reflect.Constructor ; 53 import java.lang.reflect.Method ; 54 import java.lang.reflect.Modifier ; 55 import java.util.HashMap ; 56 57 60 public class BeanDeserializer extends AbstractMapDeserializer { 61 private Class _type; 62 private HashMap _methodMap; 63 private Method _readResolve; 64 private Constructor _constructor; 65 private Object []_constructorArgs; 66 67 public BeanDeserializer(Class cl) 68 { 69 _type = cl; 70 _methodMap = getMethodMap(cl); 71 72 _readResolve = getReadResolve(cl); 73 74 Constructor []constructors = cl.getConstructors(); 75 int bestLength = Integer.MAX_VALUE; 76 77 for (int i = 0; i < constructors.length; i++) { 78 if (constructors[i].getParameterTypes().length < bestLength) { 79 _constructor = constructors[i]; 80 bestLength = _constructor.getParameterTypes().length; 81 } 82 } 83 84 if (_constructor != null) { 85 _constructor.setAccessible(true); 86 Class []params = _constructor.getParameterTypes(); 87 _constructorArgs = new Object [params.length]; 88 for (int i = 0; i < params.length; i++) { 89 _constructorArgs[i] = getParamArg(params[i]); 90 } 91 } 92 } 93 94 public Class getType() 95 { 96 return _type; 97 } 98 99 public Object readMap(AbstractHessianInput in) 100 throws IOException 101 { 102 try { 103 Object obj = instantiate(); 104 105 return readMap(in, obj); 106 } catch (IOException e) { 107 throw e; 108 } catch (Exception e) { 109 throw new IOExceptionWrapper(e); 110 } 111 } 112 113 public Object readMap(AbstractHessianInput in, Object obj) 114 throws IOException 115 { 116 try { 117 int ref = in.addRef(obj); 118 119 while (! in.isEnd()) { 120 Object key = in.readObject(); 121 122 Method method = (Method ) _methodMap.get(key); 123 124 if (method != null) { 125 Object value = in.readObject(method.getParameterTypes()[0]); 126 127 method.invoke(obj, new Object [] {value }); 128 } 129 else { 130 Object value = in.readObject(); 131 } 132 } 133 134 in.readMapEnd(); 135 136 Object resolve = resolve(obj); 137 138 if (obj != resolve) 139 in.setRef(ref, resolve); 140 141 return resolve; 142 } catch (IOException e) { 143 throw e; 144 } catch (Exception e) { 145 throw new IOExceptionWrapper(e); 146 } 147 } 148 149 private Object resolve(Object obj) 150 { 151 try { 153 if (_readResolve != null) 154 return _readResolve.invoke(obj, new Object [0]); 155 } catch (Exception e) { 156 } 157 158 return obj; 159 } 160 161 protected Object instantiate() 162 throws Exception 163 { 164 return _constructor.newInstance(_constructorArgs); 165 } 166 167 170 protected Method getReadResolve(Class cl) 171 { 172 for (; cl != null; cl = cl.getSuperclass()) { 173 Method []methods = cl.getDeclaredMethods(); 174 175 for (int i = 0; i < methods.length; i++) { 176 Method method = methods[i]; 177 178 if (method.getName().equals("readResolve") && 179 method.getParameterTypes().length == 0) 180 return method; 181 } 182 } 183 184 return null; 185 } 186 187 190 protected HashMap getMethodMap(Class cl) 191 { 192 HashMap methodMap = new HashMap (); 193 194 for (; cl != null; cl = cl.getSuperclass()) { 195 Method []methods = cl.getDeclaredMethods(); 196 197 for (int i = 0; i < methods.length; i++) { 198 Method method = methods[i]; 199 200 if (Modifier.isStatic(method.getModifiers())) 201 continue; 202 203 String name = method.getName(); 204 205 if (! name.startsWith("set")) 206 continue; 207 208 Class []paramTypes = method.getParameterTypes(); 209 if (paramTypes.length != 1) 210 continue; 211 212 if (! method.getReturnType().equals(void.class)) 213 continue; 214 215 if (findGetter(methods, name, paramTypes[0]) == null) 216 continue; 217 218 try { 220 method.setAccessible(true); 221 } catch (Throwable e) { 222 e.printStackTrace(); 223 } 224 225 name = name.substring(3); 226 227 int j = 0; 228 for (; j < name.length() && Character.isUpperCase(name.charAt(j)); j++) { 229 } 230 231 if (j == 1) 232 name = name.substring(0, j).toLowerCase() + name.substring(j); 233 else if (j > 1) 234 name = name.substring(0, j - 1).toLowerCase() + name.substring(j - 1); 235 236 237 methodMap.put(name, method); 238 } 239 } 240 241 return methodMap; 242 } 243 244 247 private Method findGetter(Method []methods, String setterName, Class arg) 248 { 249 String getterName = "get" + setterName.substring(3); 250 251 for (int i = 0; i < methods.length; i++) { 252 Method method = methods[i]; 253 254 if (! method.getName().equals(getterName)) 255 continue; 256 257 if (! method.getReturnType().equals(arg)) 258 continue; 259 260 Class []params = method.getParameterTypes(); 261 262 if (params.length == 0) 263 return method; 264 } 265 266 return null; 267 } 268 269 272 protected static Object getParamArg(Class cl) 273 { 274 if (! cl.isPrimitive()) 275 return null; 276 else if (boolean.class.equals(cl)) 277 return Boolean.FALSE; 278 else if (byte.class.equals(cl)) 279 return new Byte ((byte) 0); 280 else if (short.class.equals(cl)) 281 return new Short ((short) 0); 282 else if (char.class.equals(cl)) 283 return new Character ((char) 0); 284 else if (int.class.equals(cl)) 285 return new Integer (0); 286 else if (long.class.equals(cl)) 287 return new Long (0); 288 else if (float.class.equals(cl)) 289 return new Double (0); 290 else if (double.class.equals(cl)) 291 return new Double (0); 292 else 293 throw new UnsupportedOperationException (); 294 } 295 } 296 | Popular Tags |