1 48 49 package com.caucho.hessian.io; 50 51 import java.io.IOException ; 52 import java.lang.reflect.Method ; 53 import java.lang.reflect.Modifier ; 54 import java.util.ArrayList ; 55 56 59 public class BeanSerializer extends AbstractSerializer { 60 private Method []_methods; 61 private String []_names; 62 63 private Method _writeReplace; 64 65 public BeanSerializer(Class cl) 66 { 67 _writeReplace = getWriteReplace(cl); 68 69 ArrayList primitiveMethods = new ArrayList (); 70 ArrayList compoundMethods = new ArrayList (); 71 72 for (; cl != null; cl = cl.getSuperclass()) { 73 Method []methods = cl.getDeclaredMethods(); 74 75 for (int i = 0; i < methods.length; i++) { 76 Method method = methods[i]; 77 78 if (Modifier.isStatic(method.getModifiers())) 79 continue; 80 81 if (method.getParameterTypes().length != 0) 82 continue; 83 84 String name = method.getName(); 85 86 if (! name.startsWith("get")) 87 continue; 88 89 Class type = method.getReturnType(); 90 91 if (type.equals(void.class)) 92 continue; 93 94 if (findSetter(methods, name, type) == null) 95 continue; 96 97 method.setAccessible(true); 99 100 if (type.isPrimitive() || 101 type.getName().startsWith("java.lang.") && 102 ! type.equals(Object .class)) 103 primitiveMethods.add(method); 104 else 105 compoundMethods.add(method); 106 } 107 } 108 109 ArrayList methodList = new ArrayList (); 110 methodList.addAll(primitiveMethods); 111 methodList.addAll(compoundMethods); 112 113 _methods = new Method [methodList.size()]; 114 methodList.toArray(_methods); 115 116 _names = new String [_methods.length]; 117 118 for (int i = 0; i < _methods.length; i++) { 119 String name = _methods[i].getName(); 120 121 name = name.substring(3); 122 123 int j = 0; 124 for (; j < name.length() && Character.isUpperCase(name.charAt(j)); j++) { 125 } 126 127 if (j == 1) 128 name = name.substring(0, j).toLowerCase() + name.substring(j); 129 else if (j > 1) 130 name = name.substring(0, j - 1).toLowerCase() + name.substring(j - 1); 131 132 _names[i] = name; 133 } 134 } 135 136 139 protected Method getWriteReplace(Class cl) 140 { 141 for (; cl != null; cl = cl.getSuperclass()) { 142 Method []methods = cl.getDeclaredMethods(); 143 144 for (int i = 0; i < methods.length; i++) { 145 Method method = methods[i]; 146 147 if (method.getName().equals("writeReplace") && 148 method.getParameterTypes().length == 0) 149 return method; 150 } 151 } 152 153 return null; 154 } 155 156 public void writeObject(Object obj, AbstractHessianOutput out) 157 throws IOException 158 { 159 if (out.addRef(obj)) 160 return; 161 162 Class cl = obj.getClass(); 163 164 try { 165 if (_writeReplace != null) { 166 Object repl = _writeReplace.invoke(obj, new Object [0]); 167 168 out.removeRef(obj); 169 170 out.writeObject(repl); 171 172 out.replaceRef(repl, obj); 173 174 return; 175 } 176 } catch (Exception e) { 177 } 178 179 int ref = out.writeObjectBegin(cl.getName()); 180 181 if (ref < 0) { 182 184 for (int i = 0; i < _methods.length; i++) { 185 Method method = _methods[i]; 186 Object value = null; 187 188 try { 189 value = _methods[i].invoke(obj, (Object []) null); 190 } catch (Throwable e) { 191 } 193 194 out.writeString(_names[i]); 195 196 out.writeObject(value); 197 } 198 199 out.writeMapEnd(); 200 } 201 else { 202 if (ref == 0) { 203 out.writeInt(_names.length); 204 205 for (int i = 0; i < _names.length; i++) 206 out.writeString(_names[i]); 207 } 208 209 for (int i = 0; i < _methods.length; i++) { 210 Method method = _methods[i]; 211 Object value = null; 212 213 try { 214 value = _methods[i].invoke(obj, (Object []) null); 215 } catch (Throwable e) { 216 } 218 219 out.writeObject(value); 220 } 221 } 222 } 223 224 227 private Method findSetter(Method []methods, String getterName, Class arg) 228 { 229 String setterName = "set" + getterName.substring(3); 230 231 for (int i = 0; i < methods.length; i++) { 232 Method method = methods[i]; 233 234 if (! method.getName().equals(setterName)) 235 continue; 236 237 if (! method.getReturnType().equals(void.class)) 238 continue; 239 240 Class []params = method.getParameterTypes(); 241 242 if (params.length == 1 && params[0].equals(arg)) 243 return method; 244 } 245 246 return null; 247 } 248 } 249 | Popular Tags |