1 48 49 package com.caucho.hessian.io; 50 51 import java.io.IOException ; 52 import java.lang.reflect.Constructor ; 53 54 57 public class StringValueDeserializer extends AbstractDeserializer { 58 private Class _cl; 59 private Constructor _constructor; 60 61 public StringValueDeserializer(Class cl) 62 { 63 try { 64 _cl = cl; 65 _constructor = cl.getConstructor(new Class [] { String .class }); 66 } catch (Exception e) { 67 throw new RuntimeException (e); 68 } 69 } 70 71 public Class getType() 72 { 73 return _cl; 74 } 75 76 public Object readMap(AbstractHessianInput in) 77 throws IOException 78 { 79 String value = null; 80 81 while (! in.isEnd()) { 82 String key = in.readString(); 83 84 if (key.equals("value")) 85 value = in.readString(); 86 else 87 in.readObject(); 88 } 89 90 in.readMapEnd(); 91 92 return create(value); 93 } 94 95 public Object readObject(AbstractHessianInput in, String []fieldNames) 96 throws IOException 97 { 98 String value = null; 99 100 for (int i = 0; i < fieldNames.length; i++) { 101 if ("value".equals(fieldNames[i])) 102 value = in.readString(); 103 else 104 in.readObject(); 105 } 106 107 return create(value); 108 } 109 110 private Object create(String value) 111 throws IOException 112 { 113 if (value == null) 114 throw new IOException (_cl.getName() + " expects name."); 115 116 try { 117 return _constructor.newInstance(new Object [] { value }); 118 } catch (Exception e) { 119 throw new IOExceptionWrapper(e); 120 } 121 } 122 } 123 | Popular Tags |