src/share/classes/com/sun/corba/se/impl/io/ObjectStreamField.java

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
55540e827aef
child 158
91006f157c46
permissions
-rw-r--r--

Initial load

duke@1 1 /*
duke@1 2 * Copyright 1998-2004 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 /*
duke@1 27 * Licensed Materials - Property of IBM
duke@1 28 * RMI-IIOP v1.0
duke@1 29 * Copyright IBM Corp. 1998 1999 All Rights Reserved
duke@1 30 *
duke@1 31 */
duke@1 32
duke@1 33 package com.sun.corba.se.impl.io;
duke@1 34
duke@1 35 import java.lang.reflect.Field;
duke@1 36 import java.lang.Comparable;
duke@1 37 import java.util.Hashtable;
duke@1 38
duke@1 39 import sun.corba.Bridge ;
duke@1 40 import java.security.AccessController ;
duke@1 41 import java.security.PrivilegedAction ;
duke@1 42
duke@1 43 /**
duke@1 44 * A description of a field in a serializable class.
duke@1 45 * A array of these is used to declare the persistent fields of
duke@1 46 * a class.
duke@1 47 *
duke@1 48 */
duke@1 49 public class ObjectStreamField implements Comparable
duke@1 50 {
duke@1 51 private static final Bridge bridge =
duke@1 52 (Bridge)AccessController.doPrivileged(
duke@1 53 new PrivilegedAction() {
duke@1 54 public Object run() {
duke@1 55 return Bridge.get() ;
duke@1 56 }
duke@1 57 }
duke@1 58 ) ;
duke@1 59
duke@1 60 /**
duke@1 61 * Create a named field with the specified type.
duke@1 62 */
duke@1 63 ObjectStreamField(String n, Class clazz) {
duke@1 64 name = n;
duke@1 65 this.clazz = clazz;
duke@1 66
duke@1 67 // Compute the typecode for easy switching
duke@1 68 if (clazz.isPrimitive()) {
duke@1 69 if (clazz == Integer.TYPE) {
duke@1 70 type = 'I';
duke@1 71 } else if (clazz == Byte.TYPE) {
duke@1 72 type = 'B';
duke@1 73 } else if (clazz == Long.TYPE) {
duke@1 74 type = 'J';
duke@1 75 } else if (clazz == Float.TYPE) {
duke@1 76 type = 'F';
duke@1 77 } else if (clazz == Double.TYPE) {
duke@1 78 type = 'D';
duke@1 79 } else if (clazz == Short.TYPE) {
duke@1 80 type = 'S';
duke@1 81 } else if (clazz == Character.TYPE) {
duke@1 82 type = 'C';
duke@1 83 } else if (clazz == Boolean.TYPE) {
duke@1 84 type = 'Z';
duke@1 85 }
duke@1 86 } else if (clazz.isArray()) {
duke@1 87 type = '[';
duke@1 88 typeString = ObjectStreamClass.getSignature(clazz);
duke@1 89 } else {
duke@1 90 type = 'L';
duke@1 91 typeString = ObjectStreamClass.getSignature(clazz);
duke@1 92 }
duke@1 93
duke@1 94 if (typeString != null)
duke@1 95 signature = typeString;
duke@1 96 else
duke@1 97 signature = String.valueOf(type);
duke@1 98
duke@1 99 }
duke@1 100
duke@1 101 ObjectStreamField(Field field) {
duke@1 102 this(field.getName(), field.getType());
duke@1 103 setField( field ) ;
duke@1 104 }
duke@1 105
duke@1 106 /**
duke@1 107 * Create an ObjectStreamField containing a reflected Field.
duke@1 108 */
duke@1 109 ObjectStreamField(String n, char t, Field f, String ts)
duke@1 110 {
duke@1 111 name = n;
duke@1 112 type = t;
duke@1 113 setField( f ) ;
duke@1 114 typeString = ts;
duke@1 115
duke@1 116 if (typeString != null)
duke@1 117 signature = typeString;
duke@1 118 else
duke@1 119 signature = String.valueOf(type);
duke@1 120
duke@1 121 }
duke@1 122
duke@1 123 /**
duke@1 124 * Get the name of this field.
duke@1 125 */
duke@1 126 public String getName() {
duke@1 127 return name;
duke@1 128 }
duke@1 129
duke@1 130 /**
duke@1 131 * Get the type of the field.
duke@1 132 */
duke@1 133 public Class getType() {
duke@1 134 if (clazz != null)
duke@1 135 return clazz;
duke@1 136 switch (type) {
duke@1 137 case 'B': clazz = Byte.TYPE;
duke@1 138 break;
duke@1 139 case 'C': clazz = Character.TYPE;
duke@1 140 break;
duke@1 141 case 'S': clazz = Short.TYPE;
duke@1 142 break;
duke@1 143 case 'I': clazz = Integer.TYPE;
duke@1 144 break;
duke@1 145 case 'J': clazz = Long.TYPE;
duke@1 146 break;
duke@1 147 case 'F': clazz = Float.TYPE;
duke@1 148 break;
duke@1 149 case 'D': clazz = Double.TYPE;
duke@1 150 break;
duke@1 151 case 'Z': clazz = Boolean.TYPE;
duke@1 152 break;
duke@1 153 case '[':
duke@1 154 case 'L':
duke@1 155 clazz = Object.class;
duke@1 156 break;
duke@1 157 }
duke@1 158
duke@1 159 return clazz;
duke@1 160 }
duke@1 161
duke@1 162 public char getTypeCode() {
duke@1 163 return type;
duke@1 164 }
duke@1 165
duke@1 166 public String getTypeString() {
duke@1 167 return typeString;
duke@1 168 }
duke@1 169
duke@1 170 Field getField() {
duke@1 171 return field;
duke@1 172 }
duke@1 173
duke@1 174 void setField(Field field) {
duke@1 175 this.field = field;
duke@1 176 this.fieldID = bridge.objectFieldOffset( field ) ;
duke@1 177 }
duke@1 178
duke@1 179 /*
duke@1 180 * Default constructor creates an empty field.
duke@1 181 * Usually used just to get to the sort functions.
duke@1 182 */
duke@1 183 ObjectStreamField() {
duke@1 184 }
duke@1 185
duke@1 186 /**
duke@1 187 * test if this field is a primitive or not.
duke@1 188 */
duke@1 189 public boolean isPrimitive() {
duke@1 190 return (type != '[' && type != 'L');
duke@1 191 }
duke@1 192
duke@1 193 /**
duke@1 194 * Compare this with another ObjectStreamField.
duke@1 195 * return -1 if this is smaller, 0 if equal, 1 if greater
duke@1 196 * types that are primitives are "smaller" than objects.
duke@1 197 * if equal, the names are compared.
duke@1 198 */
duke@1 199 public int compareTo(Object o) {
duke@1 200 ObjectStreamField f2 = (ObjectStreamField)o;
duke@1 201 boolean thisprim = (this.typeString == null);
duke@1 202 boolean otherprim = (f2.typeString == null);
duke@1 203
duke@1 204 if (thisprim != otherprim) {
duke@1 205 return (thisprim ? -1 : 1);
duke@1 206 }
duke@1 207 return this.name.compareTo(f2.name);
duke@1 208 }
duke@1 209
duke@1 210 /**
duke@1 211 * Compare the types of two class descriptors.
duke@1 212 * The match if they have the same primitive types.
duke@1 213 * or if they are both objects and the object types match.
duke@1 214 */
duke@1 215 public boolean typeEquals(ObjectStreamField other) {
duke@1 216 if (other == null || type != other.type)
duke@1 217 return false;
duke@1 218
duke@1 219 /* Return true if the primitive types matched */
duke@1 220 if (typeString == null && other.typeString == null)
duke@1 221 return true;
duke@1 222
duke@1 223 return ObjectStreamClass.compareClassNames(typeString,
duke@1 224 other.typeString,
duke@1 225 '/');
duke@1 226 }
duke@1 227
duke@1 228 /* Returns the signature of the Field.
duke@1 229 *
duke@1 230 */
duke@1 231 public String getSignature() {
duke@1 232
duke@1 233 return signature;
duke@1 234
duke@1 235 }
duke@1 236
duke@1 237 /**
duke@1 238 * Return a string describing this field.
duke@1 239 */
duke@1 240 public String toString() {
duke@1 241 if (typeString != null)
duke@1 242 return typeString + " " + name;
duke@1 243 else
duke@1 244 return type + " " + name;
duke@1 245 }
duke@1 246
duke@1 247 public Class getClazz() {
duke@1 248 return clazz;
duke@1 249 }
duke@1 250
duke@1 251 /* Returns the Field ID
duke@1 252 *
duke@1 253 */
duke@1 254 public long getFieldID() {
duke@1 255 return fieldID ;
duke@1 256 }
duke@1 257
duke@1 258 private String name; // the name of the field
duke@1 259 private char type; // type first byte of the type signature
duke@1 260 private Field field; // Reflected field
duke@1 261 private String typeString; // iff object, typename
duke@1 262 private Class clazz; // the type of this field, if has been resolved
duke@1 263
duke@1 264 // the next 2 things are RMI-IIOP specific, it can be easily
duke@1 265 // removed, if we can figure out all place where there are dependencies
duke@1 266 // to this. Signature is esentially equal to typestring. Then
duke@1 267 // essentially we can use the java.io.ObjectStreamField as such.
duke@1 268
duke@1 269 private String signature; // the signature of the field
duke@1 270 private long fieldID = Bridge.INVALID_FIELD_OFFSET ;
duke@1 271 }

mercurial