Java Code Examples for java.io.ObjectStreamClass

The following examples show how to use java.io.ObjectStreamClass. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: hibernate4-memcached   Source File: CacheItem.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Compare targetClassSerialVersionUID and current JVM's targetClass serialVersionUID.
 * If they are same return true else return false.
 */
public boolean isTargetClassAndCurrentJvmTargetClassMatch() {
    Class<?> targetClassOnThisJVM;

    // JVM에 Class가 없는 상황
    try {
        targetClassOnThisJVM = Class.forName(targetClassName);
    } catch (ClassNotFoundException e) {
        log.error("target class " + targetClassName + " does not exist on this JVM.");
        return false;
    }

    ObjectStreamClass osc = ObjectStreamClass.lookup(targetClassOnThisJVM);

    // JVM Class가 Not serializable
    if (osc == null) {
        return false;
    }
    return targetClassSerialVersionUID == osc.getSerialVersionUID();
}
 
Example 2
@Override
protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
	try {
		if (this.classLoader != null) {
			// Use the specified ClassLoader to resolve local classes.
			return ClassUtils.forName(classDesc.getName(), this.classLoader);
		}
		else {
			// Use the default ClassLoader...
			return super.resolveClass(classDesc);
		}
	}
	catch (ClassNotFoundException ex) {
		return resolveFallbackIfPossible(classDesc.getName(), ex);
	}
}
 
Example 3
/**
 * Write and read class descriptors to/from a stream.
 * Arguments: <# cycles>
 */
public long run(String[] args) throws Exception {
    int ncycles = Integer.parseInt(args[0]);
    StreamBuffer sbuf = new StreamBuffer();
    ObjectOutputStream oout =
        new ObjectOutputStream(sbuf.getOutputStream());
    ObjectInputStream oin =
        new ObjectInputStream(sbuf.getInputStream());
    ObjectStreamClass desc = ObjectStreamClass.lookup(Dummy50.class);

    doReps(oout, oin, sbuf, desc, 1);       // warmup

    long start = System.currentTimeMillis();
    doReps(oout, oin, sbuf, desc, ncycles);
    return System.currentTimeMillis() - start;
}
 
Example 4
Source Project: openjdk-jdk9   Source File: ClassDesc.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Write and read class descriptors to/from a stream.
 * Arguments: <# cycles>
 */
public long run(String[] args) throws Exception {
    int ncycles = Integer.parseInt(args[0]);
    StreamBuffer sbuf = new StreamBuffer();
    ObjectOutputStream oout =
        new ObjectOutputStream(sbuf.getOutputStream());
    ObjectInputStream oin =
        new ObjectInputStream(sbuf.getInputStream());
    ObjectStreamClass desc = ObjectStreamClass.lookup(Dummy50.class);

    doReps(oout, oin, sbuf, desc, 1);       // warmup

    long start = System.currentTimeMillis();
    doReps(oout, oin, sbuf, desc, ncycles);
    return System.currentTimeMillis() - start;
}
 
Example 5
Source Project: jdk8u-jdk   Source File: ProxyClassDesc.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Write and read proxy class descriptors to/from a stream.
 * Arguments: <# cycles>
 */
public long run(String[] args) throws Exception {
    int ncycles = Integer.parseInt(args[0]);
    StreamBuffer sbuf = new StreamBuffer();
    ObjectOutputStream oout =
        new ObjectOutputStream(sbuf.getOutputStream());
    ObjectInputStream oin =
        new ObjectInputStream(sbuf.getInputStream());
    ObjectStreamClass[] descs = genDescs();

    doReps(oout, oin, sbuf, descs, 1);      // warmup

    long start = System.currentTimeMillis();
    doReps(oout, oin, sbuf, descs, ncycles);
    return System.currentTimeMillis() - start;
}
 
Example 6
Source Project: openjdk-jdk9   Source File: ValueType.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the names and types of all the persistent fields of a Class.
 */
private Hashtable getPersistentFields (Class clz) {
    Hashtable result = new Hashtable();
    ObjectStreamClass osc = ObjectStreamClass.lookup(clz);
    if (osc != null) {
        ObjectStreamField[] fields = osc.getFields();
        for (int i = 0; i < fields.length; i++) {
            String typeSig;
            String typePrefix = String.valueOf(fields[i].getTypeCode());
            if (fields[i].isPrimitive()) {
                typeSig = typePrefix;
            } else {
                if (fields[i].getTypeCode() == '[') {
                    typePrefix = "";
                }
                typeSig = typePrefix + fields[i].getType().getName().replace('.','/');
                if (typeSig.endsWith(";")) {
                    typeSig = typeSig.substring(0,typeSig.length()-1);
                }
            }
            result.put(fields[i].getName(),typeSig);
        }
    }
    return result;
}
 
