Skip to content

Commit

Permalink
fix meta share
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Jan 10, 2024
1 parent 0e85316 commit f717a82
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,24 +514,20 @@ public List<Class<?>> getRegisteredClasses() {
* non-final to write class def, so that it can be deserialized by the peer still..
*/
public boolean isMonomorphic(Class<?> clz) {
if (clz.isArray()) {
return isMonomorphic(clz.getComponentType());
}
if (ReflectionUtils.isMonomorphic(clz)) {
if (fury.getConfig().shareMetaContext()) {
boolean isInnerClass = isInnerClass(clz);
if (!isInnerClass) {
return false;
} else {
// can't create final map/collection type using TypeUtils.mapOf(TypeToken<K>,
// TypeToken<V>)
return !Map.class.isAssignableFrom(clz) && !Collection.class.isAssignableFrom(clz);
}
if (fury.getConfig().shareMetaContext()) {
if (!ReflectionUtils.isMonomorphic(clz)) {
return false;
}
boolean isInnerClass = isInnerClass(clz);
if (!isInnerClass) {
return false;
} else {
return true;
// can't create final map/collection type using TypeUtils.mapOf(TypeToken<K>,
// TypeToken<V>)
return !Map.class.isAssignableFrom(clz) && !Collection.class.isAssignableFrom(clz);
}
}
return false;
return ReflectionUtils.isMonomorphic(clz);
}

/** Returns true if <code>cls</code> is fury inner registered class. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,71 @@ public void testWriteCompatibleBasic(
}
}

@Test
public void testWriteCompatibleCollectionSimple() throws Exception {
BeanA beanA = BeanA.createBeanA(2);
String pkg = BeanA.class.getPackage().getName();
String code =
""
+ "package "
+ pkg
+ ";\n"
+ "import java.util.*;\n"
+ "import java.math.*;\n"
+ "public class BeanA {\n"
+ " private List<Double> doubleList;\n"
+ " private Iterable<BeanB> beanBIterable;\n"
+ " private List<BeanB> beanBList;\n"
+ "}";
Class<?> cls1 =
loadClass(
BeanA.class,
code,
MetaSharedCompatibleTest.class + "testWriteCompatibleCollectionBasic_1");
Fury fury1 =
Fury.builder()
.withCodegen(false)
.withMetaContextShare(true)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.requireClassRegistration(false)
.withClassLoader(cls1.getClassLoader())
.build();
code =
""
+ "package "
+ pkg
+ ";\n"
+ "import java.util.*;\n"
+ "import java.math.*;\n"
+ "public class BeanA {\n"
+ " private List<Double> doubleList;\n"
+ " private Iterable<BeanB> beanBIterable;\n"
+ "}";
Class<?> cls2 =
loadClass(
BeanA.class,
code,
MetaSharedCompatibleTest.class + "testWriteCompatibleCollectionBasic_2");
Object o2 = cls2.newInstance();
ReflectionUtils.unsafeCopy(beanA, o2);
Fury fury2 =
Fury.builder()
.withCodegen(false)
.withMetaContextShare(true)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.requireClassRegistration(false)
.withClassLoader(cls2.getClassLoader())
.build();

MetaContext context1 = new MetaContext();
MetaContext context2 = new MetaContext();
fury1.getSerializationContext().setMetaContext(context1);
byte[] objBytes = fury1.serialize(beanA);
fury2.getSerializationContext().setMetaContext(context2);
Object obj2 = fury2.deserialize(objBytes);
Assert.assertTrue(ReflectionUtils.objectCommonFieldsEquals(obj2, o2));
}

@Test(dataProvider = "config3")
public void testWriteCompatibleCollectionBasic(
boolean referenceTracking,
Expand All @@ -195,16 +260,6 @@ public void testWriteCompatibleCollectionBasic(
boolean enableCodegen3)
throws Exception {
BeanA beanA = BeanA.createBeanA(2);
Fury fury =
Fury.builder()
.withLanguage(Language.JAVA)
.withRefTracking(referenceTracking)
.withNumberCompressed(compressNumber)
.withCodegen(enableCodegen1)
.withMetaContextShare(true)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.requireClassRegistration(false)
.build();
String pkg = BeanA.class.getPackage().getName();
String code =
""
Expand Down Expand Up @@ -234,7 +289,6 @@ public void testWriteCompatibleCollectionBasic(
.requireClassRegistration(false)
.withClassLoader(cls1.getClassLoader())
.build();
MetaContext context1 = new MetaContext();
code =
""
+ "package "
Expand Down Expand Up @@ -264,6 +318,8 @@ public void testWriteCompatibleCollectionBasic(
.requireClassRegistration(false)
.withClassLoader(cls2.getClassLoader())
.build();

MetaContext context1 = new MetaContext();
MetaContext context2 = new MetaContext();
fury2.getSerializationContext().setMetaContext(context2);
byte[] bytes2 = fury2.serialize(o2);
Expand All @@ -282,6 +338,16 @@ public void testWriteCompatibleCollectionBasic(
Object obj2 = fury2.deserialize(objBytes);
Assert.assertTrue(ReflectionUtils.objectCommonFieldsEquals(obj2, o2));

Fury fury =
Fury.builder()
.withLanguage(Language.JAVA)
.withRefTracking(referenceTracking)
.withNumberCompressed(compressNumber)
.withCodegen(enableCodegen1)
.withMetaContextShare(true)
.withCompatibleMode(CompatibleMode.COMPATIBLE)
.requireClassRegistration(false)
.build();
// fury <-> fury2 is a new channel, which needs a new context.
MetaContext context = new MetaContext();
MetaContext ctx2 = new MetaContext();
Expand Down

0 comments on commit f717a82

Please sign in to comment.