KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ObjectStreamField.java 1.6 03/12/19
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.orbutil;
18
19 import java.lang.reflect.Field JavaDoc;
20 import java.lang.Comparable JavaDoc;
21 import java.util.Hashtable JavaDoc;
22
23 /**
24  * This is duplicated here somewhat in haste since we can't
25  * expose this class outside of the com.sun.corba.se.impl.io
26  * package for security reasons.
27  */

28 /**
29  * A description of a field in a serializable class.
30  * A array of these is used to declare the persistent fields of
31  * a class.
32  *
33  */

34 class ObjectStreamField implements Comparable JavaDoc {
35     /**
36      * Create a named field with the specified type.
37      */

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

84     ObjectStreamField(String JavaDoc n, char t, Field JavaDoc f, String JavaDoc ts)
85     {
86     name = n;
87     type = t;
88     field = f;
89     typeString = ts;
90
91     if (typeString != null)
92         signature = typeString;
93     else
94         signature = String.valueOf(type);
95     
96     }
97
98     /**
99      * Get the name of this field.
100      */

101     public String JavaDoc getName() {
102         return name;
103     }
104
105     /**
106      * Get the type of the field.
107      */

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

158     ObjectStreamField() {
159     }
160
161     /**
162      * test if this field is a primitive or not.
163      */

164     public boolean isPrimitive() {
165     return (type != '[' && type != 'L');
166     }
167
168     /**
169      * Compare this with another ObjectStreamField.
170      * return -1 if this is smaller, 0 if equal, 1 if greater
171      * types that are primitives are "smaller" than objects.
172      * if equal, the names are compared.
173      */

174     public int compareTo(Object JavaDoc o) {
175     ObjectStreamField f2 = (ObjectStreamField)o;
176     boolean thisprim = (this.typeString == null);
177     boolean otherprim = (f2.typeString == null);
178
179     if (thisprim != otherprim) {
180         return (thisprim ? -1 : 1);
181     }
182     return this.name.compareTo(f2.name);
183     }
184
185     /**
186      * Compare the types of two class descriptors.
187      * The match if they have the same primitive types.
188      * or if they are both objects and the object types match.
189      */

190     public boolean typeEquals(ObjectStreamField other) {
191     if (other == null || type != other.type)
192         return false;
193
194     /* Return true if the primitive types matched */
195     if (typeString == null && other.typeString == null)
196         return true;
197
198     return ObjectStreamClass_1_3_1.compareClassNames(typeString,
199                                                          other.typeString,
200                                                          '/');
201     }
202
203     /* Returns the signature of the Field.
204      *
205      */

206     public String JavaDoc getSignature() {
207
208     return signature;
209
210     }
211
212     /**
213      * Return a string describing this field.
214      */

215     public String JavaDoc toString() {
216     if (typeString != null)
217         return typeString + " " + name;
218     else
219         return type + " " + name;
220     }
221
222     public Class JavaDoc getClazz() {
223         return clazz;
224     }
225
226     /* Returns the Field ID
227      * NOT USED, since this class is used only in ObjectStreamClass_1_3_1,
228      * which is used only in RepositoryId_1_3_1.
229     public long getFieldID( Class cl ) {
230     if (fieldID == -1) {
231         if (typeString != null)
232         fieldID = getFieldIDNative( cl, getName(), typeString );
233         else
234         fieldID = getFieldIDNative( cl, getName(), getSignature() );
235     }
236     return fieldID;
237     }
238      */

239
240     private String JavaDoc name; // the name of the field
241
private char type; // type first byte of the type signature
242
private Field JavaDoc field; // Reflected field
243
private String JavaDoc typeString; // iff object, typename
244
private Class JavaDoc clazz; // the type of this field, if has been resolved
245

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

251     private String JavaDoc signature; // the signature of the field
252
private long fieldID = -1;
253     // private static native long getFieldIDNative(Class c, String fieldName, String fieldSig);
254
}
255
Popular Tags