Example 7
public static void main(String[] args) throws Exception {
    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
    ObjectOutputStream output = new ObjectOutputStream(byteOutput);
    output.writeObject(new TestClass());

    ByteArrayInputStream bais = new ByteArrayInputStream(byteOutput.toByteArray());
    TestObjectInputStream input = new TestObjectInputStream(bais);
    input.readObject();

    ObjectStreamClass osc = input.getDescriptor();

    // All OSC public API methods should complete without throwing.
    osc.getName();
    osc.forClass();
    osc.getField("str");
    osc.getFields();
    osc.getSerialVersionUID();
    osc.toString();
}
 
Example 8
Source Project: gadtry   Source File: ObjectInputStreamProxyTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void resolveClass()
        throws IOException, ClassNotFoundException
{
    ObjectStreamClass intClass = ObjectStreamClass.lookupAny(int.class);

    byte[] bytes = Serializables.serialize(1);
    ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(bytes);
    ObjectInputStreamProxy objectInputStream = new ObjectInputStreamProxy(arrayInputStream, ObjectInputStreamProxy.getLatestUserDefinedLoader());

    when(objectStreamClass.getName()).thenReturn("gadtry.gadtry.gadtry.gadtry");
    Assert.assertEquals(int.class, objectInputStream.resolveClass(intClass));

    try {
        objectInputStream.resolveClass(objectStreamClass);
        Assert.fail();
    }
    catch (ClassNotFoundException ignored) {
    }
    //ObjectStreamClass

    Assert.assertEquals(1, (int) objectInputStream.readObject());
}
 
Example 9
Source Project: openjdk-jdk8u   Source File: ValueType.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the names and types of all the persistent fields of a Class.
 */
private Hashtable getPersistentFields (Class clz) {
    Hashtable result = new Hashtable();
    ObjectStreamClass osc = ObjectStreamClass.lookup(clz);
    if (osc != null) {
        ObjectStreamField[] fields = osc.getFields();
        for (int i = 0; i < fields.length; i++) {
            String typeSig;
            String typePrefix = String.valueOf(fields[i].getTypeCode());
            if (fields[i].isPrimitive()) {
                typeSig = typePrefix;
            } else {
                if (fields[i].getTypeCode() == '[') {
                    typePrefix = "";
                }
                typeSig = typePrefix + fields[i].getType().getName().replace('.','/');
                if (typeSig.endsWith(";")) {
                    typeSig = typeSig.substring(0,typeSig.length()-1);
                }
            }
            result.put(fields[i].getName(),typeSig);
        }
    }
    return result;
}
 
Example 10
/**
 * Generate proxy class descriptors.
 */
ObjectStreamClass[] genDescs() {
    ClassLoader ldr = ProxyClassDesc.class.getClassLoader();
    Class[] ifaces = new Class[3];
    Class[] a =
        new Class[] { A1.class, A2.class, A3.class, A4.class, A5.class };
    Class[] b =
        new Class[] { B1.class, B2.class, B3.class, B4.class, B5.class };
    Class[] c =
        new Class[] { C1.class, C2.class, C3.class, C4.class, C5.class };
    ObjectStreamClass[] descs =
        new ObjectStreamClass[a.length * b.length * c.length];
    int n = 0;
    for (int i = 0; i < a.length; i++) {
        ifaces[0] = a[i];
        for (int j = 0; j < b.length; j++) {
            ifaces[1] = b[j];
            for (int k = 0; k < c.length; k++) {
                ifaces[2] = c[k];
                Class proxyClass = Proxy.getProxyClass(ldr, ifaces);
                descs[n++] = ObjectStreamClass.lookup(proxyClass);
            }
        }
    }
    return descs;
}
 
Example 11
Source Project: openjdk-8   Source File: ClassDesc.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Write and read class descriptors to/from a stream.
 * Arguments: <# cycles>
 */
