Merge pull request #11617 from amitiw4u/BAEL-5282-SerializationImprovement

Updated the code and test cases
This commit is contained in:
Loredana Crusoveanu 2021-12-27 16:26:28 +02:00 committed by GitHub
commit b7a443db5d
3 changed files with 42 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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,8 +37,10 @@ public class MySerializationUtils {
continue;
}
Class<?> fieldType = field.getType();
return isSerializable(fieldType);
}
return serializable;
if (!isSerializable(fieldType)) {
return false;
}
}
return true;
}
}

View File

@ -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));
}
}