Class SerializableTester


  • public final class SerializableTester
    extends java.lang.Object
    This is a utility class to check the serializable nature of classes.

    NOTE: In the future this class will be perhaps part of the ObjectTester class.

    Before v1.1 the methods are named "checkSerialization". Since 1.1 these methods will have now an "assert" prefix ("assertSerialization").

    Since:
    1.0.3 (21.07.2010)
    Author:
    oliver (oliver.boehm@agentes.de)
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertNoFinalTransientAttribute​(java.lang.Class<? extends java.io.Serializable> clazz)
      Serializable classes must not have final transient attributes.
      static void assertSerialization​(java.io.Serializable object)
      Check serialization and deserialization of an object.
      static void assertSerialization​(java.lang.Class<? extends java.io.Serializable> clazz)
      This method will create an object of the given class using the default constructor.
      static void assertSerialization​(java.lang.Package pkg)
      Check for each class in the given package if it can be serialized and deserialized.
      static void assertSerialization​(java.util.Collection<java.lang.Class<java.io.Serializable>> classes)
      Check for each class in the given collection if it can be serialized and deserialized.
      static void assertSerializationOfPackage​(java.lang.String packageName)
      Check for each class in the given package if it can be serialized and deserialized.
      static java.io.Serializable deserialize​(java.io.Serializable object)
      Serialiazes and deserializes the given object.
      static int getSizeOf​(java.io.Serializable object)
      Gets the size of the given object - this is the number of bytes the given object needs for serialization.
      static int getSizeOf​(java.lang.Class<java.io.Serializable> clazz)
      Gets the size of of a newly created object of the given clazz.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • assertSerialization

        public static void assertSerialization​(java.io.Serializable object)
                                        throws java.io.NotSerializableException
        Check serialization and deserialization of an object. Because not all classes has a (correct) overwritten equals method we do not compare the deserialized object with the equals() method. But if it implements Comparable we verify the equality of the two objects with the compareTo() method.
        Parameters:
        object - the object
        Throws:
        java.io.NotSerializableException - if object can't serialized
        Since:
        1.1
      • deserialize

        public static java.io.Serializable deserialize​(java.io.Serializable object)
                                                throws java.io.NotSerializableException
        Serialiazes and deserializes the given object. This can be used e.g. for testing if you want to test the deserialized object if transient attributes are initialized correct (transient attributes are null after deserialization).

        This method is a result of feature request 50.

        Parameters:
        object - the object to serialize
        Returns:
        the deserialized object
        Throws:
        java.io.NotSerializableException - if serialization fails
        Since:
        1.7.2
      • assertSerialization

        public static void assertSerialization​(java.lang.Class<? extends java.io.Serializable> clazz)
                                        throws java.io.NotSerializableException
        This method will create an object of the given class using the default constructor. So three preconditions must be true:
        1. the class must not be abstract
        2. there must be a (public) default constructor
        3. it must be Serializable
        Parameters:
        clazz - the clazz
        Throws:
        java.io.NotSerializableException - if the check fails
        Since:
        1.1
      • assertSerialization

        public static void assertSerialization​(java.util.Collection<java.lang.Class<java.io.Serializable>> classes)
                                        throws java.io.NotSerializableException
        Check for each class in the given collection if it can be serialized and deserialized.
        Parameters:
        classes - a collection of classes to be checked
        Throws:
        java.io.NotSerializableException - if one of the classes can't be serialized
        Since:
        1.1
      • assertSerialization

        public static void assertSerialization​(java.lang.Package pkg)
                                        throws java.io.NotSerializableException
        Check for each class in the given package if it can be serialized and deserialized.

        To get a name of a package call Package.getPackage(String). But be sure that you can't get null as result. In this case use assertSerializationOfPackage(String).

        Parameters:
        pkg - the package e.g. "patterntesting.runtime"
        Throws:
        java.io.NotSerializableException - if one of the class can't be serialized
        Since:
        1.1
        See Also:
        assertSerializationOfPackage(String)
      • assertSerializationOfPackage

        public static void assertSerializationOfPackage​(java.lang.String packageName)
                                                 throws java.io.NotSerializableException
        Check for each class in the given package if it can be serialized and deserialized.
        Parameters:
        packageName - the package name e.g. "patterntesting.runtime"
        Throws:
        java.io.NotSerializableException - if one of the class can't be serialized
        Since:
        1.1
      • assertNoFinalTransientAttribute

        public static void assertNoFinalTransientAttribute​(java.lang.Class<? extends java.io.Serializable> clazz)
        Serializable classes must not have final transient attributes. Why? Transient attributes are not stored when an object is serialized. And if they are final they cannot be initialized after deserialization.
        Parameters:
        clazz - the serialiable class
        Since:
        1.7.2
      • getSizeOf

        public static int getSizeOf​(java.lang.Class<java.io.Serializable> clazz)
        Gets the size of of a newly created object of the given clazz. This is the is the number of bytes the created object needs for serialization.
        Parameters:
        clazz - the clazz
        Returns:
        the size of
        Since:
        1.5 (14.10.2014)
      • getSizeOf

        public static int getSizeOf​(java.io.Serializable object)
        Gets the size of the given object - this is the number of bytes the given object needs for serialization.
        Parameters:
        object - the object
        Returns:
        the size of
        Since:
        1.5 (14.10.2014)