public long run(String[] args) throws Exception {
    int ncycles = Integer.parseInt(args[0]);
    StreamBuffer sbuf = new StreamBuffer();
    ObjectOutputStream oout =
        new ObjectOutputStream(sbuf.getOutputStream());
    ObjectInputStream oin =
        new ObjectInputStream(sbuf.getInputStream());
    ObjectStreamClass desc = ObjectStreamClass.lookup(Dummy50.class);

    doReps(oout, oin, sbuf, desc, 1);       // warmup

    long start = System.currentTimeMillis();
    doReps(oout, oin, sbuf, desc, ncycles);
    return System.currentTimeMillis() - start;
}
 
Example 12
Source Project: jdk8u60   Source File: ClassDesc.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Write and read class descriptors to/from a stream.
 * Arguments: <# cycles>
 */
public long run(String[] args) throws Exception {
    int ncycles = Integer.parseInt(args[0]);
    StreamBuffer sbuf = new StreamBuffer();
    ObjectOutputStream oout =
        new ObjectOutputStream(sbuf.getOutputStream());
    ObjectInputStream oin =
        new ObjectInputStream(sbuf.getInputStream());
    ObjectStreamClass desc = ObjectStreamClass.lookup(Dummy50.class);

    doReps(oout, oin, sbuf, desc, 1);       // warmup

    long start = System.currentTimeMillis();
    doReps(oout, oin, sbuf, desc, ncycles);
    return System.currentTimeMillis() - start;
}
 
Example 13
Source Project: jdk8u60   Source File: ProxyClassDesc.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Write and read proxy class descriptors to/from a stream.
 * Arguments: <# cycles>
 */
public long run(String[] args) throws Exception {
    int ncycles = Integer.parseInt(args[0]);
    StreamBuffer sbuf = new StreamBuffer();
    ObjectOutputStream oout =
        new ObjectOutputStream(sbuf.getOutputStream());
    ObjectInputStream oin =
        new ObjectInputStream(sbuf.getInputStream());
    ObjectStreamClass[] descs = genDescs();

    doReps(oout, oin, sbuf, descs, 1);      // warmup

    long start = System.currentTimeMillis();
    doReps(oout, oin, sbuf, descs, ncycles);
    return System.currentTimeMillis() - start;
}
 
Example 14
Source Project: incubator-retired-blur   Source File: StreamProcessor.java    License: Apache License 2.0 6 votes vote down vote up
public StreamFunction<?> getStreamFunction(String classLoaderId, InputStream inputStream) throws IOException {
  final ClassLoader classLoader = getClassLoader(classLoaderId);
  ObjectInputStream objectInputStream = new ObjectInputStream(inputStream) {
    @Override
    protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
      return classLoader.loadClass(desc.getName());
    }
  };
  try {
    return (StreamFunction<?>) objectInputStream.readObject();
  } catch (ClassNotFoundException e) {
    throw new IOException(e);
  } finally {
    objectInputStream.close();
  }
}
 
Example 15
/**
 * Updates the options that the current classifier is using.
 */
@Override
protected void updateOptions() {

  if (m_Template instanceof OptionHandler) {
    m_ClassifierOptions = Utils.joinOptions(((OptionHandler) m_Template)
        .getOptions());
  } else {
    m_ClassifierOptions = "";
  }
  if (m_Template instanceof Serializable) {
    ObjectStreamClass obs = ObjectStreamClass.lookup(m_Template.getClass());
    m_ClassifierVersion = "" + obs.getSerialVersionUID();
  } else {
    m_ClassifierVersion = "";
  }
}
 
Example 16
Source Project: openjdk-8   Source File: ProxyClassDesc.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Run benchmark for given number of cycles.
 */
void doReps(ObjectOutputStream oout, ObjectInputStream oin,
            StreamBuffer sbuf, ObjectStreamClass[] descs, int ncycles)
    throws Exception
{
    int ndescs = descs.length;
    for (int i = 0; i < ncycles; i++) {
        sbuf.reset();
        oout.reset();
        for (int j = 0; j < ndescs; j++) {
            oout.writeObject(descs[j]);
        }
        oout.flush();
        for (int j = 0; j < ndescs; j++) {
            oin.readObject();
        }
    }
}
 
Example 17
Source Project: TencentKona-8   Source File: ProxyClassDesc.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Write and read proxy class descriptors to/from a stream.
 * Arguments: <# cycles>
 */
