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