org.mapdb
Class SerializerPojo

java.lang.Object
  extended by org.mapdb.SerializerBase
      extended by org.mapdb.SerializerPojo
All Implemented Interfaces:
Serializable, Serializer<Object>

public class SerializerPojo
extends SerializerBase
implements Serializable

Serializer which handles POJO, object graphs etc.

Author:
Jan Kotek
See Also:
Serialized Form

Nested Class Summary
protected static class SerializerPojo.ClassInfo
          Stores info about single class stored in MapDB.
protected static class SerializerPojo.FieldInfo
          Stores info about single field stored in MapDB.
protected  class SerializerPojo.ObjectInputStream2
           
protected  class SerializerPojo.ObjectOutputStream2
           
 
Nested classes/interfaces inherited from class org.mapdb.SerializerBase
SerializerBase.FastArrayList<K>, SerializerBase.Header, SerializerBase.HeaderMapDB, SerializerBase.singletons
 
Nested classes/interfaces inherited from interface org.mapdb.Serializer
Serializer.CompressionWrapper<E>
 
Field Summary
protected static Method androidConstructor
           
protected  Map<Class<?>,Integer> class2classId
           
protected static Map<Class<?>,Constructor<?>> class2constuctor
           
protected  Map<Integer,Class<?>> classId2class
           
protected  DB db
           
protected  ReentrantReadWriteLock lock
           
protected  int oldSize
           
protected  CopyOnWriteArrayList<SerializerPojo.ClassInfo> registered
           
protected  CopyOnWriteArrayList<Fun.Function1> serializationTransformsDeserialize
           
protected  CopyOnWriteArrayList<Fun.Function1> serializationTransformsSerialize
           
protected static Serializer<CopyOnWriteArrayList<SerializerPojo.ClassInfo>> serializer
           
protected static Method sunConstructor
           
protected static Object sunReflFac
           
 
Fields inherited from class org.mapdb.SerializerBase
EMPTY_STRING
 
Fields inherited from interface org.mapdb.Serializer
BASIC, BOOLEAN, BYTE_ARRAY, BYTE_ARRAY_NOSIZE, CHAR_ARRAY, DOUBLE_ARRAY, ILLEGAL_ACCESS, INT_ARRAY, INTEGER, JAVA, LONG, LONG_ARRAY, STRING, STRING_ASCII, STRING_INTERN, STRING_NOSIZE, UUID
 
Constructor Summary
SerializerPojo(CopyOnWriteArrayList<SerializerPojo.ClassInfo> registered)
           
 
Method Summary
protected  void assertClassSerializable(Class<?> clazz)
           
protected static Class<?> classForName(String className)
           
 boolean containsClass(Class<?> clazz)
           
protected
<T> T
createInstanceSkippinkConstructor(Class<T> clazz)
          For pojo serialization we need to instantiate class without invoking its constructor.
 Object deserialize(DataInput is, int capacity)
          Deserialize the content of an object from a DataInput.
protected  Object deserializeUnknownHeader(DataInput in, int head, SerializerBase.FastArrayList<Object> objectStack)
          override this method to extend SerializerBase functionality
 int getClassId(Class<?> clazz)
           
protected  Engine getEngine()
           
protected  ObjectStreamField[] getFields(Class<?> clazz)
           
 Object getFieldValue(SerializerPojo.FieldInfo fieldInfo, Object object)
           
 boolean hasUnsavedChanges()
           
 void registerClass(Class<?> clazz)
           
 void save(Engine e)
           
protected  void saveClassInfo()
          action performed after classInfo was modified, feel free to override
 void serialize(DataOutput out, Object obj)
          Serialize the content of an object into a ObjectOutput
<A,R> void
serializerTransformAdd(Fun.Function1<A,R> beforeSerialization, Fun.Function1<R,A> afterDeserialization)
          Add interceptor which may modify all deserialized/serialized objects
<A,R> void
serializerTransformRemove(Fun.Function1<A,R> beforeSerialization, Fun.Function1<R,A> afterDeserialization)
          Removes interceptor which may modify all deserialized/serialized objects
protected  void serializeUnknownObject(DataOutput out, Object obj, SerializerBase.FastArrayList<Object> objectStack)
          override this method to extend SerializerBase functionality
protected  void setDb(DB db)
           
 void setFieldValue(SerializerPojo.FieldInfo fieldInfo, Object object, Object value)
           
protected  boolean usesAdvancedSerialization(Class<?> clazz)
           
 
Methods inherited from class org.mapdb.SerializerBase
assertSerializable, booleanToByteArray, deserialize, deserializeClass, deserializeMapDB, fixedSize, readBooleanArray, serialize, serializeClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serializer

protected static final Serializer<CopyOnWriteArrayList<SerializerPojo.ClassInfo>> serializer

lock

protected final ReentrantReadWriteLock lock

db

protected DB db

registered

protected CopyOnWriteArrayList<SerializerPojo.ClassInfo> registered

class2classId

protected Map<Class<?>,Integer> class2classId