public long run(String[] args) throws Exception {
    int ncycles = Integer.parseInt(args[0]);
    StreamBuffer sbuf = new StreamBuffer();
    ObjectOutputStream oout =
        new ObjectOutputStream(sbuf.getOutputStream());
    ObjectInputStream oin =
        new ObjectInputStream(sbuf.getInputStream());
    ObjectStreamClass[] descs = genDescs();

    doReps(oout, oin, sbuf, descs, 1);      // warmup

    long start = System.currentTimeMillis();
    doReps(oout, oin, sbuf, descs, ncycles);
    return System.currentTimeMillis() - start;
}
 
Example 18
public static void main(String[] args) throws Exception {
    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
    ObjectOutputStream output = new ObjectOutputStream(byteOutput);
    output.writeObject(new TestClass());

    ByteArrayInputStream bais = new ByteArrayInputStream(byteOutput.toByteArray());
    TestObjectInputStream input = new TestObjectInputStream(bais);
    input.readObject();

    ObjectStreamClass osc = input.getDescriptor();

    // All OSC public API methods should complete without throwing.
    osc.getName();
    osc.forClass();
    osc.getField("str");
    osc.getFields();
    osc.getSerialVersionUID();
    osc.toString();
}
 
Example 19
Source Project: ehcache3   Source File: CompactJavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
private int getOrAddMapping(ObjectStreamClass desc) {
  SerializableDataKey probe = new SerializableDataKey(desc, false);
  Integer rep = writeLookupCache.get(probe);
  if (rep == null) {
    return addMappingUnderLock(desc, probe);
  } else {
    return rep;
  }
}
 
Example 20
Source Project: Tomcat8-Source-Read   Source File: CustomObjectInputStream.java    License: MIT License 5 votes vote down vote up
/**
 * Load the local class equivalent of the specified stream class
 * description, by using the class loader assigned to this Context.
 *
 * @param classDesc Class description from the input stream
 *
 * @exception ClassNotFoundException if this class cannot be found
 * @exception IOException if an input/output error occurs
 */
@Override
public Class<?> resolveClass(ObjectStreamClass classDesc)
    throws ClassNotFoundException, IOException {

    String name = classDesc.getName();
    if (allowedClassNamePattern != null) {
        boolean allowed = allowedClassNamePattern.matcher(name).matches();
        if (!allowed) {
            boolean doLog = warnOnFailure && reportedClasses.add(name);
            String msg = sm.getString("customObjectInputStream.nomatch", name, allowedClassNameFilter);
            if (doLog) {
                log.warn(msg);
            } else if (log.isDebugEnabled()) {
                log.debug(msg);
            }
            throw new InvalidClassException(msg);
        }
    }

    try {
        return Class.forName(name, false, classLoader);
    } catch (ClassNotFoundException e) {
        try {
            // Try also the superclass because of primitive types
            return super.resolveClass(classDesc);
        } catch (ClassNotFoundException e2) {
            // Rethrow original exception, as it can have more information
            // about why the class was not found. BZ 48007
            throw e;
        }
    }
}
 
Example 21
Source Project: dubbo-2.6.5   Source File: CompactedObjectOutputStream.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
    Class<?> clazz = desc.forClass();
    if (clazz.isPrimitive() || clazz.isArray()) {
        write(0);
        super.writeClassDescriptor(desc);
    } else {
        write(1);
        writeUTF(desc.getName());
    }
}
 
Example 22
Source Project: jdk8u-jdk   Source File: RMIConnector.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Class<?> resolveClass(ObjectStreamClass classDesc)
        throws IOException, ClassNotFoundException {
    String name = classDesc.getName();
    ReflectUtil.checkPackageAccess(name);
    return Class.forName(name, false, loader);
}
 
Example 23
@Override
protected Class<?> resolveClass(ObjectStreamClass aClass)
        throws IOException, ClassNotFoundException {
    if (loader == null) {
        return super.resolveClass(aClass);
    } else {
        String name = aClass.getName();
        ReflectUtil.checkPackageAccess(name);
        // Query the class loader ...
        return Class.forName(name, false, loader);
    }
}
 
Example 24
Source Project: Flink-CEPplus   Source File: InstantiationUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static Map<String, ObjectStreamClass> initMap() {
	final Map<String, ObjectStreamClass> init = new HashMap<>(4);
	for (MigrationUtil m: MigrationUtil.values()) {
		init.put(m.oldSerializerName, m.newSerializerStreamClass);
	}
	return init;
}
 
Example 25
/**
 * Verifies that serializers of anonymous classes can be deserialized, even if serialVersionUID changes.
 */
