From 34998d6bb655f3270bc54656aa9519360100c2d8 Mon Sep 17 00:00:00 2001 From: Michael Koch Date: Tue, 30 Dec 2003 15:51:15 +0000 Subject: ObjectInputStream.java, [...]: Reformated, no functional code changes. 2003-12-30 Michael Koch * java/io/ObjectInputStream.java, java/io/ObjectOutputStream.java, java/io/ObjectStreamClass.java: Reformated, no functional code changes. From-SVN: r75236 --- libjava/java/io/ObjectStreamClass.java | 818 +++++++++++++++++---------------- 1 file changed, 412 insertions(+), 406 deletions(-) (limited to 'libjava/java/io/ObjectStreamClass.java') diff --git a/libjava/java/io/ObjectStreamClass.java b/libjava/java/io/ObjectStreamClass.java index 9d9d99d..5bd0e68 100644 --- a/libjava/java/io/ObjectStreamClass.java +++ b/libjava/java/io/ObjectStreamClass.java @@ -2,39 +2,39 @@ about serialized objects. Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc. -This file is part of GNU Classpath. + This file is part of GNU Classpath. -GNU Classpath is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. -GNU Classpath is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GNU Classpath; see the file COPYING. If not, write to the -Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA -02111-1307 USA. - -Linking this library statically or dynamically with other modules is -making a combined work based on this library. Thus, the terms and -conditions of the GNU General Public License cover the whole -combination. - -As a special exception, the copyright holders of this library give you -permission to link this library with independent modules to produce an -executable, regardless of the license terms of these independent -modules, and to copy and distribute the resulting executable under -terms of your choice, provided that you also meet, for each linked -independent module, the terms and conditions of the license of that -module. An independent module is a module which is not derived from -or based on this library. If you modify this library, you may extend -this exception to your version of the library, but you are not -obligated to do so. If you do not wish to do so, delete this -exception statement from your version. */ + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ package java.io; @@ -61,23 +61,23 @@ import gnu.java.security.provider.Gnu; public class ObjectStreamClass implements Serializable { /** - Returns the ObjectStreamClass for cl. - If cl is null, or is not Serializable, - null is returned. ObjectStreamClass's are memorized; - later calls to this method with the same class will return the - same ObjectStreamClass object and no recalculation - will be done. - - @see java.io.Serializable - */ - public static ObjectStreamClass lookup (Class cl) + * Returns the ObjectStreamClass for cl. + * If cl is null, or is not Serializable, + * null is returned. ObjectStreamClass's are memorized; + * later calls to this method with the same class will return the + * same ObjectStreamClass object and no recalculation + * will be done. + * + * @see java.io.Serializable + */ + public static ObjectStreamClass lookup(Class cl) { if (cl == null) return null; - if (! (Serializable.class).isAssignableFrom (cl)) + if (! (Serializable.class).isAssignableFrom(cl)) return null; - return lookupForClassObject (cl); + return lookupForClassObject(cl); } /** @@ -85,57 +85,57 @@ public class ObjectStreamClass implements Serializable * we have a java.lang.Class object C for class A, though A is not * serializable, but it's okay to serialize C. */ - static ObjectStreamClass lookupForClassObject (Class cl) + static ObjectStreamClass lookupForClassObject(Class cl) { if (cl == null) return null; - ObjectStreamClass osc = (ObjectStreamClass)classLookupTable.get (cl); + ObjectStreamClass osc = (ObjectStreamClass) classLookupTable.get(cl); if (osc != null) return osc; else - { - osc = new ObjectStreamClass (cl); - classLookupTable.put (cl, osc); - return osc; - } + { + osc = new ObjectStreamClass(cl); + classLookupTable.put(cl, osc); + return osc; + } } /** - Returns the name of the class that this - ObjectStreamClass represents. - */ - public String getName () + * Returns the name of the class that this + * ObjectStreamClass represents. + */ + public String getName() { return name; } /** - Returns the class that this ObjectStreamClass - represents. Null could be returned if this - ObjectStreamClass was read from an - ObjectInputStream and the class it represents cannot - be found or loaded. - - @see java.io.ObjectInputStream - */ - public Class forClass () + * Returns the class that this ObjectStreamClass + * represents. Null could be returned if this + * ObjectStreamClass was read from an + * ObjectInputStream and the class it represents cannot + * be found or loaded. + * + * @see java.io.ObjectInputStream + */ + public Class forClass() { return clazz; } /** - Returns the serial version stream-unique identifier for the class - represented by this ObjectStreamClass. This SUID is - either defined by the class as static final long - serialVersionUID or is calculated as specified in - Javasoft's "Object Serialization Specification" XXX: add reference - */ - public long getSerialVersionUID () + * Returns the serial version stream-unique identifier for the class + * represented by this ObjectStreamClass. This SUID is + * either defined by the class as static final long + * serialVersionUID or is calculated as specified in + * Javasoft's "Object Serialization Specification" XXX: add reference + */ + public long getSerialVersionUID() { return uid; } @@ -145,10 +145,10 @@ public class ObjectStreamClass implements Serializable // of the class represented by this ObjectStreamClass. The Fields // are sorted by name. // XXX doc - public ObjectStreamField[] getFields () + public ObjectStreamField[] getFields() { ObjectStreamField[] copy = new ObjectStreamField[ fields.length ]; - System.arraycopy (fields, 0, copy, 0, fields.length); + System.arraycopy(fields, 0, copy, 0, fields.length); return copy; } @@ -158,8 +158,8 @@ public class ObjectStreamClass implements Serializable // primitiveness. public ObjectStreamField getField (String name) { - for (int i=0; i < fields.length; i++) - if (fields[i].getName ().equals (name)) + for (int i = 0; i < fields.length; i++) + if (fields[i].getName().equals(name)) return fields[i]; return null; } @@ -174,7 +174,7 @@ public class ObjectStreamClass implements Serializable * @see #getSerialVersionUID() * @see #getName() */ - public String toString () + public String toString() { return "java.io.ObjectStreamClass< " + name + ", " + uid + " >"; } @@ -187,7 +187,7 @@ public class ObjectStreamClass implements Serializable // // This method is used by the class to override default // serialization behavior. - boolean hasWriteMethod () + boolean hasWriteMethod() { return (flags & ObjectStreamConstants.SC_WRITE_METHOD) != 0; } @@ -200,24 +200,24 @@ public class ObjectStreamClass implements Serializable // // This method is used by the class to override default // serialization behavior. - boolean hasReadMethod () + boolean hasReadMethod() { - try + try { - Class[] readObjectParams = { ObjectInputStream.class }; - forClass ().getDeclaredMethod ("readObject", readObjectParams); - return true; + Class[] readObjectParams = { ObjectInputStream.class }; + forClass().getDeclaredMethod("readObject", readObjectParams); + return true; } - catch (NoSuchMethodException e) + catch (NoSuchMethodException e) { - return false; + return false; } } // Returns true iff the class that this ObjectStreamClass represents // implements Serializable but does *not* implement Externalizable. - boolean isSerializable () + boolean isSerializable() { return (flags & ObjectStreamConstants.SC_SERIALIZABLE) != 0; } @@ -225,7 +225,7 @@ public class ObjectStreamClass implements Serializable // Returns true iff the class that this ObjectStreamClass represents // implements Externalizable. - boolean isExternalizable () + boolean isExternalizable() { return (flags & ObjectStreamConstants.SC_EXTERNALIZABLE) != 0; } @@ -235,7 +235,7 @@ public class ObjectStreamClass implements Serializable // class that is the superclass of the class this // ObjectStreamClass represents. If the superclass is // not Serializable, null is returned. - ObjectStreamClass getSuper () + ObjectStreamClass getSuper() { return superClass; } @@ -245,30 +245,30 @@ public class ObjectStreamClass implements Serializable // classes of CLAZZ and CLAZZ itself in order from most super to // CLAZZ. ObjectStreamClass[0] is the highest superclass of CLAZZ // that is serializable. - static ObjectStreamClass[] getObjectStreamClasses (Class clazz) + static ObjectStreamClass[] getObjectStreamClasses(Class clazz) { - ObjectStreamClass osc = ObjectStreamClass.lookup (clazz); + ObjectStreamClass osc = ObjectStreamClass.lookup(clazz); if (osc == null) return new ObjectStreamClass[0]; else - { - Vector oscs = new Vector (); - - while (osc != null) { - oscs.addElement (osc); - osc = osc.getSuper (); - } + Vector oscs = new Vector(); - int count = oscs.size (); - ObjectStreamClass[] sorted_oscs = new ObjectStreamClass[ count ]; + while (osc != null) + { + oscs.addElement (osc); + osc = osc.getSuper(); + } + + int count = oscs.size(); + ObjectStreamClass[] sorted_oscs = new ObjectStreamClass[ count ]; - for (int i = count - 1; i >= 0; i--) - sorted_oscs[ count - i - 1 ] = (ObjectStreamClass)oscs.elementAt (i); + for (int i = count - 1; i >= 0; i--) + sorted_oscs[ count - i - 1 ] = (ObjectStreamClass) oscs.elementAt(i); - return sorted_oscs; - } + return sorted_oscs; + } } @@ -276,14 +276,14 @@ public class ObjectStreamClass implements Serializable // properties of the class represented by this ObjectStreamClass. // The bit-flags that could be present are those defined in // ObjectStreamConstants that begin with `SC_' - int getFlags () + int getFlags() { return flags; } - ObjectStreamClass (String name, long uid, byte flags, - ObjectStreamField[] fields) + ObjectStreamClass(String name, long uid, byte flags, + ObjectStreamField[] fields) { this.name = name; this.uid = uid; @@ -302,11 +302,11 @@ public class ObjectStreamClass implements Serializable * @throws InvalidClassException if an incompatibility between computed UID and * already set UID is found. */ - void setClass (Class cl, ObjectStreamClass superClass) throws InvalidClassException + void setClass(Class cl, ObjectStreamClass superClass) throws InvalidClassException { this.clazz = cl; - long class_uid = getClassUID (cl); + long class_uid = getClassUID(cl); if (uid == 0) uid = class_uid; else @@ -322,87 +322,89 @@ public class ObjectStreamClass implements Serializable } } - isProxyClass = clazz != null && Proxy.isProxyClass (clazz); + isProxyClass = clazz != null && Proxy.isProxyClass(clazz); this.superClass = superClass; - calculateOffsets (); - + calculateOffsets(); + try { - ObjectStreamField[] exportedFields = getSerialPersistentFields (clazz); - - if (exportedFields == null) - return; - - ObjectStreamField[] newFieldList = new ObjectStreamField[exportedFields.length + fields.length]; - int i, j, k; - - /* We now check the import fields against the exported fields. - * There should not be contradiction (e.g. int x and String x) - * but extra virtual fields can be added to the class. - */ - - Arrays.sort(exportedFields); - - i = 0; j = 0; k = 0; - while (i < fields.length && j < exportedFields.length) - { - int comp = fields[i].getName().compareTo (exportedFields[j].getName()); - if (comp < 0) - { - newFieldList[k] = fields[i]; - fields[i].setPersistent(false); - fields[i].setToSet(false); - i++; - } - else if (comp > 0) - { - /* field not found in imported fields. We add it - * in the list of supported fields. - */ - newFieldList[k] = exportedFields[j]; - newFieldList[k].setPersistent(true); - newFieldList[k].setToSet(false); - j++; - } - else - { - if (!fields[i].getType().equals (exportedFields[j].getType())) - throw new InvalidClassException ("serialPersistentFields must be compatible with" + - " imported fields (about " + fields[i].getName() + ")"); - newFieldList[k] = fields[i]; - fields[i].setPersistent(true); - i++; - j++; - } - k++; - } - - if (i < fields.length) - for (; i < fields.length; i++, k++) - { - fields[i].setPersistent(false); - fields[i].setToSet(false); - newFieldList[k] = fields[i]; - } - else - if (j < exportedFields.length) - for (; j < exportedFields.length; j++, k++) - { - exportedFields[j].setPersistent(true); - exportedFields[j].setToSet(false); - newFieldList[k] = exportedFields[j]; - } - - fields = new ObjectStreamField[k]; - System.arraycopy (newFieldList, 0, fields, 0, k); + ObjectStreamField[] exportedFields = getSerialPersistentFields (clazz); + + if (exportedFields == null) + return; + + ObjectStreamField[] newFieldList = new ObjectStreamField[exportedFields.length + fields.length]; + int i, j, k; + + /* We now check the import fields against the exported fields. + * There should not be contradiction (e.g. int x and String x) + * but extra virtual fields can be added to the class. + */ + + Arrays.sort(exportedFields); + + i = 0; j = 0; k = 0; + while (i < fields.length && j < exportedFields.length) + { + int comp = fields[i].getName().compareTo(exportedFields[j].getName()); + + if (comp < 0) + { + newFieldList[k] = fields[i]; + fields[i].setPersistent(false); + fields[i].setToSet(false); + i++; + } + else if (comp > 0) + { + /* field not found in imported fields. We add it + * in the list of supported fields. + */ + newFieldList[k] = exportedFields[j]; + newFieldList[k].setPersistent(true); + newFieldList[k].setToSet(false); + j++; + } + else + { + if (!fields[i].getType().equals(exportedFields[j].getType())) + throw new InvalidClassException + ("serialPersistentFields must be compatible with" + + " imported fields (about " + fields[i].getName() + ")"); + newFieldList[k] = fields[i]; + fields[i].setPersistent(true); + i++; + j++; + } + k++; + } + + if (i < fields.length) + for (;i"); - data_out.writeInt (Modifier.STATIC); - data_out.writeUTF ("()V"); - } + // write class initializer method if present + if (VMObjectStreamClass.hasClassInitializer(cl)) + { + data_out.writeUTF(""); + data_out.writeInt(Modifier.STATIC); + data_out.writeUTF("()V"); + } - Constructor constructor; - Constructor[] constructors = cl.getDeclaredConstructors (); - Arrays.sort (constructors, memberComparator); - for (int i=0; i < constructors.length; i++) - { - constructor = constructors[i]; - modifiers = constructor.getModifiers (); - if (Modifier.isPrivate (modifiers)) - continue; - - data_out.writeUTF (""); - data_out.writeInt (modifiers); - - // the replacement of '/' with '.' was needed to make computed - // SUID's agree with those computed by JDK - data_out.writeUTF ( - TypeSignature.getEncodingOfConstructor (constructor).replace ('/','.')); - } + Constructor constructor; + Constructor[] constructors = cl.getDeclaredConstructors(); + Arrays.sort (constructors, memberComparator); + for (int i = 0; i < constructors.length; i++) + { + constructor = constructors[i]; + modifiers = constructor.getModifiers(); + if (Modifier.isPrivate(modifiers)) + continue; + + data_out.writeUTF(""); + data_out.writeInt(modifiers); + + // the replacement of '/' with '.' was needed to make computed + // SUID's agree with those computed by JDK + data_out.writeUTF + (TypeSignature.getEncodingOfConstructor(constructor).replace('/','.')); + } - Method method; - Method[] methods = cl.getDeclaredMethods (); - Arrays.sort (methods, memberComparator); - for (int i=0; i < methods.length; i++) - { - method = methods[i]; - modifiers = method.getModifiers (); - if (Modifier.isPrivate (modifiers)) - continue; - - data_out.writeUTF (method.getName ()); - data_out.writeInt (modifiers); - - // the replacement of '/' with '.' was needed to make computed - // SUID's agree with those computed by JDK - data_out.writeUTF ( - TypeSignature.getEncodingOfMethod (method).replace ('/', '.')); - } + Method method; + Method[] methods = cl.getDeclaredMethods(); + Arrays.sort(methods, memberComparator); + for (int i = 0; i < methods.length; i++) + { + method = methods[i]; + modifiers = method.getModifiers(); + if (Modifier.isPrivate(modifiers)) + continue; + + data_out.writeUTF(method.getName()); + data_out.writeInt(modifiers); + + // the replacement of '/' with '.' was needed to make computed + // SUID's agree with those computed by JDK + data_out.writeUTF + (TypeSignature.getEncodingOfMethod(method).replace('/', '.')); + } - data_out.close (); - byte[] sha = md.digest (); - long result = 0; - int len = sha.length < 8 ? sha.length : 8; - for (int i=0; i < len; i++) - result += (long)(sha[i] & 0xFF) << (8 * i); + data_out.close(); + byte[] sha = md.digest(); + long result = 0; + int len = sha.length < 8 ? sha.length : 8; + for (int i = 0; i < len; i++) + result += (long) (sha[i] & 0xFF) << (8 * i); - return result; - } + return result; + } catch (NoSuchAlgorithmException e) - { - throw new RuntimeException ("The SHA algorithm was not found to use in computing the Serial Version UID for class " - + cl.getName (), e); - } + { + throw new RuntimeException + ("The SHA algorithm was not found to use in computing the Serial Version UID for class " + + cl.getName(), e); + } catch (IOException ioe) - { - throw new RuntimeException (ioe); - } + { + throw new RuntimeException(ioe); + } } /** @@ -712,7 +718,7 @@ public class ObjectStreamClass implements Serializable * @param clazz Class to retrieve 'serialPersistentFields' from. * @return The content of 'serialPersistentFields'. */ - private ObjectStreamField[] getSerialPersistentFields (Class clazz) + private ObjectStreamField[] getSerialPersistentFields(Class clazz) throws NoSuchFieldException, IllegalAccessException { ObjectStreamField[] fieldsArray = null; @@ -724,9 +730,9 @@ public class ObjectStreamClass implements Serializable f.setAccessible(true); int modifiers = f.getModifiers(); - if (!(Modifier.isStatic(modifiers) - && Modifier.isFinal(modifiers) - && Modifier.isPrivate(modifiers))) + if (!(Modifier.isStatic(modifiers) && + Modifier.isFinal(modifiers) && + Modifier.isPrivate(modifiers))) return null; o = (ObjectStreamField[]) f.get(null); @@ -734,7 +740,7 @@ public class ObjectStreamClass implements Serializable if (o == null) return null; - fieldsArray = new ObjectStreamField[o.length]; + fieldsArray = new ObjectStreamField[ o.length ]; System.arraycopy(o, 0, fieldsArray, 0, o.length); return fieldsArray; @@ -742,10 +748,10 @@ public class ObjectStreamClass implements Serializable public static final ObjectStreamField[] NO_FIELDS = {}; - private static Hashtable classLookupTable = new Hashtable (); - private static final NullOutputStream nullOutputStream = new NullOutputStream (); - private static final Comparator interfaceComparator = new InterfaceComparator (); - private static final Comparator memberComparator = new MemberComparator (); + private static Hashtable classLookupTable = new Hashtable(); + private static final NullOutputStream nullOutputStream = new NullOutputStream(); + private static final Comparator interfaceComparator = new InterfaceComparator(); + private static final Comparator memberComparator = new MemberComparator(); private static final Class[] writeMethodArgTypes = { java.io.ObjectOutputStream.class }; @@ -775,9 +781,9 @@ public class ObjectStreamClass implements Serializable // interfaces are compared only by name class InterfaceComparator implements Comparator { - public int compare (Object o1, Object o2) + public int compare(Object o1, Object o2) { - return ((Class)o1).getName ().compareTo (((Class)o2).getName ()); + return ((Class) o1).getName().compareTo(((Class) o2).getName()); } } @@ -786,16 +792,16 @@ class InterfaceComparator implements Comparator // conflicts are resolved by comparing type signatures class MemberComparator implements Comparator { - public int compare (Object o1, Object o2) + public int compare(Object o1, Object o2) { - Member m1 = (Member)o1; - Member m2 = (Member)o2; + Member m1 = (Member) o1; + Member m2 = (Member) o2; - int comp = m1.getName ().compareTo (m2.getName ()); + int comp = m1.getName().compareTo(m2.getName()); if (comp == 0) - return TypeSignature.getEncodingOfMember (m1). - compareTo (TypeSignature.getEncodingOfMember (m2)); + return TypeSignature.getEncodingOfMember(m1). + compareTo(TypeSignature.getEncodingOfMember(m2)); else return comp; } -- cgit v1.1