classId2class

protected Map<Integer,Class<?>> classId2class

sunConstructor

protected static Method sunConstructor

sunReflFac

protected static Object sunReflFac

androidConstructor

protected static Method androidConstructor

class2constuctor

protected static Map<Class<?>,Constructor<?>> class2constuctor

oldSize

protected int oldSize

serializationTransformsSerialize

protected CopyOnWriteArrayList<Fun.Function1> serializationTransformsSerialize

serializationTransformsDeserialize

protected CopyOnWriteArrayList<Fun.Function1> serializationTransformsDeserialize
Constructor Detail

SerializerPojo

public SerializerPojo(CopyOnWriteArrayList<SerializerPojo.ClassInfo> registered)
Method Detail

classForName

protected static Class<?> classForName(String className)

setDb

protected void setDb(DB db)

registerClass

public void registerClass(Class<?> clazz)
                   throws IOException
Throws:
IOException

usesAdvancedSerialization

protected boolean usesAdvancedSerialization(Class<?> clazz)

saveClassInfo

protected void saveClassInfo()
action performed after classInfo was modified, feel free to override


getFields

protected ObjectStreamField[] getFields(Class<?> clazz)

assertClassSerializable

protected void assertClassSerializable(Class<?> clazz)
                                throws NotSerializableException,
                                       InvalidClassException
Throws:
NotSerializableException
InvalidClassException

getFieldValue

public Object getFieldValue(SerializerPojo.FieldInfo fieldInfo,
                            Object object)

setFieldValue

public void setFieldValue(SerializerPojo.FieldInfo fieldInfo,
                          Object object,
                          Object value)

containsClass

public boolean containsClass(Class<?> clazz)

getClassId

public int getClassId(Class<?> clazz)

getEngine

protected Engine getEngine()
Overrides:
getEngine in class SerializerBase

serializeUnknownObject

protected void serializeUnknownObject(DataOutput out,
                                      Object obj,
                                      SerializerBase.FastArrayList<Object> objectStack)
                               throws IOException
Description copied from class: SerializerBase
override this method to extend SerializerBase functionality

Overrides:
serializeUnknownObject in class SerializerBase
Throws:
IOException

deserializeUnknownHeader

protected Object deserializeUnknownHeader(DataInput in,
                                          int head,
                                          SerializerBase.FastArrayList<Object> objectStack)
                                   throws IOException
Description copied from class: SerializerBase
override this method to extend SerializerBase functionality

Overrides:
deserializeUnknownHeader in class SerializerBase
Throws:
IOException

createInstanceSkippinkConstructor

protected <T> T createInstanceSkippinkConstructor(Class<T> clazz)
                                       throws NoSuchMethodException,
                                              InvocationTargetException,
                                              IllegalAccessException,
                                              InstantiationException
For pojo serialization we need to instantiate class without invoking its constructor. There are two ways to do it:

Using proprietary API on Oracle JDK and OpenJDK sun.reflect.ReflectionFactory.getReflectionFactory().newConstructorForSerialization() more at http://www.javaspecialists.eu/archive/Issue175.html

Using 'ObjectInputStream.newInstance' on Android http://stackoverflow.com/a/3448384

If non of these works we fallback into usual reflection which requires an no-arg constructor

Throws:
NoSuchMethodException
InvocationTargetException
IllegalAccessException
InstantiationException

hasUnsavedChanges

public boolean hasUnsavedChanges()

save

public void save(Engine e)

serializerTransformAdd

public <A,R> void serializerTransformAdd(Fun.Function1<A,R> beforeSerialization,
                                         Fun.Function1<R,A> afterDeserialization)
Add interceptor which may modify all deserialized/serialized objects

Parameters:
beforeSerialization - transform called on all object before they are serialized
afterDeserialization - transform called on all object after they are serialized

serializerTransformRemove

public <A,R> void serializerTransformRemove(Fun.Function1<A,R> beforeSerialization,
                                            Fun.Function1<R,A> afterDeserialization)
Removes interceptor which may modify all deserialized/serialized objects

Parameters:
beforeSerialization - transform called on all object before they are serialized
afterDeserialization - transform called on all object after they are serialized

serialize

public void serialize(DataOutput out,
                      Object obj)
               throws IOException
Description copied from interface: Serializer
Serialize the content of an object into a ObjectOutput

Specified by:
serialize in interface Serializer<Object>
Overrides:
serialize in class SerializerBase
Parameters:
out - ObjectOutput to save object into
obj - Object to serialize
Throws:
IOException

deserialize

public Object deserialize(DataInput is,
                          int capacity)
                   throws IOException
Description copied from interface: Serializer
Deserialize the content of an object from a DataInput.

Specified by:
deserialize in interface Serializer<Object>
Overrides:
deserialize in class SerializerBase
Parameters:
is - to read serialized data from
capacity - how many bytes are available in DataInput for reading, may be -1 (in streams) or 0 (null).
Returns:
deserialized object
Throws:
IOException


Copyright © 2014. All Rights Reserved.