Merge pull request #932, hessian bugfix.
Hessian bugfix, revert the override sequence of hessian, field of subclass should override that of parent class, not the other way around.
This commit is contained in:
parent
6551fa3607
commit
6f7a146faf
|
|
@ -53,6 +53,8 @@ import java.lang.reflect.Field;
|
|||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
|
@ -77,8 +79,8 @@ public class JavaSerializer extends AbstractSerializer {
|
|||
if (_writeReplace != null)
|
||||
_writeReplace.setAccessible(true);
|
||||
|
||||
ArrayList primitiveFields = new ArrayList();
|
||||
ArrayList compoundFields = new ArrayList();
|
||||
List primitiveFields = new ArrayList();
|
||||
List compoundFields = new ArrayList();
|
||||
|
||||
for (; cl != null; cl = cl.getSuperclass()) {
|
||||
Field[] fields = cl.getDeclaredFields();
|
||||
|
|
@ -101,9 +103,10 @@ public class JavaSerializer extends AbstractSerializer {
|
|||
}
|
||||
}
|
||||
|
||||
ArrayList fields = new ArrayList();
|
||||
List fields = new ArrayList();
|
||||
fields.addAll(primitiveFields);
|
||||
fields.addAll(compoundFields);
|
||||
Collections.reverse(fields);
|
||||
|
||||
_fields = new Field[fields.size()];
|
||||
fields.toArray(_fields);
|
||||
|
|
|
|||
|
|
@ -17,33 +17,24 @@
|
|||
|
||||
package com.alibaba.com.caucho.hessian.io;
|
||||
|
||||
import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
|
||||
import com.alibaba.com.caucho.hessian.io.beans.Type;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.EnumSet;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
public class Hessian2EnumSetTest {
|
||||
public class Hessian2EnumSetTest extends SerializeTestBase {
|
||||
|
||||
@Test
|
||||
public void singleton() throws Exception {
|
||||
EnumSet h = EnumSet.of(Type.High);
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
Hessian2Output out = new Hessian2Output(bout);
|
||||
|
||||
out.writeObject(h);
|
||||
out.flush();
|
||||
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||
Hessian2Input input = new Hessian2Input(bin);
|
||||
EnumSet set = (EnumSet) input.readObject();
|
||||
|
||||
EnumSet set = baseHession2Serialize(h);
|
||||
assertTrue(Arrays.asList(set.toArray()).contains(Type.High));
|
||||
assertFalse(Arrays.asList(set.toArray()).contains(Type.Lower));
|
||||
}
|
||||
|
|
@ -51,17 +42,7 @@ public class Hessian2EnumSetTest {
|
|||
@Test
|
||||
public void set() throws Exception {
|
||||
EnumSet<Type> types = EnumSet.of(Type.High, Type.Lower);
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
Hessian2Output out = new Hessian2Output(bout);
|
||||
|
||||
out.writeObject(types);
|
||||
out.flush();
|
||||
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||
Hessian2Input input = new Hessian2Input(bin);
|
||||
|
||||
EnumSet set = (EnumSet) input.readObject();
|
||||
EnumSet set = baseHession2Serialize(types);
|
||||
assertTrue(set.contains(Type.High));
|
||||
assertFalse(set.contains(Type.Normal));
|
||||
}
|
||||
|
|
@ -69,17 +50,7 @@ public class Hessian2EnumSetTest {
|
|||
@Test
|
||||
public void none() throws Exception {
|
||||
EnumSet<Type> types = EnumSet.noneOf(Type.class);
|
||||
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
Hessian2Output out = new Hessian2Output(bout);
|
||||
|
||||
out.writeObject(types);
|
||||
out.flush();
|
||||
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||
Hessian2Input input = new Hessian2Input(bin);
|
||||
|
||||
EnumSet set = (EnumSet) input.readObject();
|
||||
EnumSet set = baseHession2Serialize(types);
|
||||
TestCase.assertEquals(set, EnumSet.noneOf(Type.class));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
package com.alibaba.com.caucho.hessian.io;
|
||||
|
||||
import com.alibaba.com.caucho.hessian.io.base.SerializeTestBase;
|
||||
import com.alibaba.com.caucho.hessian.io.beans.BaseUser;
|
||||
import com.alibaba.com.caucho.hessian.io.beans.GrandsonUser;
|
||||
import com.alibaba.com.caucho.hessian.io.beans.SubUser;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* fix hession serialize bug:
|
||||
* the filed of parent class will cover the filed of sub class
|
||||
*
|
||||
*/
|
||||
public class HessianJavaSerializeTest extends SerializeTestBase {
|
||||
|
||||
@Test
|
||||
public void testGetBaseUserName() throws Exception {
|
||||
|
||||
BaseUser baseUser = new BaseUser();
|
||||
baseUser.setUserId(1);
|
||||
baseUser.setUserName("tom");
|
||||
|
||||
BaseUser serializedUser = baseHessionSerialize(baseUser);
|
||||
Assert.assertEquals("tom", serializedUser.getUserName());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetSubUserName() throws Exception {
|
||||
SubUser subUser = new SubUser();
|
||||
subUser.setUserId(1);
|
||||
subUser.setUserName("tom");
|
||||
|
||||
SubUser serializedUser = baseHessionSerialize(subUser);
|
||||
Assert.assertEquals("tom", serializedUser.getUserName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetGrandsonUserName() throws Exception {
|
||||
GrandsonUser grandsonUser = new GrandsonUser();
|
||||
grandsonUser.setUserId(1);
|
||||
grandsonUser.setUserName("tom");
|
||||
|
||||
GrandsonUser serializedUser = baseHessionSerialize(grandsonUser);
|
||||
Assert.assertEquals("tom", serializedUser.getUserName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,56 @@
|
|||
package com.alibaba.com.caucho.hessian.io.base;
|
||||
|
||||
import com.alibaba.com.caucho.hessian.io.Hessian2Input;
|
||||
import com.alibaba.com.caucho.hessian.io.Hessian2Output;
|
||||
import com.alibaba.com.caucho.hessian.io.HessianInput;
|
||||
import com.alibaba.com.caucho.hessian.io.HessianOutput;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* hession base serialize utils
|
||||
*
|
||||
*/
|
||||
public class SerializeTestBase {
|
||||
/**
|
||||
* hession serialize util
|
||||
*
|
||||
* @param data
|
||||
* @param <T>
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
protected <T> T baseHessionSerialize(T data) throws IOException {
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
HessianOutput out = new HessianOutput(bout);
|
||||
|
||||
out.writeObject(data);
|
||||
out.flush();
|
||||
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||
HessianInput input = new HessianInput(bin);
|
||||
return (T) input.readObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* hession2 serialize util
|
||||
*
|
||||
* @param data
|
||||
* @param <T>
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
protected <T> T baseHession2Serialize(T data) throws IOException {
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
Hessian2Output out = new Hessian2Output(bout);
|
||||
|
||||
out.writeObject(data);
|
||||
out.flush();
|
||||
|
||||
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
|
||||
Hessian2Input input = new Hessian2Input(bin);
|
||||
return (T) input.readObject();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.alibaba.com.caucho.hessian.io.beans;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author WangXin
|
||||
*/
|
||||
public class BaseUser implements Serializable {
|
||||
private static final long serialVersionUID = 9104092580669691633L;
|
||||
private Integer userId;
|
||||
private String userName;
|
||||
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.alibaba.com.caucho.hessian.io.beans;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author WangXin
|
||||
*/
|
||||
public class GrandsonUser extends SubUser implements Serializable {
|
||||
private static final long serialVersionUID = 5013145666993778451L;
|
||||
private String userName;
|
||||
|
||||
@Override
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
@Override
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.alibaba.com.caucho.hessian.io.beans;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class SubUser extends BaseUser implements Serializable {
|
||||
private static final long serialVersionUID = 4017613093053853415L;
|
||||
private String userName;
|
||||
|
||||
@Override
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
@Override
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
||||
|
|
@ -15,8 +15,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.com.caucho.hessian.io;
|
||||
|
||||
package com.alibaba.com.caucho.hessian.io.beans;
|
||||
public enum Type {
|
||||
High, Normal, Lower
|
||||
}
|
||||
Loading…
Reference in New Issue