From f4571e47eb360caaf0c6e37f1b29551b73fe0849 Mon Sep 17 00:00:00 2001 From: LiangliangSui Date: Thu, 14 Mar 2024 22:56:59 +0800 Subject: [PATCH] chore(java): Move default Serializer registration location Move all default Serializer registrations to specific Serializers. Signed-off-by: LiangliangSui --- .../apache/fury/resolver/ClassResolver.java | 29 ++++--------------- .../fury/serializer/JdkProxySerializer.java | 6 ++++ .../fury/serializer/LambdaSerializer.java | 6 ++++ .../fury/serializer/LocaleSerializer.java | 4 +++ .../serializer/ReplaceResolveSerializer.java | 6 ++++ .../fury/serializer/StringSerializer.java | 4 +++ .../serializer/UnexistedClassSerializers.java | 5 ++++ 7 files changed, 37 insertions(+), 23 deletions(-) diff --git a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java index e24e3f6328..5feff7ff0b 100644 --- a/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java +++ b/java/fury-core/src/main/java/org/apache/fury/resolver/ClassResolver.java @@ -57,7 +57,6 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -305,7 +304,7 @@ public void initialize() { private void addDefaultSerializers() { // primitive types will be boxed. - addDefaultSerializer(String.class, new StringSerializer(fury)); + StringSerializer.registerDefaultSerializers(fury); PrimitiveSerializers.registerDefaultSerializers(fury); Serializers.registerDefaultSerializers(fury); ArraySerializers.registerDefaultSerializers(fury); @@ -313,16 +312,10 @@ private void addDefaultSerializers() { OptionalSerializers.registerDefaultSerializers(fury); CollectionSerializers.registerDefaultSerializers(fury); MapSerializers.registerDefaultSerializers(fury); - addDefaultSerializer(Locale.class, new LocaleSerializer(fury)); - addDefaultSerializer( - LambdaSerializer.ReplaceStub.class, - new LambdaSerializer(fury, LambdaSerializer.ReplaceStub.class)); - addDefaultSerializer( - JdkProxySerializer.ReplaceStub.class, - new JdkProxySerializer(fury, JdkProxySerializer.ReplaceStub.class)); - addDefaultSerializer( - ReplaceResolveSerializer.ReplaceStub.class, - new ReplaceResolveSerializer(fury, ReplaceResolveSerializer.ReplaceStub.class)); + LocaleSerializer.registerDefaultSerializers(fury); + LambdaSerializer.registerDefaultSerializers(fury); + JdkProxySerializer.registerDefaultSerializers(fury); + ReplaceResolveSerializer.registerDefaultSerializers(fury); SynchronizedSerializers.registerSerializers(fury); UnmodifiableSerializers.registerSerializers(fury); ImmutableCollectionSerializers.registerSerializers(fury); @@ -330,8 +323,7 @@ private void addDefaultSerializers() { GuavaCollectionSerializers.registerDefaultSerializers(fury); } if (metaContextShareEnabled) { - addDefaultSerializer( - UnexistedMetaSharedClass.class, new UnexistedClassSerializer(fury, null)); + UnexistedClassSerializer.registerDefaultSerializers(fury); // Those class id must be known in advance, here is two bytes, so // `UnexistedClassSerializer.writeClassDef` // can overwrite written classinfo and replace with real classinfo. @@ -343,15 +335,6 @@ private void addDefaultSerializers() { } } - private void addDefaultSerializer(Class type, Class serializerClass) { - addDefaultSerializer(type, Serializers.newSerializer(fury, type, serializerClass)); - } - - private void addDefaultSerializer(Class type, Serializer serializer) { - registerSerializer(type, serializer); - register(type); - } - /** Register common class ahead to get smaller class id for serialization. */ private void registerCommonUsedClasses() { register(Class.class); diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/JdkProxySerializer.java b/java/fury-core/src/main/java/org/apache/fury/serializer/JdkProxySerializer.java index 5befb551ce..55ce5cc1cc 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/JdkProxySerializer.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/JdkProxySerializer.java @@ -74,5 +74,11 @@ public Object read(MemoryBuffer buffer) { return proxy; } + public static void registerDefaultSerializers(Fury fury) { + fury.registerSerializer( + JdkProxySerializer.ReplaceStub.class, + new JdkProxySerializer(fury, JdkProxySerializer.ReplaceStub.class)); + } + public static class ReplaceStub {} } diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/LambdaSerializer.java b/java/fury-core/src/main/java/org/apache/fury/serializer/LambdaSerializer.java index 3d60ac003d..4b6871d7ee 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/LambdaSerializer.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/LambdaSerializer.java @@ -114,6 +114,12 @@ private Serializer getDataSerializer() { return dataSerializer; } + public static void registerDefaultSerializers(Fury fury) { + fury.registerSerializer( + LambdaSerializer.ReplaceStub.class, + new LambdaSerializer(fury, LambdaSerializer.ReplaceStub.class)); + } + /** * Class name of dynamic generated class is not fixed, so we use a stub class to mock dynamic * class. diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/LocaleSerializer.java b/java/fury-core/src/main/java/org/apache/fury/serializer/LocaleSerializer.java index de7c84fe10..0cba84f300 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/LocaleSerializer.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/LocaleSerializer.java @@ -102,4 +102,8 @@ static boolean isSame(Locale locale, String language, String country, String var && locale.getCountry().equals(country) && locale.getVariant().equals(variant)); } + + public static void registerDefaultSerializers(Fury fury) { + fury.registerSerializer(Locale.class, new LocaleSerializer(fury)); + } } diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/ReplaceResolveSerializer.java b/java/fury-core/src/main/java/org/apache/fury/serializer/ReplaceResolveSerializer.java index da1c59fa2b..e8001ffc94 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/ReplaceResolveSerializer.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/ReplaceResolveSerializer.java @@ -330,4 +330,10 @@ private Object readObject(MemoryBuffer buffer) { return o; } } + + public static void registerDefaultSerializers(Fury fury) { + fury.registerSerializer( + ReplaceResolveSerializer.ReplaceStub.class, + new ReplaceResolveSerializer(fury, ReplaceResolveSerializer.ReplaceStub.class)); + } } diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/StringSerializer.java b/java/fury-core/src/main/java/org/apache/fury/serializer/StringSerializer.java index 0bd8420184..2548ee8070 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/StringSerializer.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/StringSerializer.java @@ -694,4 +694,8 @@ public String readUTF8String(MemoryBuffer buffer) { return new String(tmpArray, 0, numBytes, StandardCharsets.UTF_8); } } + + public static void registerDefaultSerializers(Fury fury) { + fury.registerSerializer(String.class, new StringSerializer(fury)); + } } diff --git a/java/fury-core/src/main/java/org/apache/fury/serializer/UnexistedClassSerializers.java b/java/fury-core/src/main/java/org/apache/fury/serializer/UnexistedClassSerializers.java index 73215c0a8e..785d4a1677 100644 --- a/java/fury-core/src/main/java/org/apache/fury/serializer/UnexistedClassSerializers.java +++ b/java/fury-core/src/main/java/org/apache/fury/serializer/UnexistedClassSerializers.java @@ -238,5 +238,10 @@ public Object read(MemoryBuffer buffer) { obj.setEntries(entries); return obj; } + + public static void registerDefaultSerializers(Fury fury) { + fury.registerSerializer( + UnexistedMetaSharedClass.class, new UnexistedClassSerializer(fury, null)); + } } }