📄 objectstreamclass_1_3_1.java
字号:
if (fields[i].getField() != null) { field[fieldNum++] = fields[i].getField(); } } if (field.length > 1) Arrays.sort(field, compareMemberByName); for (int i = 0; i < field.length; i++) { Field f = field[i]; /* Include in the hash all fields except those that are * transient */ int m = f.getModifiers(); //Serial 6 //if (Modifier.isTransient(m) || Modifier.isStatic(m)) // spec reference 00-01-06.pdf, 1.3.5.6, states non-static // non-transient, public fields are mapped to Java IDL. // // Here's the quote from the first paragraph: // Java non-static non-transient public fields are mapped to // OMG IDL public data members, and other Java fields are // not mapped. // if (Modifier.isTransient(m) || Modifier.isStatic(m)) // continue; data.writeUTF(f.getName()); data.writeUTF(getSignature(f.getType())); } /* Compute the hash value for this class. * Use only the first 64 bits of the hash. */ data.flush(); byte hasharray[] = md.digest(); // int minimum = Math.min(8, hasharray.length); // SerialBug 3: SHA computation is wrong; for loop reversed //for (int i = minimum; i > 0; i--) for (int i = 0; i < Math.min(8, hasharray.length); i++) { h += (long)(hasharray[i] & 255) << (i * 8); } } catch (IOException ignore) { /* can't happen, but be deterministic anyway. */ h = -1; } catch (NoSuchAlgorithmException complain) { throw new SecurityException(complain.getMessage()); } return h; } /** * Compute the JVM signature for the class. */ static String getSignature(Class clazz) { String type = null; if (clazz.isArray()) { Class cl = clazz; int dimensions = 0; while (cl.isArray()) { dimensions++; cl = cl.getComponentType(); } StringBuffer sb = new StringBuffer(); for (int i = 0; i < dimensions; i++) { sb.append("["); } sb.append(getSignature(cl)); type = sb.toString(); } else if (clazz.isPrimitive()) { if (clazz == Integer.TYPE) { type = "I"; } else if (clazz == Byte.TYPE) { type = "B"; } else if (clazz == Long.TYPE) { type = "J"; } else if (clazz == Float.TYPE) { type = "F"; } else if (clazz == Double.TYPE) { type = "D"; } else if (clazz == Short.TYPE) { type = "S"; } else if (clazz == Character.TYPE) { type = "C"; } else if (clazz == Boolean.TYPE) { type = "Z"; } else if (clazz == Void.TYPE) { type = "V"; } } else { type = "L" + clazz.getName().replace('.', '/') + ";"; } return type; } /* * Compute the JVM method descriptor for the method. */ static String getSignature(Method meth) { StringBuffer sb = new StringBuffer(); sb.append("("); Class[] params = meth.getParameterTypes(); // avoid clone for (int j = 0; j < params.length; j++) { sb.append(getSignature(params[j])); } sb.append(")"); sb.append(getSignature(meth.getReturnType())); return sb.toString(); } /* * Compute the JVM constructor descriptor for the constructor. */ static String getSignature(Constructor cons) { StringBuffer sb = new StringBuffer(); sb.append("("); Class[] params = cons.getParameterTypes(); // avoid clone for (int j = 0; j < params.length; j++) { sb.append(getSignature(params[j])); } sb.append(")V"); return sb.toString(); } /* * Cache of Class -> ClassDescriptor Mappings. */ static private ObjectStreamClassEntry[] descriptorFor = new ObjectStreamClassEntry[61]; /* * findDescriptorFor a Class. This looks in the cache for a * mapping from Class -> ObjectStreamClass mappings. The hashCode * of the Class is used for the lookup since the Class is the key. * The entries are extended from java.lang.ref.SoftReference so the * gc will be able to free them if needed. */ private static ObjectStreamClass_1_3_1 findDescriptorFor(Class cl) { int hash = cl.hashCode(); int index = (hash & 0x7FFFFFFF) % descriptorFor.length; ObjectStreamClassEntry e; ObjectStreamClassEntry prev; /* Free any initial entries whose refs have been cleared */ while ((e = descriptorFor[index]) != null && e.get() == null) { descriptorFor[index] = e.next; } /* Traverse the chain looking for a descriptor with ofClass == cl. * unlink entries that are unresolved. */ prev = e; while (e != null ) { ObjectStreamClass_1_3_1 desc = (ObjectStreamClass_1_3_1)(e.get()); if (desc == null) { // This entry has been cleared, unlink it prev.next = e.next; } else { if (desc.ofClass == cl) return desc; prev = e; } e = e.next; } return null; } /* * insertDescriptorFor a Class -> ObjectStreamClass_1_3_1 mapping. */ private static void insertDescriptorFor(ObjectStreamClass_1_3_1 desc) { // Make sure not already present if (findDescriptorFor(desc.ofClass) != null) { return; } int hash = desc.ofClass.hashCode(); int index = (hash & 0x7FFFFFFF) % descriptorFor.length; ObjectStreamClassEntry e = new ObjectStreamClassEntry(desc); e.next = descriptorFor[index]; descriptorFor[index] = e; } private static Field[] getDeclaredFields(final Class clz) { return (Field[]) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { return clz.getDeclaredFields(); } }); } /* * The name of this descriptor */ private String name; /* * The descriptor of the supertype. */ private ObjectStreamClass_1_3_1 superclass; /* * Flags for Serializable and Externalizable. */ private boolean serializable; private boolean externalizable; /* * Array of persistent fields of this class, sorted by * type and name. */ private ObjectStreamField[] fields; /* * Class that is a descriptor for in this virtual machine. */ private Class ofClass; /* * True if descriptor for a proxy class. */ boolean forProxyClass; /* * SerialVersionUID for this class. */ private long suid = kDefaultUID; private String suidStr = null; /* * Actual (computed) SerialVersionUID for this class. */ private long actualSuid = kDefaultUID; private String actualSuidStr = null; /* * The total number of bytes of primitive fields. * The total number of object fields. */ int primBytes; int objFields; /* Internal lock object. */ private Object lock = new Object(); /* True if this class has/had a writeObject method */ private boolean hasWriteObjectMethod; /* In JDK 1.1, external data was not written in block mode. * As of JDK 1.2, external data is written in block data mode. This * flag enables JDK 1.2 to be able to read JDK 1.1 written external data. * * @since JDK 1.2 */ private boolean hasExternalizableBlockData; Method writeObjectMethod; Method readObjectMethod; private Method writeReplaceObjectMethod; private Method readResolveObjectMethod; /* * ObjectStreamClass_1_3_1 that this one was built from. */ private ObjectStreamClass_1_3_1 localClassDesc; /* Get the private static final field for serial version UID */ // private static native long getSerialVersionUIDField(Class cl); /* The Class Object for java.io.Serializable */ private static Class classSerializable = null; private static Class classExternalizable = null; /* * Resolve java.io.Serializable at load time. */ static { try { classSerializable = Class.forName("java.io.Serializable"); classExternalizable = Class.forName("java.io.Externalizable"); } catch (Throwable e) { System.err.println("Could not load java.io.Serializable or java.io.Externalizable."); } } /** use serialVersionUID from JDK 1.1. for interoperability */ private static final long serialVersionUID = -6120832682080437368L; /** * Set serialPersistentFields of a Serializable class to this value to * denote that the class has no Serializable fields. */ public static final ObjectStreamField[] NO_FIELDS = new ObjectStreamField[0]; /* * Entries held in the Cache of known ObjectStreamClass_1_3_1 objects. * Entries are chained together with the same hash value (modulo array size). */ private static class ObjectStreamClassEntry // extends java.lang.ref.SoftReference { ObjectStreamClassEntry(ObjectStreamClass_1_3_1 c) { //super(c); this.c = c; } ObjectStreamClassEntry next; public Object get() { return c; } private ObjectStreamClass_1_3_1 c; } /* * Comparator object for Classes and Interfaces */ private static Comparator compareClassByName = new CompareClassByName(); private static class CompareClassByName implements Comparator { public int compare(Object o1, Object o2) { Class c1 = (Class)o1; Class c2 = (Class)o2; return (c1.getName()).compareTo(c2.getName()); } } /* * Comparator object for Members, Fields, and Methods */ private static Comparator compareMemberByName = new CompareMemberByName(); private static class CompareMemberByName implements Comparator { public int compare(Object o1, Object o2) { String s1 = ((Member)o1).getName(); String s2 = ((Member)o2).getName(); if (o1 instanceof Method) { s1 += getSignature((Method)o1); s2 += getSignature((Method)o2); } else if (o1 instanceof Constructor) { s1 += getSignature((Constructor)o1); s2 += getSignature((Constructor)o2); } return s1.compareTo(s2); } } /* It is expensive to recompute a method or constructor signature many times, so compute it only once using this data structure. */ private static class MethodSignature implements Comparator { Member member; String signature; // cached parameter signature /* Given an array of Method or Constructor members, return a sorted array of the non-private members.*/ /* A better implementation would be to implement the returned data structure as an insertion sorted link list.*/ static MethodSignature[] removePrivateAndSort(Member[] m) { int numNonPrivate = 0; for (int i = 0; i < m.length; i++) { if (! Modifier.isPrivate(m[i].getModifiers())) { numNonPrivate++; } } MethodSignature[] cm = new MethodSignature[numNonPrivate]; int cmi = 0; for (int i = 0; i < m.length; i++) { if (! Modifier.isPrivate(m[i].getModifiers())) { cm[cmi] = new MethodSignature(m[i]); cmi++; } } if (cmi > 0) Arrays.sort(cm, cm[0]); return cm; } /* Assumes that o1 and o2 are either both methods or both constructors.*/ public int compare(Object o1, Object o2) { /* Arrays.sort calls compare when o1 and o2 are equal.*/ if (o1 == o2) return 0; MethodSignature c1 = (MethodSignature)o1; MethodSignature c2 = (MethodSignature)o2; int result; if (isConstructor()) { result = c1.signature.compareTo(c2.signature); } else { // is a Method. result = c1.member.getName().compareTo(c2.member.getName()); if (result == 0) result = c1.signature.compareTo(c2.signature); } return result; } final private boolean isConstructor() { return member instanceof Constructor; } private MethodSignature(Member m) { member = m; if (isConstructor()) { signature = ObjectStreamClass_1_3_1.getSignature((Constructor)m); } else { signature = ObjectStreamClass_1_3_1.getSignature((Method)m); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -