/** * Override coverts old class names into new class names to preserve compatibility with * pre-Apache namespaces. */ @Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { ObjectStreamClass read = super.readClassDescriptor(); if (read.getName().startsWith("com.fs.pxe.")) { return ObjectStreamClass.lookup( Class.forName(read.getName().replace("com.fs.pxe.", "org.apache.ode."))); } if (read.getName().startsWith("com.fs.utils.")) { return ObjectStreamClass.lookup( Class.forName(read.getName().replace("com.fs.utils.", "org.apache.ode.utils."))); } return read; }
/** * Computes the serial version UID value for the given class. The code is taken from {@link * ObjectStreamClass#computeDefaultSUID(Class)}. * * @param cls A class. * @return A serial version UID. * @throws IOException If failed. */ static long computeSerialVersionUid(Class cls) throws IOException { if (Serializable.class.isAssignableFrom(cls) && !Enum.class.isAssignableFrom(cls)) { return ObjectStreamClass.lookup(cls).getSerialVersionUID(); } MessageDigest md; try { md = MessageDigest.getInstance("SHA"); } catch (NoSuchAlgorithmException e) { throw new IOException("Failed to get digest for SHA.", e); } md.update(cls.getName().getBytes(UTF_8)); for (Field f : getFieldsForSerialization(cls)) { md.update(f.getName().getBytes(UTF_8)); md.update(f.getType().getName().getBytes(UTF_8)); } byte[] hashBytes = md.digest(); long hash = 0; // Composes a single-long hash from the byte[] hash. for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) hash = (hash << 8) | (hashBytes[i] & 0xFF); return hash; }
/* */ public String[] bases(String paramString) /* */ { /* */ try /* */ { /* 163 */ if (this.vhandler == null) { /* 164 */ this.vhandler = ValueHandlerImpl.getInstance(false); /* */ } /* */ /* 167 */ Stack localStack = new Stack(); /* 168 */ Class localClass = ObjectStreamClass.lookup(this.vhandler.getClassFromType(paramString)) .forClass() .getSuperclass(); /* */ /* 170 */ while (!localClass.equals(Object.class)) { /* 171 */ localStack.push(this.vhandler.createForAnyType(localClass)); /* 172 */ localClass = localClass.getSuperclass(); /* */ } /* */ /* 175 */ String[] arrayOfString = new String[localStack.size()]; /* 176 */ for (int i = arrayOfString.length - 1; i >= 0; i++) { /* 177 */ arrayOfString[i] = ((String) localStack.pop()); /* */ } /* 179 */ return arrayOfString; /* */ } catch (Throwable localThrowable) { /* 181 */ throw this.wrapper.missingLocalValueImpl( CompletionStatus.COMPLETED_MAYBE, localThrowable); /* */ } /* */ }
public static void writeObject(Object obj, SerializationInfo info) { try { boolean replaced = false; Class cl = obj.getClass(); ObjectStreamClass desc; for (; ; ) { Class repCl; desc = ObjectStreamClass.lookup(cl, true); if (!desc.hasWriteReplaceMethod() || (obj = desc.invokeWriteReplace(obj)) == null || (repCl = obj.getClass()) == cl) { break; } cl = repCl; replaced = true; } if (replaced) { info.AddValue("obj", obj); info.SetType(ikvm.runtime.Util.getInstanceTypeFromClass(ReplaceProxy.class)); } else { new InteropObjectOutputStream(info, obj, cl, desc); } } catch (IOException x) { ikvm.runtime.Util.throwException(new SerializationException(x.getMessage(), x)); } }
@Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { ObjectStreamClass classDesc = super.readClassDescriptor(); String className = classDesc.getName(); return renamedClasses.containsKey(className) ? ObjectStreamClass.lookup(renamedClasses.get(className)) : classDesc; }
class TestObjectOutputStream extends ObjectOutputStream { static ObjectStreamClass fooDesc = ObjectStreamClass.lookup(Foo.class); static ObjectStreamClass integerDesc = ObjectStreamClass.lookup(Integer.class); TestObjectOutputStream(OutputStream out) throws IOException { super(out); } protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { if (desc == fooDesc) { super.writeClassDescriptor(integerDesc); } else if (desc == integerDesc) { super.writeClassDescriptor(fooDesc); } else { super.writeClassDescriptor(desc); } } }
protected void annotateClass(Class cl) throws IOException { super.annotateClass(cl); if (verbose) { ObjectStreamClass desc = ObjectStreamClass.lookup(cl); System.out.println("******************"); System.out.println("annotateClass(" + cl.getName() + ")"); System.out.println("******************"); SerialBox.print(desc); } }
/* */ public FullValueDescription meta(String paramString) /* */ { /* */ try { /* 117 */ FullValueDescription localFullValueDescription = (FullValueDescription) fvds.get(paramString); /* */ /* 119 */ if (localFullValueDescription == null) /* */ { /* 122 */ if (this.vhandler == null) { /* 123 */ this.vhandler = ValueHandlerImpl.getInstance(false); /* */ } /* */ try /* */ { /* 127 */ localFullValueDescription = ValueUtility.translate( _orb(), ObjectStreamClass.lookup(this.vhandler.getAnyClassFromType(paramString)), this.vhandler); /* */ } /* */ catch (Throwable localThrowable2) { /* 130 */ if (this.orb == null) /* 131 */ this.orb = ORB.init(); /* 132 */ localFullValueDescription = ValueUtility.translate( this.orb, ObjectStreamClass.lookup(this.vhandler.getAnyClassFromType(paramString)), this.vhandler); /* */ } /* */ /* 136 */ if (localFullValueDescription != null) /* 137 */ fvds.put(paramString, localFullValueDescription); /* */ else { /* 139 */ throw this.wrapper.missingLocalValueImpl(CompletionStatus.COMPLETED_MAYBE); /* */ } /* */ } /* */ /* 143 */ return localFullValueDescription; /* */ } catch (Throwable localThrowable1) { /* 145 */ throw this.wrapper.incompatibleValueImpl( CompletionStatus.COMPLETED_MAYBE, localThrowable1); /* */ } /* */ }
protected Class resolveClass(ObjectStreamClass v) throws ClassNotFoundException, IOException { if (verbose) { System.out.println("*******************************BEGIN"); System.out.println("**Stream ObjectStreamClass Descriptor"); SerialBox.print(v); } Class cl = super.resolveClass(v); if (verbose) { ObjectStreamClass localDesc = ObjectStreamClass.lookup(cl); if (localDesc != null) { System.out.println("**Local JVM ObjectStreamClass Descriptor"); SerialBox.print(localDesc); } System.out.println("*******************************END "); } return cl; }
public static void main(String args[]) throws Exception { File f = new File("tmp.ser"); if (args[0].compareTo("-s") == 0) { FileOutputStream of = new FileOutputStream(f); ObjectOutputStream oos = new ObjectOutputStream(of); Class cl = Class.forName(args[1]); oos.writeObject(cl); if (ObjectStreamClass.lookup(cl) != null) oos.writeObject(cl.newInstance()); oos.close(); System.out.println("Serialized Class " + cl.getName()); } else if (args[0].compareTo("-de") == 0) { FileInputStream inf = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(inf); Class cl = null; try { cl = (Class) ois.readObject(); throw new Error("Expected InvalidClassException to be thrown"); } catch (InvalidClassException e) { System.out.println("Caught expected exception DeSerializing class " + e.getMessage()); } ois.close(); } else if (args[0].compareTo("-doe") == 0) { FileInputStream inf = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(inf); Class cl = null; cl = (Class) ois.readObject(); try { ois.readObject(); throw new Error("Expected InvalidClassException to be thrown"); } catch (InvalidClassException e) { System.out.println("Caught expected exception DeSerializing class " + e.getMessage()); } ois.close(); } else if (args[0].compareTo("-d") == 0) { FileInputStream inf = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(inf); Class cl = (Class) ois.readObject(); try { ois.readObject(); } catch (EOFException e) { } ois.close(); System.out.println("DeSerialized Class " + cl.getName()); } }
private static Map<String, Long> getMessageSVUID(Type messageType) throws SJIOException, ClassNotFoundException // SJCompilerUtils has a simpler version of this routine. { HashMap<String, Long> ours = new HashMap<String, Long>(); if (messageType instanceof SJSessionType) // Should come before ordinary class cases? (But SJSessionType shouldn't be a // class type). { ours.putAll(getClassSVUIDs((SJSessionType) messageType)); } else if (messageType.isPrimitive()) { // No SVUID needed for primitive types. } else if (messageType.isClass()) // Duplicated from above. { String className = messageType.toClass().fullName(); Class<?> c = Class.forName(className); if (c.isInterface()) { // Interfaces don't have SVUIDs (so what should we do here?). // This encourages use of // abstract classes rather than interfaces for message types? } else { ObjectStreamClass osc = ObjectStreamClass.lookup(c); if (osc == null) { throw new SJIOException("Class not serializable: " + c); } ours.put( className, osc .getSerialVersionUID()); // Not possible to find a different SVUID for the same // (name) class here? // SVUIDs could be recorded in the // session type objects. // Put currently problems working // with these values at compilation time if the class // binary is not available a priori (SVUID value not built // yet?). } } else if (messageType.isArray()) { throw new SJIOException("Array types not done yet: " + messageType); } return ours; }
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { hookCalled = true; return ObjectStreamClass.lookup(Class.forName(readUTF())); }
/** * Deserializes examplars stored in archives in getArchiveDirectory(). * * @throws RuntimeException if clazz cannot be serialized. This exception has an informative * message and wraps the originally thrown exception as root cause. * @see #getArchiveDirectory() */ public void deserializeArchivedVersions() throws RuntimeException { System.out.println("Deserializing archived instances in " + getArchiveDirectory() + "."); File archive = new File(getArchiveDirectory()); if (!archive.exists() || !archive.isDirectory()) { return; } String[] listing = archive.list(); for (String archiveName : listing) { if (!(archiveName.endsWith(".zip"))) { continue; } try { File file = new File(getArchiveDirectory(), archiveName); ZipFile zipFile = new ZipFile(file); ZipEntry entry = zipFile.getEntry("class_fields.ser"); InputStream inputStream = zipFile.getInputStream(entry); ObjectInputStream objectIn = new ObjectInputStream(inputStream); Map<String, List<String>> classFields = (Map<String, List<String>>) objectIn.readObject(); zipFile.close(); for (String className : classFields.keySet()) { // if (classFields.equals("HypotheticalGraph")) continue; List<String> fieldNames = classFields.get(className); Class<?> clazz = Class.forName(className); ObjectStreamClass streamClass = ObjectStreamClass.lookup(clazz); if (streamClass == null) { System.out.println(); } for (String fieldName : fieldNames) { assert streamClass != null; ObjectStreamField field = streamClass.getField(fieldName); if (field == null) { throw new RuntimeException( "Field '" + fieldName + "' was dropped from class '" + className + "' as a serializable field! Please " + "put it back!!!" + "\nIt used to be in " + className + " in this archive: " + archiveName + "."); } } } } catch (ClassNotFoundException e) { throw new RuntimeException( "Could not read class_fields.ser in archive + " + archiveName + " .", e); } catch (IOException e) { throw new RuntimeException("Problem reading archive" + archiveName + "; see cause.", e); } System.out.println("...Deserializing instances in " + archiveName + "..."); ZipEntry zipEntry = null; try { File file = new File(getArchiveDirectory(), archiveName); FileInputStream in = new FileInputStream(file); ZipInputStream zipinputstream = new ZipInputStream(in); while ((zipEntry = zipinputstream.getNextEntry()) != null) { if (!zipEntry.getName().endsWith(".ser")) { continue; } ObjectInputStream objectIn = new ObjectInputStream(zipinputstream); objectIn.readObject(); zipinputstream.closeEntry(); } zipinputstream.close(); } catch (ClassNotFoundException e) { throw new RuntimeException( "Could not read object zipped file " + zipEntry.getName() + " in archive " + archiveName + ". " + "Perhaps the class was renamed, moved to another package, or " + "removed. In any case, please put it back where it was.", e); } catch (IOException e) { throw new RuntimeException("Problem reading archive" + archiveName + "; see cause.", e); } } System.out.println("Finished deserializing archived instances."); }
/** * Serializes the given class to the getCurrentDirectory() directory. The static * serializedInstance() method of clazz will be called to get an examplar of clazz. This examplar * will then be serialized out to a file stored in getCurrentDirectory(). * * @param clazz the class to serialize. * @throws RuntimeException if clazz cannot be serialized. This exception has an informative * message and wraps the originally thrown exception as root cause. * @see #getCurrentDirectory() */ private void serializeClass(Class clazz, Map<String, List<String>> classFields) throws RuntimeException { File current = new File(getCurrentDirectory()); if (!current.exists() || !current.isDirectory()) { throw new IllegalStateException( "There is no " + current.getAbsolutePath() + " directory. " + "\nThis is where the serialized classes should be. " + "Please run serializeCurrentDirectory() first."); } try { Field field = clazz.getDeclaredField("serialVersionUID"); int modifiers = field.getModifiers(); boolean _static = Modifier.isStatic(modifiers); boolean _final = Modifier.isFinal(modifiers); field.setAccessible(true); if (!_static || !_final || !(23L == field.getLong(null))) { throw new RuntimeException( "Class " + clazz + " does not define static final " + "long serialVersionUID = 23L"); } int numFields = getNumNonSerialVersionUIDFields(clazz); if (numFields > 0) { Method method = clazz.getMethod("serializableInstance"); Object object = method.invoke(null); File file = new File(current, clazz.getName() + ".ser"); boolean created = file.createNewFile(); FileOutputStream out = new FileOutputStream(file); ObjectOutputStream objOut = new ObjectOutputStream(out); objOut.writeObject(object); out.close(); } // Make entry in list of class fields. ObjectStreamClass objectStreamClass = ObjectStreamClass.lookup(clazz); String className = objectStreamClass.getName(); ObjectStreamField[] fields = objectStreamClass.getFields(); @SuppressWarnings("Convert2Diamond") List<String> fieldList = new ArrayList<>(); for (ObjectStreamField objectStreamField : fields) { String fieldName = objectStreamField.getName(); fieldList.add(fieldName); } classFields.put(className, fieldList); } catch (NoSuchFieldException e) { throw new RuntimeException( ("There is no static final long field " + "'serialVersionUID' in " + clazz + ". Please make one and set it " + "to 23L.")); } catch (NoSuchMethodException e) { throw new RuntimeException( "Class " + clazz + "does not " + "have a public static serializableInstance constructor.", e); } catch (IllegalAccessException e) { throw new RuntimeException( "The method serializableInstance() of " + "class " + clazz + " is not public.", e); } catch (InvocationTargetException e) { throw new RuntimeException( "Unable to statically call the " + "serializableInstance() method of class " + clazz + ".", e); } catch (IOException e) { throw new RuntimeException( "Could not create a new, writeable file " + "in " + getCurrentDirectory() + " when trying to serialize " + clazz + ".", e); } }