Method for validation of Serializable Objects

Method for validation of Serializable Objects
This commit is contained in:
Amitabh Tiwari 2021-10-15 09:13:02 +05:30
parent e2f8cdd817
commit e0ca514118
2 changed files with 12 additions and 0 deletions

View File

@ -25,5 +25,9 @@ public class SerializationUtils {
Object o = ois.readObject(); Object o = ois.readObject();
return cl.cast(o); return cl.cast(o);
} }
public static boolean isSerializable(Class<?> it) {
return it.isPrimitive() || it.isInterface() || Serializable.class.isAssignableFrom(it);
}
} }

View File

@ -1,6 +1,7 @@
package com.baeldung.serialization; package com.baeldung.serialization;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -76,4 +77,11 @@ public class SerializationUnitTest {
assertTrue(p2.getAge() == p.getAge()); assertTrue(p2.getAge() == p.getAge());
assertTrue(p2.getName().equals(p.getName())); assertTrue(p2.getName().equals(p.getName()));
} }
@Test
public void whenSerializingUsingCustomSerializationUtils_ThanOk(){
assertFalse(com.baeldung.util.SerializationUtils.isSerializable(Address.class));
assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Person.class));
assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Integer.class));
}
} }