1 48 49 package com.caucho.hessian.io; 50 51 import java.io.IOException ; 52 import java.io.InputStream ; 53 import java.lang.reflect.Field ; 54 import java.lang.reflect.Method ; 55 import java.lang.reflect.Modifier ; 56 import java.util.ArrayList ; 57 import java.util.HashMap ; 58 59 88 public class HessianSerializerInput extends HessianInput { 89 95 public HessianSerializerInput(InputStream is) 96 { 97 super(is); 98 } 99 100 103 public HessianSerializerInput() 104 { 105 } 106 107 111 protected Object readObjectImpl(Class cl) 112 throws IOException 113 { 114 try { 115 Object obj = cl.newInstance(); 116 117 if (_refs == null) 118 _refs = new ArrayList (); 119 _refs.add(obj); 120 121 HashMap fieldMap = getFieldMap(cl); 122 123 int code = read(); 124 for (; code >= 0 && code != 'z'; code = read()) { 125 _peek = code; 126 127 Object key = readObject(); 128 129 Field field = (Field ) fieldMap.get(key); 130 131 if (field != null) { 132 Object value = readObject(field.getType()); 133 field.set(obj, value); 134 } 135 else { 136 Object value = readObject(); 137 } 138 } 139 140 if (code != 'z') 141 throw expect("map", code); 142 143 try { 145 Method method = cl.getMethod("readResolve", new Class [0]); 146 return method.invoke(obj, new Object [0]); 147 } catch (Exception e) { 148 } 149 150 return obj; 151 } catch (IOException e) { 152 throw e; 153 } catch (Exception e) { 154 throw new IOExceptionWrapper(e); 155 } 156 } 157 158 161 protected HashMap getFieldMap(Class cl) 162 { 163 HashMap fieldMap = new HashMap (); 164 165 for (; cl != null; cl = cl.getSuperclass()) { 166 Field []fields = cl.getDeclaredFields(); 167 for (int i = 0; i < fields.length; i++) { 168 Field field = fields[i]; 169 170 if (Modifier.isTransient(field.getModifiers()) || 171 Modifier.isStatic(field.getModifiers())) 172 continue; 173 174 field.setAccessible(true); 176 177 fieldMap.put(field.getName(), field); 178 } 179 } 180 181 return fieldMap; 182 } 183 } 184 | Popular Tags |