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 EnumSerializer extends AbstractSerializer { 58 private Method _name; 59 60 public EnumSerializer(Class cl) 61 { 62 try { 63 _name = cl.getMethod("name", new Class [0]); 64 } catch (Exception e) { 65 throw new RuntimeException (e); 66 } 67 } 68 69 public void writeObject(Object obj, AbstractHessianOutput out) 70 throws IOException 71 { 72 if (out.addRef(obj)) 73 return; 74 75 Class cl = obj.getClass(); 76 77 String name = null; 78 try { 79 name = (String ) _name.invoke(obj, (Object []) null); 80 } catch (Exception e) { 81 throw new RuntimeException (e); 82 } 83 84 int ref = out.writeObjectBegin(cl.getName()); 85 86 if (ref < 0) { 87 out.writeString("name"); 88 out.writeString(name); 89 out.writeMapEnd(); 90 } 91 else { 92 if (ref == 0) { 93 out.writeClassFieldLength(1); 94 out.writeString("name"); 95 } 96 97 out.writeString(name); 98 } 99 } 100 } 101 | Popular Tags |