KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > io > FieldDescriptor


1 package org.openejb.util.io;
2
3
4 import java.io.IOException JavaDoc;
5 import java.io.InvalidClassException JavaDoc;
6 import java.lang.reflect.Field JavaDoc;
7
8 public class FieldDescriptor implements java.io.Serializable JavaDoc, Comparable JavaDoc {
9
10
11     //================================================================
12
// Methods and fields to suppport the basic properties that will
13
// be written to or dread from the stream.
14
// this FieldDescriptor belongs to.
15
//
16
//====
17

18     public FieldDescriptor(Field JavaDoc field){
19         this.field = field;
20         this.name = field.getName();
21         this.type = field.getType();
22
23         if (type.isPrimitive()) {
24             if (type == Integer.TYPE) typeCode = 'I';
25         else if (type == Byte.TYPE) typeCode = 'B';
26             else if (type == Long.TYPE) typeCode = 'J';
27             else if (type == Float.TYPE) typeCode = 'F';
28             else if (type == Double.TYPE) typeCode = 'D';
29             else if (type == Short.TYPE) typeCode = 'S';
30             else if (type == Character.TYPE) typeCode = 'C';
31             else if (type == Boolean.TYPE) typeCode = 'Z';
32             else if (type == Void.TYPE) typeCode = 'V';
33         }
34         else if (type.isArray()) {
35             typeCode = '[';
36             typeString = ClassDescriptor.getSignature(type).toString().intern();
37         }
38         else {
39             typeCode = 'L';
40             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
41             buf.append(typeCode);
42             buf.append(type.getName().replace('.', '/'));
43             buf.append(';');
44             typeString = buf.toString().intern();
45         }
46
47     }
48
49     public FieldDescriptor(String JavaDoc name, Class JavaDoc type){
50
51     }
52
53     protected String JavaDoc typeString;
54
55     public String JavaDoc getTypeString(){
56         return typeString;
57     }
58
59     /*
60      * The name of this field
61      */

62     protected String JavaDoc name;
63
64     public String JavaDoc getName(){
65         return name;
66     }
67
68     public void setName(String JavaDoc name){
69         this.name = name;
70     }
71
72     protected Field JavaDoc field;
73
74     public Field JavaDoc getField(){
75         return field;
76     }
77
78     public void setField(Field JavaDoc field){
79         this.field = field;
80     }
81
82     protected char typeCode;
83
84     public char getTypeCode(){
85         return typeCode;
86     }
87
88     public void setTypeCode(char typeCode){
89         this.typeCode = typeCode;
90     }
91
92     protected Class JavaDoc type;
93
94     /**
95      * Compare this field with another <code>FieldDescriptor</code>.
96      * Return -1 if this is smaller, 0 if equal, 1 if greater.
97      * Types that are primitives are "smaller" than object types.
98      * If equal, the field names are compared.
99      */

100     // This method is called quite often. Declaring
101
// field2 outside of the method prevents us from
102
// burning trough these objects for every comparison
103
// necessary to sort a list of these fields.
104
public int compareTo(Object JavaDoc o) {
105         FieldDescriptor f2 = (FieldDescriptor)o;
106         boolean thisprim = (this.typeString == null);
107         boolean otherprim = (f2.typeString == null);
108
109         if (thisprim != otherprim) {
110             return (thisprim ? -1 : 1);
111         }
112         return this.name.compareTo(f2.name);
113     }
114
115     //================================================================
116
// Methods and fields to suppport tracking of the ClassDescriptor
117
// this FieldDescriptor belongs to.
118
//
119
//====
120

121     protected ClassDescriptor classDesc;
122
123     public ClassDescriptor getClassDescriptor(){
124         return classDesc;
125     }
126
127     public void setClassDescriptor(ClassDescriptor classDesc){
128         this.classDesc = classDesc;
129     }
130
131     public void writeDesc(ObjectOutputStream out) throws IOException JavaDoc {
132             out.writeByte((int)typeCode);
133             out.writeUTF(name);
134             if (!type.isPrimitive()) out.writeString(typeString);
135     }
136
137     public void write(Object JavaDoc o, ObjectOutputStream out) throws IOException JavaDoc, InvalidClassException JavaDoc {
138
139         if (field == null) throw new InvalidClassException JavaDoc(classDesc.forClass().getName(), "Nonexistent field " + name);
140         try {
141         switch (typeCode) {
142             case 'B': out.writeByte(field.getByte(o)); break;
143             case 'C': out.writeChar(field.getChar(o)); break;
144             case 'I': out.writeInt(field.getInt(o)); break;
145             case 'Z': out.writeBoolean(field.getBoolean(o)); break;
146             case 'J': out.writeLong(field.getLong(o)); break;
147             case 'F': out.writeFloat(field.getFloat(o)); break;
148             case 'D': out.writeDouble(field.getDouble(o)); break;
149             case 'S': out.writeShort(field.getShort(o)); break;
150             case '[':
151             case 'L': out.writeObject(field.get(o)); break;
152             default: throw new InvalidClassException JavaDoc(classDesc.forClass().getName());
153         }
154         }
155         catch (IllegalAccessException JavaDoc e) {
156         throw new InvalidClassException JavaDoc(classDesc.forClass().getName(), e.getMessage());
157         }
158     }
159 }
Popular Tags