001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.util.io;
017:
018: import java.io.IOException;
019: import java.io.InvalidClassException;
020: import java.lang.reflect.Field;
021:
022: public class FieldDescriptor implements java.io.Serializable,
023: Comparable {
024:
025: public FieldDescriptor(Field field) {
026: this .field = field;
027: this .name = field.getName();
028: this .type = field.getType();
029:
030: if (type.isPrimitive()) {
031: if (type == Integer.TYPE)
032: typeCode = 'I';
033: else if (type == Byte.TYPE)
034: typeCode = 'B';
035: else if (type == Long.TYPE)
036: typeCode = 'J';
037: else if (type == Float.TYPE)
038: typeCode = 'F';
039: else if (type == Double.TYPE)
040: typeCode = 'D';
041: else if (type == Short.TYPE)
042: typeCode = 'S';
043: else if (type == Character.TYPE)
044: typeCode = 'C';
045: else if (type == Boolean.TYPE)
046: typeCode = 'Z';
047: else if (type == Void.TYPE)
048: typeCode = 'V';
049: } else if (type.isArray()) {
050: typeCode = '[';
051: typeString = ClassDescriptor.getSignature(type).toString()
052: .intern();
053: } else {
054: typeCode = 'L';
055: StringBuffer buf = new StringBuffer();
056: buf.append(typeCode);
057: buf.append(type.getName().replace('.', '/'));
058: buf.append(';');
059: typeString = buf.toString().intern();
060: }
061:
062: }
063:
064: public FieldDescriptor(String name, Class type) {
065:
066: }
067:
068: protected String typeString;
069:
070: public String getTypeString() {
071: return typeString;
072: }
073:
074: /*
075: * The name of this field
076: */
077: protected String name;
078:
079: public String getName() {
080: return name;
081: }
082:
083: public void setName(String name) {
084: this .name = name;
085: }
086:
087: protected Field field;
088:
089: public Field getField() {
090: return field;
091: }
092:
093: public void setField(Field field) {
094: this .field = field;
095: }
096:
097: protected char typeCode;
098:
099: public char getTypeCode() {
100: return typeCode;
101: }
102:
103: public void setTypeCode(char typeCode) {
104: this .typeCode = typeCode;
105: }
106:
107: protected Class type;
108:
109: public int compareTo(Object o) {
110: FieldDescriptor f2 = (FieldDescriptor) o;
111: boolean this prim = (this .typeString == null);
112: boolean otherprim = (f2.typeString == null);
113:
114: if (this prim != otherprim) {
115: return (this prim ? -1 : 1);
116: }
117: return this .name.compareTo(f2.name);
118: }
119:
120: protected ClassDescriptor classDesc;
121:
122: public ClassDescriptor getClassDescriptor() {
123: return classDesc;
124: }
125:
126: public void setClassDescriptor(ClassDescriptor classDesc) {
127: this .classDesc = classDesc;
128: }
129:
130: public void writeDesc(ObjectOutputStream out) throws IOException {
131: out.writeByte((int) typeCode);
132: out.writeUTF(name);
133: if (!type.isPrimitive())
134: out.writeString(typeString);
135: }
136:
137: public void write(Object o, ObjectOutputStream out)
138: throws IOException, InvalidClassException {
139:
140: if (field == null)
141: throw new InvalidClassException(classDesc.forClass()
142: .getName(), "Nonexistent field " + name);
143: try {
144: switch (typeCode) {
145: case 'B':
146: out.writeByte(field.getByte(o));
147: break;
148: case 'C':
149: out.writeChar(field.getChar(o));
150: break;
151: case 'I':
152: out.writeInt(field.getInt(o));
153: break;
154: case 'Z':
155: out.writeBoolean(field.getBoolean(o));
156: break;
157: case 'J':
158: out.writeLong(field.getLong(o));
159: break;
160: case 'F':
161: out.writeFloat(field.getFloat(o));
162: break;
163: case 'D':
164: out.writeDouble(field.getDouble(o));
165: break;
166: case 'S':
167: out.writeShort(field.getShort(o));
168: break;
169: case '[':
170: case 'L':
171: out.writeObject(field.get(o));
172: break;
173: default:
174: throw new InvalidClassException(classDesc.forClass()
175: .getName());
176: }
177: } catch (IllegalAccessException e) {
178: throw (InvalidClassException) new InvalidClassException(
179: classDesc.forClass().getName(), e.getMessage())
180: .initCause(e);
181: }
182: }
183: }
|