KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > io > ObjectStreamField


1 /*
2  * @(#)ObjectStreamField.java 1.24 04/04/07
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * Licensed Materials - Property of IBM
10  * RMI-IIOP v1.0
11  * Copyright IBM Corp. 1998 1999 All Rights Reserved
12  *
13  * US Government Users Restricted Rights - Use, duplication or
14  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
15  */

16
17 package com.sun.corba.se.impl.io;
18
19 import java.lang.reflect.Field JavaDoc;
20 import java.lang.Comparable JavaDoc;
21 import java.util.Hashtable JavaDoc;
22
23 import sun.corba.Bridge ;
24 import java.security.AccessController JavaDoc ;
25 import java.security.PrivilegedAction JavaDoc ;
26
27 /**
28  * A description of a field in a serializable class.
29  * A array of these is used to declare the persistent fields of
30  * a class.
31  *
32  */

33 public class ObjectStreamField implements Comparable JavaDoc
34 {
35     private static final Bridge bridge =
36     (Bridge)AccessController.doPrivileged(
37         new PrivilegedAction JavaDoc() {
38         public Object JavaDoc run() {
39             return Bridge.get() ;
40         }
41         }
42     ) ;
43
44     /**
45      * Create a named field with the specified type.
46      */

47     ObjectStreamField(String JavaDoc n, Class JavaDoc clazz) {
48         name = n;
49         this.clazz = clazz;
50
51     // Compute the typecode for easy switching
52
if (clazz.isPrimitive()) {
53         if (clazz == Integer.TYPE) {
54         type = 'I';
55         } else if (clazz == Byte.TYPE) {
56         type = 'B';
57         } else if (clazz == Long.TYPE) {
58         type = 'J';
59         } else if (clazz == Float.TYPE) {
60         type = 'F';
61         } else if (clazz == Double.TYPE) {
62         type = 'D';
63         } else if (clazz == Short.TYPE) {
64         type = 'S';
65         } else if (clazz == Character.TYPE) {
66         type = 'C';
67         } else if (clazz == Boolean.TYPE) {
68         type = 'Z';
69         }
70     } else if (clazz.isArray()) {
71         type = '[';
72         typeString = ObjectStreamClass.getSignature(clazz);
73     } else {
74         type = 'L';
75         typeString = ObjectStreamClass.getSignature(clazz);
76     }
77
78     if (typeString != null)
79         signature = typeString;
80     else
81         signature = String.valueOf(type);
82
83     }
84
85     ObjectStreamField(Field JavaDoc field) {
86     this(field.getName(), field.getType());
87     setField( field ) ;
88     }
89
90     /**
91      * Create an ObjectStreamField containing a reflected Field.
92      */

93     ObjectStreamField(String JavaDoc n, char t, Field JavaDoc f, String JavaDoc ts)
94     {
95     name = n;
96     type = t;
97     setField( f ) ;
98     typeString = ts;
99
100     if (typeString != null)
101         signature = typeString;
102     else
103         signature = String.valueOf(type);
104     
105     }
106
107     /**
108      * Get the name of this field.
109      */

110     public String JavaDoc getName() {
111         return name;
112     }
113
114     /**
115      * Get the type of the field.
116      */

117     public Class JavaDoc getType() {
118         if (clazz != null)
119             return clazz;
120     switch (type) {
121     case 'B': clazz = Byte.TYPE;
122         break;
123     case 'C': clazz = Character.TYPE;
124         break;
125     case 'S': clazz = Short.TYPE;
126         break;
127     case 'I': clazz = Integer.TYPE;
128         break;
129     case 'J': clazz = Long.TYPE;
130         break;
131     case 'F': clazz = Float.TYPE;
132         break;
133     case 'D': clazz = Double.TYPE;
134         break;
135     case 'Z': clazz = Boolean.TYPE;
136         break;
137     case '[':
138     case 'L':
139         clazz = Object JavaDoc.class;
140         break;
141     }
142
143         return clazz;
144     }
145
146     public char getTypeCode() {
147     return type;
148     }
149
150     public String JavaDoc getTypeString() {
151     return typeString;
152     }
153
154     Field JavaDoc getField() {
155     return field;
156     }
157
158     void setField(Field JavaDoc field) {
159     this.field = field;
160     this.fieldID = bridge.objectFieldOffset( field ) ;
161     }
162
163     /*
164      * Default constructor creates an empty field.
165      * Usually used just to get to the sort functions.
166      */

167     ObjectStreamField() {
168     }
169
170     /**
171      * test if this field is a primitive or not.
172      */

173     public boolean isPrimitive() {
174     return (type != '[' && type != 'L');
175     }
176
177     /**
178      * Compare this with another ObjectStreamField.
179      * return -1 if this is smaller, 0 if equal, 1 if greater
180      * types that are primitives are "smaller" than objects.
181      * if equal, the names are compared.
182      */

183     public int compareTo(Object JavaDoc o) {
184     ObjectStreamField f2 = (ObjectStreamField)o;
185     boolean thisprim = (this.typeString == null);
186     boolean otherprim = (f2.typeString == null);
187
188     if (thisprim != otherprim) {
189         return (thisprim ? -1 : 1);
190     }
191     return this.name.compareTo(f2.name);
192     }
193
194     /**
195      * Compare the types of two class descriptors.
196      * The match if they have the same primitive types.
197      * or if they are both objects and the object types match.
198      */

199     public boolean typeEquals(ObjectStreamField other) {
200     if (other == null || type != other.type)
201         return false;
202
203     /* Return true if the primitive types matched */
204     if (typeString == null && other.typeString == null)
205         return true;
206
207     return ObjectStreamClass.compareClassNames(typeString,
208                            other.typeString,
209                            '/');
210     }
211
212     /* Returns the signature of the Field.
213      *
214      */

215     public String JavaDoc getSignature() {
216
217     return signature;
218
219     }
220
221     /**
222      * Return a string describing this field.
223      */

224     public String JavaDoc toString() {
225     if (typeString != null)
226         return typeString + " " + name;
227     else
228         return type + " " + name;
229     }
230
231     public Class JavaDoc getClazz() {
232         return clazz;
233     }
234
235     /* Returns the Field ID
236      *
237      */

238     public long getFieldID() {
239     return fieldID ;
240     }
241
242     private String JavaDoc name; // the name of the field
243
private char type; // type first byte of the type signature
244
private Field JavaDoc field; // Reflected field
245
private String JavaDoc typeString; // iff object, typename
246
private Class JavaDoc clazz; // the type of this field, if has been resolved
247

248     // the next 2 things are RMI-IIOP specific, it can be easily
249
// removed, if we can figure out all place where there are dependencies
250
// to this. Signature is esentially equal to typestring. Then
251
// essentially we can use the java.io.ObjectStreamField as such.
252

253     private String JavaDoc signature; // the signature of the field
254
private long fieldID = Bridge.INVALID_FIELD_OFFSET ;
255 }
256
Popular Tags