diff --git a/integration_tests/latest_jdk_tests/src/test/java/io/fury/integration_tests/RecordSerializersTest.java b/integration_tests/latest_jdk_tests/src/test/java/io/fury/integration_tests/RecordSerializersTest.java index 395aaea31d..570086713e 100644 --- a/integration_tests/latest_jdk_tests/src/test/java/io/fury/integration_tests/RecordSerializersTest.java +++ b/integration_tests/latest_jdk_tests/src/test/java/io/fury/integration_tests/RecordSerializersTest.java @@ -97,7 +97,9 @@ public void testRecordCompatible(boolean codegen) throws Throwable { String code1 = "import java.util.*;" + "public record TestRecord(int f1, String f2, List f3, char f4, Map f5) {}"; - Class cls1 = Struct.createStructClass("TestRecord", code1); + Class cls1 = + Struct.createStructClass( + "TestRecord", code1, RecordSerializersTest.class + "testRecordCompatible_1"); Object record1 = RecordUtils.getRecordConstructor(cls1) .f1 @@ -114,7 +116,9 @@ public void testRecordCompatible(boolean codegen) throws Throwable { String code2 = "import java.util.*;" + "public record TestRecord(int f1, String f2, char f4, Map f5) {}"; - Class cls2 = Struct.createStructClass("TestRecord", code2); + Class cls2 = + Struct.createStructClass( + "TestRecord", code2, RecordSerializersTest.class + "testRecordCompatible_2"); Object record2 = RecordUtils.getRecordConstructor(cls2).f1.invoke(1, "abc", 'a', ofHashMap("a", 1)); Fury fury2 = @@ -134,7 +138,9 @@ public void testRecordMetaShare(boolean codegen) throws Throwable { String code1 = "import java.util.*;" + "public record TestRecord(int f1, String f2, List f3, char f4, Map f5) {}"; - Class cls1 = Struct.createStructClass("TestRecord", code1); + Class cls1 = + Struct.createStructClass( + "TestRecord", code1, RecordSerializersTest.class + "testRecordMetaShare_1"); Object record1 = RecordUtils.getRecordConstructor(cls1) .f1 @@ -150,7 +156,9 @@ public void testRecordMetaShare(boolean codegen) throws Throwable { String code2 = "import java.util.*;" + "public record TestRecord(String f2, char f4, Map f5) {}"; - Class cls2 = Struct.createStructClass("TestRecord", code2); + Class cls2 = + Struct.createStructClass( + "TestRecord", code2, RecordSerializersTest.class + "testRecordMetaShare_2"); Object record2 = RecordUtils.getRecordConstructor(cls2).f1.invoke("abc", 'a', ofHashMap("a", 1)); Fury fury2 = diff --git a/java/fury-core/src/test/java/io/fury/serializer/ClassUtils.java b/java/fury-core/src/test/java/io/fury/serializer/ClassUtils.java index 9a3f6edb8b..71b534d2b8 100644 --- a/java/fury-core/src/test/java/io/fury/serializer/ClassUtils.java +++ b/java/fury-core/src/test/java/io/fury/serializer/ClassUtils.java @@ -63,7 +63,7 @@ public static Class createCompatibleClass1() { + " private int[][] int2DArray;\n" + " private int[][] int2DArray_added;\n" + "}"; - return loadClass(BeanA.class, code); + return loadClass(BeanA.class, code, ClassUtils.class + "createCompatibleClass1"); } public static Class createCompatibleClass2() { @@ -90,7 +90,7 @@ public static Class createCompatibleClass2() { + " public Map map2;\n" + " public SortedMap sortedMap3;" + "}"; - return loadClass(CollectionFields.class, code); + return loadClass(CollectionFields.class, code, ClassUtils.class + "createCompatibleClass2"); } public static Class createCompatibleClass3() { @@ -121,13 +121,13 @@ public static Class createCompatibleClass3() { + " public Map singletonMap;\n" + " public Map singletonMap2;\n" + "}"; - return loadClass(MapFields.class, code); + return loadClass(MapFields.class, code, ClassUtils.class + "createCompatibleClass3"); } - private static final ConcurrentHashMap> classCache = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap> classCache = new ConcurrentHashMap<>(); - static Class loadClass(Class cls, String code) { - return classCache.computeIfAbsent(code, k -> compileClass(cls, code)); + static Class loadClass(Class cls, String code, Object cacheKey) { + return classCache.computeIfAbsent(cacheKey, k -> compileClass(cls, code)); } private static Class compileClass(Class cls, String code) { diff --git a/java/fury-core/src/test/java/io/fury/serializer/CodegenCompatibleSerializerTest.java b/java/fury-core/src/test/java/io/fury/serializer/CodegenCompatibleSerializerTest.java index 13f98a7b8b..faa6fd60c9 100644 --- a/java/fury-core/src/test/java/io/fury/serializer/CodegenCompatibleSerializerTest.java +++ b/java/fury-core/src/test/java/io/fury/serializer/CodegenCompatibleSerializerTest.java @@ -144,7 +144,11 @@ public void testWriteCompatibleCollectionBasic(boolean referenceTracking, boolea + " private Iterable beanBIterable;\n" + " private List beanBList;\n" + "}"; - Class cls1 = loadClass(BeanA.class, code); + Class cls1 = + loadClass( + BeanA.class, + code, + CodegenCompatibleSerializerTest.class + "testWriteCompatibleCollectionBasic_1"); Fury fury1 = Fury.builder() .withLanguage(Language.JAVA) @@ -165,7 +169,11 @@ public void testWriteCompatibleCollectionBasic(boolean referenceTracking, boolea + " private List doubleList;\n" + " private Iterable beanBIterable;\n" + "}"; - Class cls2 = loadClass(BeanA.class, code); + Class cls2 = + loadClass( + BeanA.class, + code, + CodegenCompatibleSerializerTest.class + "testWriteCompatibleCollectionBasic_2"); Object newBeanA = cls2.newInstance(); ReflectionUtils.unsafeCopy(beanA, newBeanA); Fury fury2 = diff --git a/java/fury-core/src/test/java/io/fury/serializer/CompatibleSerializerTest.java b/java/fury-core/src/test/java/io/fury/serializer/CompatibleSerializerTest.java index 05e4c89762..32aa4271ae 100644 --- a/java/fury-core/src/test/java/io/fury/serializer/CompatibleSerializerTest.java +++ b/java/fury-core/src/test/java/io/fury/serializer/CompatibleSerializerTest.java @@ -226,7 +226,8 @@ public static Class createCompatibleClass1() { + " private int[][] int2DArray;\n" + " private int[][] int2DArray_added;\n" + "}"; - return ClassUtils.loadClass(BeanA.class, code); + return ClassUtils.loadClass( + BeanA.class, code, CompatibleSerializerTest.class + "createCompatibleClass1"); } @Test(dataProvider = "referenceTrackingConfig") @@ -304,7 +305,8 @@ public static Class createCompatibleClass2() { + " public Map map2;\n" + " public SortedMap sortedMap3;" + "}"; - return ClassUtils.loadClass(CollectionFields.class, code); + return ClassUtils.loadClass( + CollectionFields.class, code, CompatibleSerializerTest.class + "createCompatibleClass2"); } @Test(dataProvider = "referenceTrackingConfig") @@ -380,7 +382,8 @@ public static Class createCompatibleClass3() { + " public Map singletonMap;\n" + " public Map singletonMap2;\n" + "}"; - return ClassUtils.loadClass(MapFields.class, code); + return ClassUtils.loadClass( + MapFields.class, code, CompatibleSerializerTest.class + "createCompatibleClass3"); } @Test(dataProvider = "compressNumber") diff --git a/java/fury-core/src/test/java/io/fury/serializer/MetaSharedCompatibleTest.java b/java/fury-core/src/test/java/io/fury/serializer/MetaSharedCompatibleTest.java index 67eab0fcfa..ff595bfc00 100644 --- a/java/fury-core/src/test/java/io/fury/serializer/MetaSharedCompatibleTest.java +++ b/java/fury-core/src/test/java/io/fury/serializer/MetaSharedCompatibleTest.java @@ -213,7 +213,11 @@ public void testWriteCompatibleCollectionBasic( + " private Iterable beanBIterable;\n" + " private List beanBList;\n" + "}"; - Class cls1 = loadClass(BeanA.class, code); + Class cls1 = + loadClass( + BeanA.class, + code, + MetaSharedCompatibleTest.class + "testWriteCompatibleCollectionBasic_1"); Fury fury1 = Fury.builder() .withLanguage(Language.JAVA) @@ -237,7 +241,11 @@ public void testWriteCompatibleCollectionBasic( + " private List doubleList;\n" + " private Iterable beanBIterable;\n" + "}"; - Class cls2 = loadClass(BeanA.class, code); + Class cls2 = + loadClass( + BeanA.class, + code, + MetaSharedCompatibleTest.class + "testWriteCompatibleCollectionBasic_2"); Object o2 = cls2.newInstance(); ReflectionUtils.unsafeCopy(beanA, o2); Fury fury2 = diff --git a/java/fury-test-core/src/main/java/io/fury/test/bean/Struct.java b/java/fury-test-core/src/main/java/io/fury/test/bean/Struct.java index 0fa4f9256e..3cf7921c57 100644 --- a/java/fury-test-core/src/main/java/io/fury/test/bean/Struct.java +++ b/java/fury-test-core/src/main/java/io/fury/test/bean/Struct.java @@ -228,7 +228,7 @@ public static Class createNumberStructClass(String classname, int repeat) { IntStream.range(i * numFields, i * numFields + numFields).boxed().toArray())); } classCode.append("}"); - return createStructClass(classname, classCode.toString()); + return compile(classname, classCode.toString()); }); } @@ -277,13 +277,20 @@ public static Class createStructClass(String classname, int repeat) { IntStream.range(i * numFields, i * numFields + numFields).boxed().toArray())); } classCode.append("}"); - return createStructClass(classname, classCode.toString()); + return compile(classname, classCode.toString()); }); } /** Create class. */ - private static Class createStructClass(String classname, String classCode) { + public static Class createStructClass(String classname, String classCode, Object cache) { + if (cache == null) { + return compile(classname, classCode); + } + return classCache.computeIfAbsent(cache, k -> compile(classname, classCode)); + } + /** Create class. */ + private static Class compile(String classname, String classCode) { Path path = Paths.get(classname + ".java"); try { Files.deleteIfExists(path);