Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherited fields are not serialized. #8

Closed
Toilal opened this issue Feb 16, 2012 · 2 comments
Closed

Inherited fields are not serialized. #8

Toilal opened this issue Feb 16, 2012 · 2 comments

Comments

@Toilal
Copy link
Contributor

Toilal commented Feb 16, 2012

@see http://pastebin.com/RuXFsxD1 (Base class TestBean2.java)
@see http://pastebin.com/Yb2Hd1Zu (Derived class DerivedBean.java)
@see http://pastebin.com/EKuHNiTi (Unit test)

Unit test doesn't pass, because fields from TestBean2 base class are null. JDBM File doesn't contains inherited field definitions.

@Toilal
Copy link
Contributor Author

Toilal commented Feb 16, 2012

I have fixed this issue.

    ObjectStreamClass streamClass = ObjectStreamClass.lookup(clazz);
    ObjectStreamField[] fields = streamClass.getFields()

This lookup grab information for clazz, but fields doesn't contains fields from superclass. Here is a code to use instead.

private ObjectStreamField[] getFields(Class clazz) {
    ObjectStreamClass streamClass = ObjectStreamClass.lookup(clazz);
    FastArrayList<ObjectStreamField> fieldsList = new FastArrayList<ObjectStreamField>();
    while (streamClass != null) {
        for (ObjectStreamField f : streamClass.getFields()) {
            fieldsList.add(f);
        }
        clazz = clazz.getSuperclass();
        streamClass = ObjectStreamClass.lookup(clazz);
    }
    ObjectStreamField[] fields = new ObjectStreamField[fieldsList.size()];
    for (int i = 0; i < fields.length; i++) {
        fields[i] = fieldsList.get(i);
    }
    return fields;
}

This method should be called by SerialClassInfo class, in registerClass(Class) and writeObject(DataOutput, Object, FastArrayList) methods.

Then the unit test pass properly.

@jankotek
Copy link
Owner

I used your fix, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants