java.io.ObjectStreamClass Java Examples

The following examples show how to use java.io.ObjectStreamClass. 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 check out the related API usage on the sidebar.
Example #1
Source File: ProxyClassDesc.java    From TencentKona-8 with 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 #2
Source File: ClassDesc.java    From openjdk-jdk8u-backup with 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 #3
Source File: ClassDesc.java    From openjdk-jdk9 with 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 #4
Source File: TestObjectStreamClass.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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 #5
Source File: CacheItem.java    From hibernate4-memcached with 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 #6
Source File: ProxyClassDesc.java    From jdk8u-jdk with 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 #7
Source File: ConfigurableObjectInputStream.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #8
Source File: ValueType.java    From openjdk-jdk9 with 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 #9
Source File: ProxyClassDesc.java    From openjdk-8 with 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 #10
Source File: CollectiveClassifierSplitEvaluator.java    From collective-classification-weka-package with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #11
Source File: StreamProcessor.java    From incubator-retired-blur with 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 #12
Source File: ProxyClassDesc.java    From jdk8u60 with 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 #13
Source File: ClassDesc.java    From jdk8u60 with 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 #14
Source File: TestObjectStreamClass.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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 #15
Source File: ObjectInputStreamProxyTest.java    From gadtry with 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 #16
Source File: ValueType.java    From openjdk-jdk8u with 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 #17
Source File: ClassDesc.java    From openjdk-8 with 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 #18
Source File: ProxyClassDesc.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #19
Source File: PersistentObject.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Recreate a Hashtable from a byte array as created by flatten()
 * 
 * @param flat
 *            the byte array
 * @param resolver
 *            {@link IClassResolver} implementation used for class resolving / mapping
 * @return the original Hashtable or null if no Hashtable could be created from the array
 */
public static Object foldObject(final byte[] flat, IClassResolver resolver){
	if (flat.length == 0) {
		return null;
	}
	try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(flat))) {
		ZipEntry entry = zis.getNextEntry();
		if (entry != null) {
			try (ObjectInputStream ois = new ObjectInputStream(zis) {
				protected java.lang.Class<?> resolveClass(java.io.ObjectStreamClass desc)
					throws IOException, ClassNotFoundException{
					if (resolver != null) {
						Class<?> resolved = resolver.resolveClass(desc);
						return (resolved != null) ? resolved : super.resolveClass(desc);
					} else {
						return super.resolveClass(desc);
					}
				};
			}) {
				return ois.readObject();
			}
		} else {
			return null;
		}
	} catch (Exception ex) {
		log.error("Error unfolding object", ex);
		return null;
	}
}
 
Example #20
Source File: Android17Instantiator.java    From objenesis with Apache License 2.0 5 votes vote down vote up
private static Method getNewInstanceMethod() {
   try {
      Method newInstanceMethod = ObjectStreamClass.class.getDeclaredMethod(
         "newInstance", Class.class, Integer.TYPE);
      newInstanceMethod.setAccessible(true);
      return newInstanceMethod;
   }
   catch(RuntimeException | NoSuchMethodException e) {
      throw new ObjenesisException(e);
   }
}
 
Example #21
Source File: ClassLoaderObjectInputStream.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
    try {
        return loader.loadClass(desc.getName());
    } catch (ClassNotFoundException e) {
        return super.resolveClass(desc);
    }
}
 
Example #22
Source File: SerializationTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSerializeFieldMadeTransient() throws Exception {
    // Does ObjectStreamClass have the right idea?
    ObjectStreamClass osc = ObjectStreamClass.lookup(FieldMadeTransient.class);
    ObjectStreamField[] fields = osc.getFields();
    assertEquals(1, fields.length);
    assertEquals("nonTransientInt", fields[0].getName());
    assertEquals(int.class, fields[0].getType());

    // this was created by serializing a FieldMadeTransient with a non-0 transientInt
    String s = "aced0005737200346c6962636f72652e6a6176612e696f2e53657269616c697a6174696f6e54657"
            + "374244669656c644d6164655472616e7369656e74000000000000000002000149000c7472616e736"
            + "9656e74496e747870abababab";
    FieldMadeTransient deserialized = (FieldMadeTransient) SerializationTester.deserializeHex(s);
    assertEquals(0, deserialized.transientInt);
}
 
Example #23
Source File: ClassTableEntry.java    From WLT3Serial with MIT License 5 votes vote down vote up
public ClassTableEntry(ObjectStreamClass osc, String s) {
	descriptor = osc;
	annotation = s;
	clz = null;
	ccl = null;
	sent = Boolean.parseBoolean(System.getProperty("bort.millipede.wlt3.sent"));
}
 
Example #24
Source File: CompactedObjectOutputStream.java    From dubbox with 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 #25
Source File: ObjectStreamClassTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.io.ObjectStreamClass#lookup(java.lang.Class)
 */
public void test_lookupLjava_lang_Class() {
    ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class);
    assertEquals(
            "lookup returned wrong class: " + osc.getName(),
            "org.apache.harmony.tests.java.io.ObjectStreamClassTest$DummyClass",
            osc.getName());
}
 
Example #26
Source File: CompactedObjectOutputStream.java    From dubbox with 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 #27
Source File: ObjectStreamClassInstantiator.java    From objenesis with Apache License 2.0 5 votes vote down vote up
private static void initialize() {
   if(newInstanceMethod == null) {
      try {
         newInstanceMethod = ObjectStreamClass.class.getDeclaredMethod("newInstance");
         newInstanceMethod.setAccessible(true);
      }
      catch(RuntimeException | NoSuchMethodException e) {
         throw new ObjenesisException(e);
      }
   }
}
 
Example #28
Source File: SUIDGeneratorTest.java    From revapi with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandlingEmptyClass() throws Exception {
    try {
        ObjectStreamClass s = ObjectStreamClass.lookup(Empty.class);
        long officialSUID = s.getSerialVersionUID();
        SUIDGeneratingAnnotationProcessor ap = new SUIDGeneratingAnnotationProcessor();

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

        JavaCompiler.CompilationTask task = compiler
                .getTask(null, null, null, null, Arrays.asList(TestClass.class.getName()),
                        Arrays.asList(new SourceInClassLoader("suid/Empty.java")));

        task.setProcessors(Arrays.asList(ap));

        task.call();

        Assert.assertEquals(officialSUID, ap.generatedSUID);

        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] hashBytes = md.digest("".getBytes("UTF-8"));
        long hash = 0;
        for (int i = Math.min(hashBytes.length, 8) - 1; i >= 0; i--) {
            hash = (hash << 8) | (hashBytes[i] & 0xFF);
        }

        Assert.assertEquals(hash, ap.generatedStructuralId);
    } finally {
        new File("Empty.class").delete();
    }
}
 
Example #29
Source File: CompactJavaSerializer.java    From ehcache3 with 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 #30
Source File: SerializableType.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected ObjectInputStream createObjectInputStream(InputStream is) throws IOException {
    return new ObjectInputStream(is) {
        @Override
        protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
            return ReflectUtil.loadClass(desc.getName());
        }
    };
}