Search in sources :

Example 66 with ObjectStreamClass

use of java.io.ObjectStreamClass in project tetrad by cmu-phil.

the class DecompressibleInputStream method readClassDescriptor.

@Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
    // initially streams descriptor
    ObjectStreamClass resultClassDescriptor = super.readClassDescriptor();
    // the class in the local JVM that this descriptor represents.
    Class localClass;
    try {
        localClass = Class.forName(resultClassDescriptor.getName());
    } catch (ClassNotFoundException e) {
        LOGGER.error("No local class for " + resultClassDescriptor.getName(), e);
        return resultClassDescriptor;
    }
    ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);
    if (localClassDescriptor != null) {
        // only if class implements serializable
        final long localSUID = localClassDescriptor.getSerialVersionUID();
        final long streamSUID = resultClassDescriptor.getSerialVersionUID();
        if (streamSUID != localSUID) {
            // check for serialVersionUID mismatch.
            // Use local class descriptor for deserialization
            resultClassDescriptor = localClassDescriptor;
        }
    }
    return resultClassDescriptor;
}
Also used : ObjectStreamClass(java.io.ObjectStreamClass) ObjectStreamClass(java.io.ObjectStreamClass)

Example 67 with ObjectStreamClass

use of java.io.ObjectStreamClass in project evosuite by EvoSuite.

the class CreateClassResetClassAdapter method determineSerialisableUID.

@Deprecated
private void determineSerialisableUID() {
    try {
        Class<?> clazz = Class.forName(className.replace('/', '.'), false, MethodCallReplacementClassAdapter.class.getClassLoader());
        if (Serializable.class.isAssignableFrom(clazz)) {
            ObjectStreamClass c = ObjectStreamClass.lookup(clazz);
            serialUID = c.getSerialVersionUID();
        }
    } catch (ClassNotFoundException e) {
        logger.info("Failed to add serialId to class " + className + ": " + e.getMessage());
    }
}
Also used : ObjectStreamClass(java.io.ObjectStreamClass)

Example 68 with ObjectStreamClass

use of java.io.ObjectStreamClass in project HotswapAgent by HotswapProjects.

the class ProxyObjectInputStream method readClassDescriptor.

protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
    boolean isProxy = readBoolean();
    if (isProxy) {
        String name = (String) readObject();
        Class superClass = loader.loadClass(name);
        int length = readInt();
        Class[] interfaces = new Class[length];
        for (int i = 0; i < length; i++) {
            name = (String) readObject();
            interfaces[i] = loader.loadClass(name);
        }
        length = readInt();
        byte[] signature = new byte[length];
        read(signature);
        ProxyFactory factory = new ProxyFactory();
        // we must always use the cache and never use writeReplace when using
        // ProxyObjectOutputStream and ProxyObjectInputStream
        factory.setUseCache(true);
        factory.setUseWriteReplace(false);
        factory.setSuperclass(superClass);
        factory.setInterfaces(interfaces);
        Class proxyClass = factory.createClass(signature);
        return ObjectStreamClass.lookup(proxyClass);
    } else {
        return super.readClassDescriptor();
    }
}
Also used : ObjectStreamClass(java.io.ObjectStreamClass)

Example 69 with ObjectStreamClass

use of java.io.ObjectStreamClass in project apex-core by apache.

the class FSRecoveryHandler method restore.

@Override
public Object restore() throws IOException {
    FileContext fc = FileContext.getFileContext(fs.getUri());
    // recover from wherever it was left
    if (fc.util().exists(snapshotBackupPath)) {
        LOG.warn("Incomplete checkpoint, reverting to {}", snapshotBackupPath);
        fc.rename(snapshotBackupPath, snapshotPath, Rename.OVERWRITE);
        // combine logs (w/o append, create new file)
        Path tmpLogPath = new Path(basedir, "log.combined");
        try (FSDataOutputStream fsOut = fc.create(tmpLogPath, EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE))) {
            try (FSDataInputStream fsIn = fc.open(logBackupPath)) {
                IOUtils.copy(fsIn, fsOut);
            }
            try (FSDataInputStream fsIn = fc.open(logPath)) {
                IOUtils.copy(fsIn, fsOut);
            }
        }
        fc.rename(tmpLogPath, logPath, Rename.OVERWRITE);
        fc.delete(logBackupPath, false);
    } else {
        // failure between log rotation and writing checkpoint
        if (fc.util().exists(logBackupPath)) {
            LOG.warn("Found {}, did checkpointing fail?", logBackupPath);
            fc.rename(logBackupPath, logPath, Rename.OVERWRITE);
        }
    }
    if (!fc.util().exists(snapshotPath)) {
        LOG.debug("No existing checkpoint.");
        return null;
    }
    LOG.debug("Reading checkpoint {}", snapshotPath);
    InputStream is = fc.open(snapshotPath);
    // indeterministic class loading behavior
    // http://stackoverflow.com/questions/9110677/readresolve-not-working-an-instance-of-guavas-serializedform-appears
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try (ObjectInputStream ois = new ObjectInputStream(is) {

        @Override
        protected Class<?> resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException {
            return Class.forName(objectStreamClass.getName(), true, loader);
        }
    }) {
        return ois.readObject();
    } catch (ClassNotFoundException cnfe) {
        throw new IOException("Failed to read checkpointed state", cnfe);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) DataInputStream(java.io.DataInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) InputStream(java.io.InputStream) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) FileContext(org.apache.hadoop.fs.FileContext) ObjectInputStream(java.io.ObjectInputStream)

