Java Code Examples for java.io.ObjectStreamClass#lookup()

The following examples show how to use java.io.ObjectStreamClass#lookup() . 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: ObjectStreamClassTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.io.ObjectStreamClass#lookupAny(java.lang.Class)
 * @since 1.6
 */
public void test_lookupAnyLjava_lang_Class() {
    // Test for method java.io.ObjectStreamClass
    // java.io.ObjectStreamClass.lookupAny(java.lang.Class)
    ObjectStreamClass osc = ObjectStreamClass.lookupAny(DummyClass.class);
    assertEquals("lookup returned wrong class: " + osc.getName(),
            "org.apache.harmony.tests.java.io.ObjectStreamClassTest$DummyClass", osc
            .getName());

    osc = ObjectStreamClass.lookupAny(NonSerialzableClass.class);
    assertEquals("lookup returned wrong class: " + osc.getName(),
            "org.apache.harmony.tests.java.io.ObjectStreamClassTest$NonSerialzableClass",
            osc.getName());

    osc = ObjectStreamClass.lookupAny(ExternalizableClass.class);
    assertEquals("lookup returned wrong class: " + osc.getName(),
            "org.apache.harmony.tests.java.io.ObjectStreamClassTest$ExternalizableClass",
            osc.getName());

    osc = ObjectStreamClass.lookup(NonSerialzableClass.class);
    assertNull(osc);

}
 
Example 2
Source File: ValueType.java    From hottub 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 3
Source File: ProxyClassDesc.java    From openjdk-jdk8u 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 4
Source File: ClassDesc.java    From openjdk-jdk8u 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 5
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 6
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 7
Source File: ProxyClassDesc.java    From TencentKona-8 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 8
Source File: ClassDesc.java    From TencentKona-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 9
Source File: ProxyClassDesc.java    From jdk8u-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 10
Source File: WPCustomObjectInputStream.java    From WorldPainter with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
    ObjectStreamClass resultClassDescriptor = super.readClassDescriptor();
    Class localClass = resolveClass(resultClassDescriptor);
    if (patchClasses.contains(localClass)) {
        ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);
        if (localClassDescriptor != null) {
            final long localSUID = localClassDescriptor.getSerialVersionUID();
            final long streamSUID = resultClassDescriptor.getSerialVersionUID();
            if (streamSUID != localSUID) {
                logger.warn("Overriding serialized class version mismatch: local serialVersionUID = " + localSUID + " stream serialVersionUID = " + streamSUID);
                resultClassDescriptor = localClassDescriptor;
            }
        }
    } else if (localClass == null) {
        logger.warn("No local class for " + resultClassDescriptor.getName());
    }
    return resultClassDescriptor;
}
 
Example 11
Source File: SerialVer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static String resolveClass(String classname) throws ClassNotFoundException {
    Class<?> cl = Class.forName(classname, false, loader);
    ObjectStreamClass desc = ObjectStreamClass.lookup(cl);
    if (desc != null) {
        return "    private static final long serialVersionUID = " +
            desc.getSerialVersionUID() + "L;";
    } else {
        return null;
    }
}
 
Example 12
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 13
Source File: EnhancedObjectInputStreamTest.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Test
void whitelist() throws Exception {
    final byte[] serializationHeader = new byte[] { (byte) (((short) 0xaced >>> 8) & 0xFF),
            (byte) (((short) 0xaced) & 0xFF), (byte) (((short) 5 >>> 8) & 0xFF), (byte) (((short) 5) & 0xFF) };
    final ObjectStreamClass desc = ObjectStreamClass.lookup(Ser.class);
    assertNotNull(new EnhancedObjectInputStream(new ByteArrayInputStream(serializationHeader),
            Thread.currentThread().getContextClassLoader(), name -> name
                    .equals("org.talend.sdk.component.runtime.serialization.EnhancedObjectInputStreamTest$Ser")) {
    }.resolveClass(desc));
    assertThrows(SecurityException.class, () -> new EnhancedObjectInputStream(
            new ByteArrayInputStream(serializationHeader), Thread.currentThread().getContextClassLoader(),
            name -> name.equals("org.talend.sdk.component.runtime.serialization.EnhancedObjectInputStream")) {
    }.resolveClass(desc));
}
 
Example 14
Source File: ObjectStreamClassTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.io.ObjectStreamClass#getName()
 */
public void test_getName() {
    ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class);
    assertEquals(
            "getName returned incorrect name: " + osc.getName(),
            "org.apache.harmony.tests.java.io.ObjectStreamClassTest$DummyClass",
            osc.getName());
}
 
Example 15
Source File: SerialVer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static String resolveClass(String classname) throws ClassNotFoundException {
    Class<?> cl = Class.forName(classname, false, loader);
    ObjectStreamClass desc = ObjectStreamClass.lookup(cl);
    if (desc != null) {
        return "    private static final long serialVersionUID = " +
            desc.getSerialVersionUID() + "L;";
    } else {
        return null;
    }
}
 
