Search in sources :

Example 6 with ObjectStreamField

use of java.io.ObjectStreamField in project wicket by apache.

the class CheckingObjectOutputStream method checkFields.

private void checkFields(Object obj, ObjectStreamClass desc) {
    int numFields;
    try {
        numFields = (Integer) GET_NUM_OBJ_FIELDS_METHOD.invoke(desc, (Object[]) null);
    } catch (IllegalAccessException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
    if (numFields > 0) {
        int numPrimFields;
        ObjectStreamField[] fields = desc.getFields();
        Object[] objVals = new Object[numFields];
        numPrimFields = fields.length - objVals.length;
        try {
            GET_OBJ_FIELD_VALUES_METHOD.invoke(desc, obj, objVals);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < objVals.length; i++) {
            if (objVals[i] instanceof String || objVals[i] instanceof Number || objVals[i] instanceof Date || objVals[i] instanceof Boolean || objVals[i] instanceof Class) {
                // filter out common cases
                continue;
            }
            // Check for circular reference.
            if (checked.containsKey(objVals[i])) {
                continue;
            }
            ObjectStreamField fieldDesc = fields[numPrimFields + i];
            Field field;
            try {
                field = (Field) GET_FIELD_METHOD.invoke(fieldDesc, (Object[]) null);
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
            simpleName = field.getName();
            fieldDescription = field.toString();
            check(objVals[i]);
        }
    }
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) Date(java.util.Date) Field(java.lang.reflect.Field) ObjectStreamField(java.io.ObjectStreamField) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ObjectStreamField(java.io.ObjectStreamField) ObjectStreamClass(java.io.ObjectStreamClass)

Example 7 with ObjectStreamField

use of java.io.ObjectStreamField in project j2objc by google.

the class ObjectStreamClassTest method test_specialTypes.

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);
}
Also used : ObjectStreamField(java.io.ObjectStreamField) ObjectStreamClass(java.io.ObjectStreamClass)

Example 8 with ObjectStreamField

use of java.io.ObjectStreamField in project j2objc by google.

the class MyObjectInputStream method test_getType_Deserialized.

/**
 * 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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamField(java.io.ObjectStreamField) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectStreamClass(java.io.ObjectStreamClass) ObjectInputStream(java.io.ObjectInputStream)

Example 9 with ObjectStreamField

use of java.io.ObjectStreamField in project j2objc by google.

the class OldObjectStreamFieldTest method test_ConstructorLjava_lang_StringLjava_lang_ClassB.

public void test_ConstructorLjava_lang_StringLjava_lang_ClassB() {
    ObjectStreamField osf = new ObjectStreamField("aField", int.class, false);
    assertTrue("Test 1: Name member not set correctly.", osf.getName().equals("aField"));
    assertTrue("Test 2: Type member not set correctly.", osf.getType().equals(int.class));
    assertFalse("Test 3: Unshared member not set correctly.", osf.isUnshared());
    // Repeat the tests with a different object to make sure
    // that we have not tested against default values.
    osf = new ObjectStreamField("anotherField", String.class, true);
    assertTrue("Test 4: Name member not set correctly.", osf.getName().equals("anotherField"));
    assertTrue("Test 5: Type member not set correctly.", osf.getType().equals(String.class));
    assertTrue("Test 6: Unshared member not set correctly.", osf.isUnshared());
    // Invalid argument tests.
    try {
        osf = new ObjectStreamField(null, boolean.class);
        fail("Test 7: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected.
    }
    try {
        osf = new ObjectStreamField("thisField", null);
        fail("Test 8: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected.
    }
}
Also used : ObjectStreamField(java.io.ObjectStreamField)

Example 10 with ObjectStreamField

use of java.io.ObjectStreamField in project robovm by robovm.

the class OldObjectStreamFieldTest method test_ConstructorLjava_lang_StringLjava_lang_Class.

public void test_ConstructorLjava_lang_StringLjava_lang_Class() {
    ObjectStreamField osf = new ObjectStreamField("aField", int.class);
    assertTrue("Test 1: Name member not set correctly.", osf.getName().equals("aField"));
    assertTrue("Test 2: Type member not set correctly.", osf.getType().equals(int.class));
    // Repeat the tests with a different object to make sure
    // that we have not tested against default values.
    osf = new ObjectStreamField("anotherField", String.class);
    assertTrue("Test 3: Name member not set correctly.", osf.getName().equals("anotherField"));
    assertTrue("Test 4: Type member not set correctly.", osf.getType().equals(String.class));
    // Invalid argument tests.
    try {
        osf = new ObjectStreamField(null, boolean.class);
        fail("Test 5: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected.
    }
    try {
        osf = new ObjectStreamField("thisField", null);
        fail("Test 6: NullPointerException expected.");
    } catch (NullPointerException e) {
    // Expected.
    }
}
Also used : ObjectStreamField(java.io.ObjectStreamField)

Aggregations

ObjectStreamField (java.io.ObjectStreamField)19 ObjectStreamClass (java.io.ObjectStreamClass)7 Field (java.lang.reflect.Field)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Externalizable (java.io.Externalizable)1 ObjectInputStream (java.io.ObjectInputStream)1 Serializable (java.io.Serializable)1 Method (java.lang.reflect.Method)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1