From bea5094d603c3b786d28a9b8db77d14f955fea2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alen=20Vre=C4=8Dko?= <332217+avrecko@users.noreply.github.com> Date: Sun, 12 Mar 2023 12:00:50 +0100 Subject: [PATCH] Don't need to implement Serializable for json only serialization. --- src/one/nio/serial/GeneratedSerializer.java | 7 ++- src/one/nio/serial/Repository.java | 9 ++-- src/one/nio/serial/gen/DelegateGenerator.java | 45 ++++++++++++++++--- test/one/nio/serial/JsonTest.java | 5 +-- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/one/nio/serial/GeneratedSerializer.java b/src/one/nio/serial/GeneratedSerializer.java index 2acc362..0fcd4e1 100755 --- a/src/one/nio/serial/GeneratedSerializer.java +++ b/src/one/nio/serial/GeneratedSerializer.java @@ -39,12 +39,15 @@ public class GeneratedSerializer extends Serializer { static final AtomicInteger renamedFields = new AtomicInteger(); static final AtomicInteger unsupportedFields = new AtomicInteger(); + private final boolean jsonOnlySerialization; + private FieldDescriptor[] fds; private FieldDescriptor[] defaultFields; private Delegate delegate; - GeneratedSerializer(Class cls) { + GeneratedSerializer(Class cls, boolean jsonOnlySerialization) { super(cls); + this.jsonOnlySerialization = jsonOnlySerialization; Field[] ownFields = getSerializableFields(); this.fds = new FieldDescriptor[ownFields.length / 2]; @@ -108,7 +111,7 @@ public void skipExternal(ObjectInput in) throws IOException, ClassNotFoundExcept @Override public byte[] code() { - return DelegateGenerator.generate(cls, fds, defaultFields); + return DelegateGenerator.generate(cls, fds, defaultFields, jsonOnlySerialization); } @Override diff --git a/src/one/nio/serial/Repository.java b/src/one/nio/serial/Repository.java index 5e1f946..3203a13 100755 --- a/src/one/nio/serial/Repository.java +++ b/src/one/nio/serial/Repository.java @@ -372,14 +372,13 @@ private static Serializer generateFor(Class cls) { serializer = new CollectionSerializer(cls); } else if (Map.class.isAssignableFrom(cls) && !hasOptions(cls, FIELD_SERIALIZATION)) { serializer = new MapSerializer(cls); - } else if (Serializable.class.isAssignableFrom(cls)) { - if (cls.getName().startsWith("java.time.") && JavaInternals.findMethod(cls, "writeReplace") != null) { + } else { + boolean extendsSerializable = Serializable.class.isAssignableFrom(cls); + if (extendsSerializable && cls.getName().startsWith("java.time.") && JavaInternals.findMethod(cls, "writeReplace") != null) { serializer = new JavaTimeSerializer(cls); } else { - serializer = new GeneratedSerializer(cls); + serializer = new GeneratedSerializer(cls, !extendsSerializable); } - } else { - serializer = new InvalidSerializer(cls); } serializer.generateUid(); } catch (Throwable e) { diff --git a/src/one/nio/serial/gen/DelegateGenerator.java b/src/one/nio/serial/gen/DelegateGenerator.java index 0f47454..704dce4 100755 --- a/src/one/nio/serial/gen/DelegateGenerator.java +++ b/src/one/nio/serial/gen/DelegateGenerator.java @@ -115,10 +115,10 @@ public static Delegate instantiate(Class cls, FieldDescriptor[] fds, byte[] code } public static Delegate instantiate(Class cls, FieldDescriptor[] fds, FieldDescriptor[] defaultFields) { - return instantiate(cls, fds, generate(cls, fds, defaultFields)); + return instantiate(cls, fds, generate(cls, fds, defaultFields, false)); } - public static byte[] generate(Class cls, FieldDescriptor[] fds, FieldDescriptor[] defaultFields) { + public static byte[] generate(Class cls, FieldDescriptor[] fds, FieldDescriptor[] defaultFields, boolean jsonOnly) { String className = "sun/reflect/Delegate" + index.getAndIncrement() + '_' + cls.getSimpleName(); ClassWriter cv = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); @@ -126,10 +126,19 @@ public static byte[] generate(Class cls, FieldDescriptor[] fds, FieldDescriptor[ new String[]{"one/nio/serial/gen/Delegate"}); generateConstructor(cv, className); - generateCalcSize(cv, cls, fds); - generateWrite(cv, cls, fds); - generateRead(cv, cls, fds, defaultFields, className); - generateSkip(cv, fds); + + if (!jsonOnly) { + generateCalcSize(cv, cls, fds); + generateWrite(cv, cls, fds); + generateRead(cv, cls, fds, defaultFields, className); + generateSkip(cv, fds); + } else { + generateThrowCalcSize(cv, cls); + generateThrowWrite(cv, cls); + generateThrowRead(cv, cls); + generateThrowSkip(cv, cls); + } + generateToJson(cv, cls, fds); generateFromJson(cv, cls, fds, defaultFields, className); @@ -362,6 +371,30 @@ private static void generateSkip(ClassVisitor cv, FieldDescriptor[] fds) { mv.visitEnd(); } + private static void generateThrowNonSerializableMethod(ClassWriter cv, String methodName, String descriptor, Class cls) { + MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_FINAL, methodName, descriptor, + null, new String[]{"java/io/NotSerializableException"}); + mv.visitCode(); + emitThrow(mv, "java/io/NotSerializableException", cls.getName()); + mv.visitEnd(); + } + + private static void generateThrowCalcSize(ClassWriter cv, Class cls) { + generateThrowNonSerializableMethod(cv, "calcSize", "(Ljava/lang/Object;Lone/nio/serial/CalcSizeStream;)V", cls); + } + + private static void generateThrowWrite(ClassWriter cv, Class cls) { + generateThrowNonSerializableMethod(cv, "write", "(Ljava/lang/Object;Lone/nio/serial/DataStream;)V", cls); + } + + private static void generateThrowRead(ClassWriter cv, Class cls) { + generateThrowNonSerializableMethod(cv, "read", "(Lone/nio/serial/DataStream;)Ljava/lang/Object;", cls); + } + + private static void generateThrowSkip(ClassWriter cv, Class cls) { + generateThrowNonSerializableMethod(cv, "skip", "(Lone/nio/serial/DataStream;)V", cls); + } + private static void generateToJson(ClassVisitor cv, Class cls, FieldDescriptor[] fds) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_FINAL, "toJson", "(Ljava/lang/Object;Ljava/lang/StringBuilder;)V", null, new String[]{"java/io/IOException"}); diff --git a/test/one/nio/serial/JsonTest.java b/test/one/nio/serial/JsonTest.java index a0ff8ad..85ff444 100755 --- a/test/one/nio/serial/JsonTest.java +++ b/test/one/nio/serial/JsonTest.java @@ -20,12 +20,11 @@ import org.junit.Test; import java.io.IOException; -import java.io.Serializable; import java.util.Arrays; import java.util.Date; import java.util.HashMap; -public class JsonTest implements Serializable { +public class JsonTest { private long lng = Long.MIN_VALUE; private Object map = new HashMap() {{ @@ -54,7 +53,7 @@ public void testDateParsing() throws IOException, ClassNotFoundException { Assert.assertNull(Json.fromJson("{\"date\":\" \"}", TestObject.class).date); } - public static class TestObject implements Serializable { + public static class TestObject { @JsonName("test_name") public String name; public Date date;