Example 70 with ObjectStreamClass

use of java.io.ObjectStreamClass in project graal by oracle.

the class SerializationBuilder method registerIncludingAssociatedClasses.

private void registerIncludingAssociatedClasses(ConfigurationCondition condition, Class<?> clazz, Set<Class<?>> alreadyVisited) {
    if (alreadyVisited.contains(clazz)) {
        return;
    }
    alreadyVisited.add(clazz);
    String targetClassName = clazz.getName();
    // always an Object.
    if (clazz.isPrimitive()) {
        Class<?> boxedType = JavaKind.fromJavaClass(clazz).toBoxedJavaClass();
        registerIncludingAssociatedClasses(condition, boxedType, alreadyVisited);
        return;
    } else if (!Serializable.class.isAssignableFrom(clazz)) {
        warn("Class " + targetClassName + " does not implement java.io.Serializable and was not registered for object serialization.\n");
        return;
    } else if (access.findSubclasses(clazz).size() > 1) {
        // The classes returned from access.findSubclasses API including the base class itself
        warn("Class " + targetClassName + " has subclasses. No classes were registered for object serialization.\n");
        return;
    }
    try {
        clazz.getDeclaredMethod("writeObject", ObjectOutputStream.class);
        warn("Class " + targetClassName + " implements its own writeObject method for object serialization. Any serialization types it uses need to be explicitly registered.\n");
        return;
    } catch (NoSuchMethodException e) {
    // Expected case. Do nothing
    }
    register(condition, clazz);
    if (clazz.isArray()) {
        registerIncludingAssociatedClasses(condition, clazz.getComponentType(), alreadyVisited);
        return;
    }
    ObjectStreamClass osc = ObjectStreamClass.lookup(clazz);
    try {
        for (Object o : (Object[]) getDataLayoutMethod.invoke(osc)) {
            ObjectStreamClass desc = (ObjectStreamClass) descField.get(o);
            if (!desc.equals(osc)) {
                registerIncludingAssociatedClasses(condition, desc.forClass(), alreadyVisited);
            }
        }
    } catch (ReflectiveOperationException e) {
        VMError.shouldNotReachHere("Cannot register serialization classes due to", e);
    }
    for (ObjectStreamField field : osc.getFields()) {
        registerIncludingAssociatedClasses(condition, field.getType(), alreadyVisited);
    }
}
Also used : ObjectStreamField(java.io.ObjectStreamField) ObjectStreamClass(java.io.ObjectStreamClass)

Aggregations

ObjectStreamClass (java.io.ObjectStreamClass)70 IOException (java.io.IOException)27 ObjectInputStream (java.io.ObjectInputStream)27 ByteArrayInputStream (java.io.ByteArrayInputStream)25 ObjectOutputStream (java.io.ObjectOutputStream)14 ObjectStreamField (java.io.ObjectStreamField)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Serializable (java.io.Serializable)7 InvalidClassException (java.io.InvalidClassException)3 Field (java.lang.reflect.Field)3 ORecordBytes (com.orientechnologies.orient.core.record.impl.ORecordBytes)2 File (java.io.File)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 JavaCompiler (javax.tools.JavaCompiler)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 Test (org.junit.Test)2 ParseException (org.locationtech.jts.io.ParseException)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)1 OGroup (eu.esdihumboldt.hale.common.instance.orient.OGroup)1