Package weka.core
Class SerializationHelper
java.lang.Object
weka.core.SerializationHelper
- All Implemented Interfaces:
RevisionHandler
A helper class for determining serialVersionUIDs and checking whether classes
contain one and/or need one. One can also serialize and deserialize objects
to and from files or streams.
- Version:
- $Revision: 14293 $
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ClassLoadercheckForThirdPartyClass(String className, WekaPackageLibIsolatingClassLoader l) Checks to see if the supplied package class loader (or any of its dependent package class loaders) has the given third party class.static ObjectInputStreamgetObjectInputStream(InputStream stream) Get a (Weka package classloader aware)ObjectInputStreaminstance for reading objects from the supplied input streamReturns the revision string.static longreads or creates the serialVersionUID for the given class.static longreads or creates the serialVersionUID for the given class.static booleanchecks whether the given class contains a serialVersionUID.static booleanchecks whether the given class contains a serialVersionUID.static booleanisSerializable(Class<?> c) checks whether a class is serializable.static booleanisSerializable(String classname) checks whether a class is serializable.static voidOutputs information about a class on the commandline, takes class name as arguments.static booleanchecks whether a class needs to declare a serialVersionUID, i.e., it implements the java.io.Serializable interface but doesn't declare a serialVersionUID.static booleanchecks whether a class needs to declare a serialVersionUID, i.e., it implements the java.io.Serializable interface but doesn't declare a serialVersionUID.static Objectread(InputStream stream) deserializes from the given stream and returns the object from it.static Objectdeserializes the given file and returns the object from it.static Object[]readAll(InputStream stream) deserializes from the given stream and returns the object from it.static Object[]deserializes the given file and returns the objects from it.static voidwrite(OutputStream stream, Object o) serializes the given object to the specified stream.static voidserializes the given object to the specified file.static voidwriteAll(OutputStream stream, Object[] o) serializes the given objects to the specified stream.static voidserializes the given objects to the specified file.
-
Field Details
-
SERIAL_VERSION_UID
the field name of serialVersionUID.- See Also:
-
-
Constructor Details
-
SerializationHelper
public SerializationHelper()
-
-
Method Details
-
isSerializable
checks whether a class is serializable.- Parameters:
classname- the class to check- Returns:
- true if the class or one of its ancestors implements the Serializable interface, otherwise false (also if the class cannot be loaded)
-
isSerializable
checks whether a class is serializable.- Parameters:
c- the class to check- Returns:
- true if the class or one of its ancestors implements the Serializable interface, otherwise false
-
hasUID
checks whether the given class contains a serialVersionUID.- Parameters:
classname- the class to check- Returns:
- true if the class contains a serialVersionUID, otherwise false (also if the class is not implementing serializable or cannot be loaded)
-
hasUID
checks whether the given class contains a serialVersionUID.- Parameters:
c- the class to check- Returns:
- true if the class contains a serialVersionUID, otherwise false (also if the class is not implementing serializable)
-
needsUID
checks whether a class needs to declare a serialVersionUID, i.e., it implements the java.io.Serializable interface but doesn't declare a serialVersionUID.- Parameters:
classname- the class to check- Returns:
- true if the class needs to declare one, false otherwise (also if the class cannot be loaded!)
-
needsUID
checks whether a class needs to declare a serialVersionUID, i.e., it implements the java.io.Serializable interface but doesn't declare a serialVersionUID.- Parameters:
c- the class to check- Returns:
- true if the class needs to declare one, false otherwise
-
getUID
reads or creates the serialVersionUID for the given class.- Parameters:
classname- the class to get the serialVersionUID for- Returns:
- the UID, 0L for non-serializable classes (or if the class cannot be loaded)
-
getUID
reads or creates the serialVersionUID for the given class.- Parameters:
c- the class to get the serialVersionUID for- Returns:
- the UID, 0L for non-serializable classes
-
write
serializes the given object to the specified file.- Parameters:
filename- the file to write the object too- the object to serialize- Throws:
Exception- if serialization fails
-
write
serializes the given object to the specified stream.- Parameters:
stream- the stream to write the object too- the object to serialize- Throws:
Exception- if serialization fails
-
writeAll
serializes the given objects to the specified file.- Parameters:
filename- the file to write the object too- the objects to serialize- Throws:
Exception- if serialization fails
-
writeAll
serializes the given objects to the specified stream.- Parameters:
stream- the stream to write the object too- the objects to serialize- Throws:
Exception- if serialization fails
-
read
deserializes the given file and returns the object from it.- Parameters:
filename- the file to deserialize from- Returns:
- the deserialized object
- Throws:
Exception- if deserialization fails
-
read
deserializes from the given stream and returns the object from it.- Parameters:
stream- the stream to deserialize from- Returns:
- the deserialized object
- Throws:
Exception- if deserialization fails
-
checkForThirdPartyClass
public static ClassLoader checkForThirdPartyClass(String className, WekaPackageLibIsolatingClassLoader l) Checks to see if the supplied package class loader (or any of its dependent package class loaders) has the given third party class.- Parameters:
className- the name of the third-party class to check forl- the third party class loader- Returns:
- the class loader that owns the named third-party class, or null if not found.
-
getObjectInputStream
Get a (Weka package classloader aware)ObjectInputStreaminstance for reading objects from the supplied input stream- Parameters:
stream- the stream to wrap- Returns:
- an
ObjectInputStreaminstance that is aware of of Weka package classloaders - Throws:
IOException- if a problem occurs
-
readAll
deserializes the given file and returns the objects from it.- Parameters:
filename- the file to deserialize from- Returns:
- the deserialized objects
- Throws:
Exception- if deserialization fails
-
readAll
deserializes from the given stream and returns the object from it.- Parameters:
stream- the stream to deserialize from- Returns:
- the deserialized object
- Throws:
Exception- if deserialization fails
-
getRevision
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Returns:
- the revision
-
main
Outputs information about a class on the commandline, takes class name as arguments.- Parameters:
args- the classnames to check- Throws:
Exception- if something goes wrong
-