@Test
public void testAnonymousSerializerClassWithChangedSerialVersionUID() throws Exception {

	TypeSerializer anonymousClassSerializer = new AbstractIntSerializer() {
		@Override
		public TypeSerializerSnapshot<Integer> snapshotConfiguration() {
			return null;
		}
	};
	// assert that our assumption holds
	Assert.assertTrue(anonymousClassSerializer.getClass().isAnonymousClass());

	byte[] anonymousSerializerBytes;
	try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
		TypeSerializerSerializationUtil.writeSerializer(new DataOutputViewStreamWrapper(out), anonymousClassSerializer);
		anonymousSerializerBytes = out.toByteArray();
	}

	long newSerialVersionUID = 1234567L;
	// assert that we're actually modifying to a different serialVersionUID
	Assert.assertNotEquals(ObjectStreamClass.lookup(anonymousClassSerializer.getClass()).getSerialVersionUID(), newSerialVersionUID);
	modifySerialVersionUID(anonymousSerializerBytes, anonymousClassSerializer.getClass().getName(), newSerialVersionUID);

	try (ByteArrayInputStream in = new ByteArrayInputStream(anonymousSerializerBytes)) {
		anonymousClassSerializer = TypeSerializerSerializationUtil.tryReadSerializer(new DataInputViewStreamWrapper(in), Thread.currentThread().getContextClassLoader());
	}

	// serializer should have been deserialized despite serialVersionUID mismatch
	Assert.assertNotNull(anonymousClassSerializer);
	Assert.assertTrue(anonymousClassSerializer.getClass().isAnonymousClass());
}
 
Example 26
@Override
protected Class<?> resolveClass(ObjectStreamClass aClass)
        throws IOException, ClassNotFoundException {
    if (loader == null) {
        return super.resolveClass(aClass);
    } else {
        String name = aClass.getName();
        ReflectUtil.checkPackageAccess(name);
        // Query the class loader ...
        return Class.forName(name, false, loader);
    }
}
 
Example 27
Source Project: ehcache3   Source File: CompactJavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
private static byte[] getSerializedForm(ObjectStreamClass desc) throws IOException {
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  try {
    try (ObjectOutputStream oout = new ObjectOutputStream(bout)) {
      oout.writeObject(desc);
    }
  } finally {
    bout.close();
  }
  return bout.toByteArray();
}
 
Example 28
Source Project: android_9.0.0_r45   Source File: Parcel.java    License: Apache License 2.0 5 votes vote down vote up
private final Serializable readSerializable(final ClassLoader loader) {
    String name = readString();
    if (name == null) {
        // For some reason we were unable to read the name of the Serializable (either there
        // is nothing left in the Parcel to read, or the next value wasn't a String), so
        // return null, which indicates that the name wasn't found in the parcel.
        return null;
    }

    byte[] serializedData = createByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
    try {
        ObjectInputStream ois = new ObjectInputStream(bais) {
            @Override
            protected Class<?> resolveClass(ObjectStreamClass osClass)
                    throws IOException, ClassNotFoundException {
                // try the custom classloader if provided
                if (loader != null) {
                    Class<?> c = Class.forName(osClass.getName(), false, loader);
                    if (c != null) {
                        return c;
                    }
                }
                return super.resolveClass(osClass);
            }
        };
        return (Serializable) ois.readObject();
    } catch (IOException ioe) {
        throw new RuntimeException("Parcelable encountered " +
            "IOException reading a Serializable object (name = " + name +
            ")", ioe);
    } catch (ClassNotFoundException cnfe) {
        throw new RuntimeException("Parcelable encountered " +
            "ClassNotFoundException reading a Serializable object (name = "
            + name + ")", cnfe);
    }
}
 
Example 29
Source Project: sofa-registry   Source File: ServerDataBoxInputStream.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @see ObjectInputStream#resolveClass(ObjectStreamClass)
 */
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException,
                                                       ClassNotFoundException {
    String name = desc.getName();
    try {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        return Class.forName(name, false, classLoader);
    } catch (ClassNotFoundException ex) {
        return super.resolveClass(desc);
    }
}
 
Example 30
private void readObject(
        ObjectInputStream stream)
        throws IOException, ClassNotFoundException {
    stream.defaultReadObject();
    ObjectStreamClass myObject = ObjectStreamClass.lookup(
            Class.forName(Rittner_Agilent7700_RawDataTemplate.class.getCanonicalName()));
    long theSUID = myObject.getSerialVersionUID();
    System.out.println("Customized De-serialization of Rittner_Agilent7700_RawDataTemplate " + theSUID);
}