1. /*
  2. * @(#)ObjectStreamField.java 1.19 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * Licensed Materials - Property of IBM
  9. * RMI-IIOP v1.0
  10. * Copyright IBM Corp. 1998 1999 All Rights Reserved
  11. *
  12. * US Government Users Restricted Rights - Use, duplication or
  13. * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  14. */
  15. package com.sun.corba.se.internal.io;
  16. import java.lang.reflect.Field;
  17. import java.lang.Comparable;
  18. import java.util.Hashtable;
  19. /**
  20. * A description of a field in a serializable class.
  21. * A array of these is used to declare the persistent fields of
  22. * a class.
  23. *
  24. */
  25. public class ObjectStreamField implements Comparable {
  26. /**
  27. * Create a named field with the specified type.
  28. */
  29. ObjectStreamField(String n, Class clazz) {
  30. name = n;
  31. this.clazz = clazz;
  32. // Compute the typecode for easy switching
  33. if (clazz.isPrimitive()) {
  34. if (clazz == Integer.TYPE) {
  35. type = 'I';
  36. } else if (clazz == Byte.TYPE) {
  37. type = 'B';
  38. } else if (clazz == Long.TYPE) {
  39. type = 'J';
  40. } else if (clazz == Float.TYPE) {
  41. type = 'F';
  42. } else if (clazz == Double.TYPE) {
  43. type = 'D';
  44. } else if (clazz == Short.TYPE) {
  45. type = 'S';
  46. } else if (clazz == Character.TYPE) {
  47. type = 'C';
  48. } else if (clazz == Boolean.TYPE) {
  49. type = 'Z';
  50. }
  51. } else if (clazz.isArray()) {
  52. type = '[';
  53. typeString = ObjectStreamClass.getSignature(clazz);
  54. } else {
  55. type = 'L';
  56. typeString = ObjectStreamClass.getSignature(clazz);
  57. }
  58. if (typeString != null)
  59. signature = typeString;
  60. else
  61. signature = String.valueOf(type);
  62. }
  63. ObjectStreamField(Field field) {
  64. this(field.getName(), field.getType());
  65. this.field = field;
  66. }
  67. /**
  68. * Create an ObjectStreamField containing a reflected Field.
  69. */
  70. ObjectStreamField(String n, char t, Field f, String ts)
  71. {
  72. name = n;
  73. type = t;
  74. field = f;
  75. typeString = ts;
  76. if (typeString != null)
  77. signature = typeString;
  78. else
  79. signature = String.valueOf(type);
  80. }
  81. /**
  82. * Get the name of this field.
  83. */
  84. public String getName() {
  85. return name;
  86. }
  87. /**
  88. * Get the type of the field.
  89. */
  90. public Class getType() {
  91. if (clazz != null)
  92. return clazz;
  93. switch (type) {
  94. case 'B': clazz = Byte.TYPE;
  95. break;
  96. case 'C': clazz = Character.TYPE;
  97. break;
  98. case 'S': clazz = Short.TYPE;
  99. break;
  100. case 'I': clazz = Integer.TYPE;
  101. break;
  102. case 'J': clazz = Long.TYPE;
  103. break;
  104. case 'F': clazz = Float.TYPE;
  105. break;
  106. case 'D': clazz = Double.TYPE;
  107. break;
  108. case 'Z': clazz = Boolean.TYPE;
  109. break;
  110. case '[':
  111. case 'L':
  112. clazz = Object.class;
  113. break;
  114. }
  115. return clazz;
  116. }
  117. public char getTypeCode() {
  118. return type;
  119. }
  120. public String getTypeString() {
  121. return typeString;
  122. }
  123. Field getField() {
  124. return field;
  125. }
  126. void setField(Field field) {
  127. this.field = field;
  128. this.fieldID = -1;
  129. }
  130. /*
  131. * Default constructor creates an empty field.
  132. * Usually used just to get to the sort functions.
  133. */
  134. ObjectStreamField() {
  135. }
  136. /**
  137. * test if this field is a primitive or not.
  138. */
  139. public boolean isPrimitive() {
  140. return (type != '[' && type != 'L');
  141. }
  142. /**
  143. * Compare this with another ObjectStreamField.
  144. * return -1 if this is smaller, 0 if equal, 1 if greater
  145. * types that are primitives are "smaller" than objects.
  146. * if equal, the names are compared.
  147. */
  148. public int compareTo(Object o) {
  149. ObjectStreamField f2 = (ObjectStreamField)o;
  150. boolean thisprim = (this.typeString == null);
  151. boolean otherprim = (f2.typeString == null);
  152. if (thisprim != otherprim) {
  153. return (thisprim ? -1 : 1);
  154. }
  155. return this.name.compareTo(f2.name);
  156. }
  157. /**
  158. * Compare the types of two class descriptors.
  159. * The match if they have the same primitive types.
  160. * or if they are both objects and the object types match.
  161. */
  162. public boolean typeEquals(ObjectStreamField other) {
  163. if (other == null || type != other.type)
  164. return false;
  165. /* Return true if the primitive types matched */
  166. if (typeString == null && other.typeString == null)
  167. return true;
  168. return ObjectStreamClass.compareClassNames(typeString,
  169. other.typeString,
  170. '/');
  171. }
  172. /* Returns the signature of the Field.
  173. *
  174. */
  175. public String getSignature() {
  176. return signature;
  177. }
  178. /**
  179. * Return a string describing this field.
  180. */
  181. public String toString() {
  182. if (typeString != null)
  183. return typeString + " " + name;
  184. else
  185. return type + " " + name;
  186. }
  187. public Class getClazz() {
  188. return clazz;
  189. }
  190. /* Returns the Field ID
  191. *
  192. */
  193. public long getFieldID( Class cl ) {
  194. if (fieldID == -1) {
  195. if (typeString != null)
  196. fieldID = getFieldIDNative( cl, getName(), typeString );
  197. else
  198. fieldID = getFieldIDNative( cl, getName(), getSignature() );
  199. }
  200. return fieldID;
  201. }
  202. private String name; // the name of the field
  203. private char type; // type first byte of the type signature
  204. private Field field; // Reflected field
  205. private String typeString; // iff object, typename
  206. private Class clazz; // the type of this field, if has been resolved
  207. // the next 3 things are RMI-IIOP specific, it can be easily
  208. // removed, if we can figure out all place where there are dependencies
  209. // to this. Signature is esentially equal to typestring. Then
  210. // essentially we can use the java.io.ObjectStreamField as such.
  211. private String signature; // the signature of the field
  212. private long fieldID = -1;
  213. private static native long getFieldIDNative(Class c, String fieldName, String fieldSig);
  214. }