Example 16
Source File: OldObjectStreamFieldTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
protected void setUp() {
    osc = ObjectStreamClass.lookup(DummyClass.class);
    bamField = osc.getField("bam");
    samField = osc.getField("sam");
    hamField = osc.getField("ham");
    holaField = osc.getField("hola");
}
 
Example 17
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 18
Source File: InstantiationUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
	ObjectStreamClass streamClassDescriptor = super.readClassDescriptor();

	try {
		Class.forName(streamClassDescriptor.getName(), false, classLoader);
	} catch (ClassNotFoundException e) {

		final ObjectStreamClass equivalentSerializer =
				MigrationUtil.getEquivalentSerializer(streamClassDescriptor.getName());

		if (equivalentSerializer != null) {
			return equivalentSerializer;
		}
	}

	final Class localClass = resolveClass(streamClassDescriptor);
	final String name = localClass.getName();
	if (scalaSerializerClassnames.contains(name) || scalaTypes.contains(name) || isAnonymousClass(localClass)
		|| isOldAvroSerializer(name, streamClassDescriptor.getSerialVersionUID())) {
		final ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);
		if (localClassDescriptor != null
			&& localClassDescriptor.getSerialVersionUID() != streamClassDescriptor.getSerialVersionUID()) {
			LOG.warn("Ignoring serialVersionUID mismatch for class {}; was {}, now {}.",
				streamClassDescriptor.getName(), streamClassDescriptor.getSerialVersionUID(), localClassDescriptor.getSerialVersionUID());

			streamClassDescriptor = localClassDescriptor;
		}
	}

	return streamClassDescriptor;
}
 
Example 19
Source File: SerialVer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static String resolveClass(String classname) throws ClassNotFoundException {
    Class<?> cl = Class.forName(classname, false, loader);
    ObjectStreamClass desc = ObjectStreamClass.lookup(cl);
    if (desc != null) {
        return "    private static final long serialVersionUID = " +
            desc.getSerialVersionUID() + "L;";
    } else {
        return null;
    }
}
 
Example 20
Source File: ClassConfigurator.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public void init() throws ChannelException {
    //populate the map
    try {
        /* GemStoneAddition - we read from a canned text file, not xml

        // make sure we have a class for DocumentBuilderFactory
        // getClass().getClassLoader().loadClass("javax.xml.parsers.DocumentBuilderFactory");
        Util.loadClass("javax.xml.parsers.DocumentBuilderFactory", this.getClass());

        MagicNumberReader reader=new MagicNumberReader();
        
        // PropertyPermission not granted if running in an untrusted environment with JNLP.
        try {
            String mnfile = System.getProperty("com.gemstone.org.jgroups.conf.magicNumberFile");
            if(mnfile != null) {
                if(log.isDebugEnabled()) log.debug("Using " + mnfile + " as magic number file");
                reader.setFilename(mnfile);
            }
        }
        catch (SecurityException ex){
        }

        ClassMap[] mapping=reader.readMagicNumberMapping();
        */
        ObjectStreamClass objStreamClass; // GemStoneAddition - moved from above previous line
        ClassMap[] mapping = MagicNumberReader.readMagicNumberMappingFromText(); // GemStoneAddition
        
        if(mapping != null) {
            Integer m;
            ClassMap currentMap = null;
            for(int i=0; i < mapping.length; i++) {
                currentMap = mapping[i];
                m=Integer.valueOf(currentMap.getMagicNumber());
                try {
                    Class clazz=currentMap.getClassForMap();
                    objStreamClass=ObjectStreamClass.lookup(clazz);
                    if(objStreamClass == null)
                        throw new ChannelException("ObjectStreamClass for " + clazz + " not found");
                    if(magicMap.containsKey(m)) {
                        throw new ChannelException("magic key " + m + " (" + clazz.getName() + ')' +
                                                   " is already in map; please make sure that " +
                                                   "all magic keys are unique");
                    }
                    else {
                        magicMap.put(m, clazz);
                        classMap.put(clazz, m);

                        streamMapId.put(m, objStreamClass);
                        streamMapClass.put(objStreamClass, m);
                    }
                }
                catch(ClassNotFoundException cnf) {
                    throw new ChannelException("failed loading class " 
                        + currentMap.getClassName(), cnf);
                }
            }
            //if(log.isDebugEnabled()) log.debug("mapping is:\n" + printMagicMap());
        }
    }
    catch(ChannelException ex) {
        throw ex;
    }
    catch(RuntimeException x) {
        // if(log.isErrorEnabled()) log.error("failed reading the magic number mapping file, reason: " + Util.print(x));
        throw new ChannelException("failed reading the magic number mapping file", x);
    }
}