From 2cec538ac1468a09367397243e73e0e6c6f8eeb3 Mon Sep 17 00:00:00 2001 From: Amitabh Tiwari Date: Sat, 25 Dec 2021 06:08:28 +0530 Subject: [PATCH] Updated the code and test cases --- .../com/baeldung/serialization/Customer.java | 35 +++++++++++++++++++ .../baeldung/util/MySerializationUtils.java | 4 +-- .../serialization/SerializationUnitTest.java | 2 ++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 core-java-modules/core-java-serialization/src/main/java/com/baeldung/serialization/Customer.java diff --git a/core-java-modules/core-java-serialization/src/main/java/com/baeldung/serialization/Customer.java b/core-java-modules/core-java-serialization/src/main/java/com/baeldung/serialization/Customer.java new file mode 100644 index 0000000000..51587b6c86 --- /dev/null +++ b/core-java-modules/core-java-serialization/src/main/java/com/baeldung/serialization/Customer.java @@ -0,0 +1,35 @@ +package com.baeldung.serialization; + +import java.io.Serializable; + +public class Customer implements Serializable { + private static final long serialVersionUID = 1L; + + private long id; + private volatile String name; + private Address address; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/core-java-modules/core-java-serialization/src/main/java/com/baeldung/util/MySerializationUtils.java b/core-java-modules/core-java-serialization/src/main/java/com/baeldung/util/MySerializationUtils.java index bfaa91313c..d8a3b99481 100644 --- a/core-java-modules/core-java-serialization/src/main/java/com/baeldung/util/MySerializationUtils.java +++ b/core-java-modules/core-java-serialization/src/main/java/com/baeldung/util/MySerializationUtils.java @@ -29,7 +29,7 @@ public class MySerializationUtils { public static boolean isSerializable(Class it) { boolean serializable = it.isPrimitive() || it.isInterface() || Serializable.class.isAssignableFrom(it); if (!serializable) { - return serializable; + return false; } Field[] declaredFields = it.getDeclaredFields(); for (Field field : declaredFields) { @@ -37,7 +37,7 @@ public class MySerializationUtils { continue; } Class fieldType = field.getType(); - return isSerializable(fieldType); + serializable = isSerializable(fieldType); } return serializable; } diff --git a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/SerializationUnitTest.java b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/SerializationUnitTest.java index a8c4009386..dde9beb2bb 100644 --- a/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/SerializationUnitTest.java +++ b/core-java-modules/core-java-serialization/src/test/java/com/baeldung/serialization/SerializationUnitTest.java @@ -107,5 +107,7 @@ public class SerializationUnitTest { assertFalse(MySerializationUtils.isSerializable(Address.class)); assertTrue(MySerializationUtils.isSerializable(Person.class)); assertTrue(MySerializationUtils.isSerializable(Integer.class)); + assertFalse(MySerializationUtils.isSerializable(Customer.class)); + assertTrue(MySerializationUtils.isSerializable(Employee.class)); } }