java.io.ObjectStreamField Java Examples

The following examples show how to use java.io.ObjectStreamField. 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 Project: TencentKona-8   Author: Tencent   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 #2
Source Project: jdk8u60   Author: chenghanpeng   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 #3
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   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 #4
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   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 #5
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   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 #6
Source Project: hottub   Author: dsrg-uoft   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
Source Project: openjdk-8-source   Author: keerath   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 #8
Source Project: openjdk-8   Author: bpupadhyaya   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 #9
Source Project: j2objc   Author: google   File: ObjectStreamClassTest.java    License: Apache License 2.0 6 votes vote down vote up
public void test_specialTypes() {
    Class<?> proxyClass = Proxy.getProxyClass(this.getClass()
            .getClassLoader(), new Class[] { Runnable.class });

    ObjectStreamClass proxyStreamClass = ObjectStreamClass
            .lookup(proxyClass);

    assertEquals("Proxy classes should have zero serialVersionUID", 0,
            proxyStreamClass.getSerialVersionUID());
    ObjectStreamField[] proxyFields = proxyStreamClass.getFields();
    assertEquals("Proxy classes should have no serialized fields", 0,
            proxyFields.length);

    ObjectStreamClass enumStreamClass = ObjectStreamClass
            .lookup(Thread.State.class);

    assertEquals("Enum classes should have zero serialVersionUID", 0,
            enumStreamClass.getSerialVersionUID());
    ObjectStreamField[] enumFields = enumStreamClass.getFields();
    assertEquals("Enum classes should have no serialized fields", 0,
            enumFields.length);
}
 
Example #10
Source Project: j2objc   Author: google   File: ObjectStreamFieldTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * java.io.ObjectStreamField#getType()
 */
public void test_getType_Deserialized() throws IOException,
        ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(new SerializableObject());
    oos.close();
    baos.close();

    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    SerializableObject obj = (SerializableObject) ois.readObject();

    ObjectStreamClass oc = obj.getObjectStreamClass();
    ObjectStreamField field = oc.getField("i");
    assertEquals(Object.class, field.getType());
}
 
Example #11
Source Project: j2objc   Author: google   File: ObjectStreamFieldTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * java.io.ObjectStreamField#getType()
 */
public void test_getType_MockObjectInputStream() throws IOException,
        ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(new SerializableObject());
    oos.close();
    baos.close();

    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    MockObjectInputStream ois = new MockObjectInputStream(bais);
    ois.readObject();

    ObjectStreamClass oc = ois.getObjectStreamClass();
    ObjectStreamField field = oc.getField("i");
    assertEquals(Object.class, field.getType());
}
 
Example #12
Source Project: Learn-Java-12-Programming   Author: PacktPublishing   File: SerializableDemo.java    License: MIT License 5 votes vote down vote up
private static void printInfo(ObjectStreamClass osc) {
    System.out.println(osc.forClass());
    System.out.println("Class name: " + osc.getName());
    System.out.println("SerialVersionUID: " + osc.getSerialVersionUID());
    ObjectStreamField[] fields = osc.getFields();
    System.out.println("Serialized fields:");
    for (ObjectStreamField osf : fields) {
        System.out.println(osf.getName() + ": ");
        System.out.println("\t" + osf.getType());
        System.out.println("\t" + osf.getTypeCode());
        System.out.println("\t" + osf.getTypeString());
        System.out.println("\t" + osf.getOffset());
    }
}
 
Example #13
Source Project: j2objc   Author: google   File: ObjectStreamClassTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * java.io.ObjectStreamClass#getFields()
 */
public void test_getFields() {
    ObjectStreamClass osc = ObjectStreamClass.lookup(DummyClass.class);
    ObjectStreamField[] osfArray = osc.getFields();
    assertTrue(
            "Array of fields should be of length 2 but is instead of length: "
                    + osfArray.length, osfArray.length == 2);
}
 
Example #14
Source Project: j2objc   Author: google   File: ObjectStreamFieldTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * java.io.ObjectStreamField#getOffset()
 */
public void test_getOffset() {
    ObjectStreamField[] osfArray;
    osfArray = osc.getFields();
    assertTrue("getOffset did not return reasonable values", osfArray[0]
            .getOffset() != osfArray[1].getOffset());
    assertEquals("getOffset for osfArray[0].getOffset() did not return 0",
            0, osfArray[0].getOffset());
    assertEquals("osfArray[1].getOffset() did not return	8", 8, osfArray[1]
            .getOffset());
    assertEquals("osfArray[2].getOffset() did not return 12", 12,
            osfArray[2].getOffset());
}
 
Example #15
Source Project: j2objc   Author: google   File: ObjectStreamFieldTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * java.io.ObjectStreamField#getTypeString()
 */
public void test_getTypeString() {
    assertTrue("getTypeString returned: " + holaField.getTypeString(),
            holaField.getTypeString().indexOf("Object") >= 0);
    assertNull("Primitive types' strings should be null", hamField
            .getTypeString());

    ObjectStreamField osf = new ObjectStreamField("s", String.class, true);
    assertTrue(osf.getTypeString() == "Ljava/lang/String;");
}
 
Example #16
Source Project: j2objc   Author: google   File: SerializationTest.java    License: 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 #17
Source Project: j2objc   Author: google   File: OldObjectStreamFieldTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_getOffset() {
    ObjectStreamField[] osfArray;
    osfArray = osc.getFields();
    int[] expectedOffsets = {0, 1, 9, 11, 19, 23, 27, 31, 39, 41, 0};

    assertTrue("getOffset() did not return reasonable values.", osfArray[0]
            .getOffset() != osfArray[1].getOffset());

    for (int i = 0; i < expectedOffsets.length; i++) {
        assertEquals(String.format("Unexpected value for osfArray[%d].getOffset(): ", i),
                expectedOffsets[i], osfArray[i].getOffset());

    }
}
 
Example #18
Source Project: j2objc   Author: google   File: OldObjectStreamFieldTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_isPrimitive() {
    // Test for method int java.io.ObjectStreamField.getOffset()
    ObjectStreamField[] osfArray;
    osfArray = osc.getFields();

    for (int i = 0; i < (osfArray.length - 1); i++) {
        assertTrue(String.format("osfArray[%d].isPrimitive() should return true.", i),
                   osfArray[i].isPrimitive());
    }
    assertFalse(String.format("osfArray[%d].isPrimitive() should return false.",
                              osfArray.length - 1),
                osfArray[(osfArray.length - 1)].isPrimitive());
}
 
Example #19
Source Project: lams   Author: lamsfoundation   File: SerializableConverter.java    License: GNU General Public License v2.0 4 votes vote down vote up
private Object readField(ObjectStreamField field, Class type, Object instance) {
    Field javaField = Fields.find(type, field.getName());
    return Fields.read(javaField, instance);
}