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