1 7 8 package java.io; 9 10 import java.lang.reflect.Field ; 11 12 22 public class ObjectStreamField 23 implements Comparable <Object > 24 { 25 26 27 private final String name; 28 29 private final String signature; 30 31 private final Class type; 32 33 private final boolean unshared; 34 35 private final Field field; 36 37 private int offset = 0; 38 39 46 public ObjectStreamField(String name, Class <?> type) { 47 this(name, type, false); 48 } 49 50 66 public ObjectStreamField(String name, Class <?> type, boolean unshared) { 67 if (name == null) { 68 throw new NullPointerException (); 69 } 70 this.name = name; 71 this.type = type; 72 this.unshared = unshared; 73 signature = ObjectStreamClass.getClassSignature(type).intern(); 74 field = null; 75 } 76 77 81 ObjectStreamField(String name, String signature, boolean unshared) { 82 if (name == null) { 83 throw new NullPointerException (); 84 } 85 this.name = name; 86 this.signature = signature.intern(); 87 this.unshared = unshared; 88 field = null; 89 90 switch (signature.charAt(0)) { 91 case 'Z': type = Boolean.TYPE; break; 92 case 'B': type = Byte.TYPE; break; 93 case 'C': type = Character.TYPE; break; 94 case 'S': type = Short.TYPE; break; 95 case 'I': type = Integer.TYPE; break; 96 case 'J': type = Long.TYPE; break; 97 case 'F': type = Float.TYPE; break; 98 case 'D': type = Double.TYPE; break; 99 case 'L': 100 case '[': type = Object .class; break; 101 default: throw new IllegalArgumentException ("illegal signature"); 102 } 103 } 104 105 113 ObjectStreamField(Field field, boolean unshared, boolean showType) { 114 this.field = field; 115 this.unshared = unshared; 116 name = field.getName(); 117 Class ftype = field.getType(); 118 type = (showType || ftype.isPrimitive()) ? ftype : Object .class; 119 signature = ObjectStreamClass.getClassSignature(ftype).intern(); 120 } 121 122 128 public String getName() { 129 return name; 130 } 131 132 142 public Class <?> getType() { 143 return type; 144 } 145 146 163 public char getTypeCode() { 165 return signature.charAt(0); 166 } 167 168 173 public String getTypeString() { 175 return isPrimitive() ? null : signature; 176 } 177 178 184 public int getOffset() { 186 return offset; 187 } 188 189 195 protected void setOffset(int offset) { 197 this.offset = offset; 198 } 199 200 205 public boolean isPrimitive() { 207 char tcode = signature.charAt(0); 208 return ((tcode != 'L') && (tcode != '[')); 209 } 210 211 215 public boolean isUnshared() { 216 return unshared; 217 } 218 219 225 public int compareTo(Object obj) { 227 ObjectStreamField other = (ObjectStreamField ) obj; 228 boolean isPrim = isPrimitive(); 229 if (isPrim != other.isPrimitive()) { 230 return isPrim ? -1 : 1; 231 } 232 return name.compareTo(other.name); 233 } 234 235 238 public String toString() { 239 return signature + ' ' + name; 240 } 241 242 246 Field getField() { 247 return field; 248 } 249 250 254 String getSignature() { 255 return signature; 256 } 257 } 258 | Popular Tags |