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