Merge pull request #11617 from amitiw4u/BAEL-5282-SerializationImprovement
Updated the code and test cases
This commit is contained in:
commit
b7a443db5d
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
if (!isSerializable(fieldType)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return serializable;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue