1 package org.openejb.util.io; 2 3 4 import java.io.IOException ; 5 import java.io.InvalidClassException ; 6 import java.lang.reflect.Field ; 7 8 public class FieldDescriptor implements java.io.Serializable , Comparable { 9 10 11 18 public FieldDescriptor(Field field){ 19 this.field = field; 20 this.name = field.getName(); 21 this.type = field.getType(); 22 23 if (type.isPrimitive()) { 24 if (type == Integer.TYPE) typeCode = 'I'; 25 else if (type == Byte.TYPE) typeCode = 'B'; 26 else if (type == Long.TYPE) typeCode = 'J'; 27 else if (type == Float.TYPE) typeCode = 'F'; 28 else if (type == Double.TYPE) typeCode = 'D'; 29 else if (type == Short.TYPE) typeCode = 'S'; 30 else if (type == Character.TYPE) typeCode = 'C'; 31 else if (type == Boolean.TYPE) typeCode = 'Z'; 32 else if (type == Void.TYPE) typeCode = 'V'; 33 } 34 else if (type.isArray()) { 35 typeCode = '['; 36 typeString = ClassDescriptor.getSignature(type).toString().intern(); 37 } 38 else { 39 typeCode = 'L'; 40 StringBuffer buf = new StringBuffer (); 41 buf.append(typeCode); 42 buf.append(type.getName().replace('.', '/')); 43 buf.append(';'); 44 typeString = buf.toString().intern(); 45 } 46 47 } 48 49 public FieldDescriptor(String name, Class type){ 50 51 } 52 53 protected String typeString; 54 55 public String getTypeString(){ 56 return typeString; 57 } 58 59 62 protected String name; 63 64 public String getName(){ 65 return name; 66 } 67 68 public void setName(String name){ 69 this.name = name; 70 } 71 72 protected Field field; 73 74 public Field getField(){ 75 return field; 76 } 77 78 public void setField(Field field){ 79 this.field = field; 80 } 81 82 protected char typeCode; 83 84 public char getTypeCode(){ 85 return typeCode; 86 } 87 88 public void setTypeCode(char typeCode){ 89 this.typeCode = typeCode; 90 } 91 92 protected Class type; 93 94 100 public int compareTo(Object o) { 105 FieldDescriptor f2 = (FieldDescriptor)o; 106 boolean thisprim = (this.typeString == null); 107 boolean otherprim = (f2.typeString == null); 108 109 if (thisprim != otherprim) { 110 return (thisprim ? -1 : 1); 111 } 112 return this.name.compareTo(f2.name); 113 } 114 115 121 protected ClassDescriptor classDesc; 122 123 public ClassDescriptor getClassDescriptor(){ 124 return classDesc; 125 } 126 127 public void setClassDescriptor(ClassDescriptor classDesc){ 128 this.classDesc = classDesc; 129 } 130 131 public void writeDesc(ObjectOutputStream out) throws IOException { 132 out.writeByte((int)typeCode); 133 out.writeUTF(name); 134 if (!type.isPrimitive()) out.writeString(typeString); 135 } 136 137 public void write(Object o, ObjectOutputStream out) throws IOException , InvalidClassException { 138 139 if (field == null) throw new InvalidClassException (classDesc.forClass().getName(), "Nonexistent field " + name); 140 try { 141 switch (typeCode) { 142 case 'B': out.writeByte(field.getByte(o)); break; 143 case 'C': out.writeChar(field.getChar(o)); break; 144 case 'I': out.writeInt(field.getInt(o)); break; 145 case 'Z': out.writeBoolean(field.getBoolean(o)); break; 146 case 'J': out.writeLong(field.getLong(o)); break; 147 case 'F': out.writeFloat(field.getFloat(o)); break; 148 case 'D': out.writeDouble(field.getDouble(o)); break; 149 case 'S': out.writeShort(field.getShort(o)); break; 150 case '[': 151 case 'L': out.writeObject(field.get(o)); break; 152 default: throw new InvalidClassException (classDesc.forClass().getName()); 153 } 154 } 155 catch (IllegalAccessException e) { 156 throw new InvalidClassException (classDesc.forClass().getName(), e.getMessage()); 157 } 158 } 159 } | Popular Tags |