public Object readObject(final ObjectDataInput in) {
   try {
     final boolean isNull = in.readBoolean();
     if (isNull) {
       return null;
     }
     final int typeId = in.readInt();
     final SerializerAdapter serializer = serializerFor(typeId);
     if (serializer == null) {
       if (active) {
         throw new HazelcastSerializationException(
             "There is no suitable de-serializer for type " + typeId);
       }
       throw new HazelcastInstanceNotActiveException();
     }
     if (typeId == SerializationConstants.CONSTANT_TYPE_PORTABLE
         && in instanceof PortableContextAwareInputStream) {
       ClassDefinition classDefinition = new ClassDefinitionImpl();
       classDefinition.readData(in);
       classDefinition = serializationContext.registerClassDefinition(classDefinition);
       PortableContextAwareInputStream ctxIn = (PortableContextAwareInputStream) in;
       ctxIn.setClassDefinition(classDefinition);
     }
     Object obj = serializer.read(in);
     if (managedContext != null) {
       obj = managedContext.initialize(obj);
     }
     return obj;
   } catch (Throwable e) {
     handleException(e);
   }
   return null;
 }
 public void writeObject(final ObjectDataOutput out, final Object obj) {
   final boolean isNull = obj == null;
   try {
     out.writeBoolean(isNull);
     if (isNull) {
       return;
     }
     final SerializerAdapter serializer = serializerFor(obj.getClass());
     if (serializer == null) {
       if (active) {
         throw new HazelcastSerializationException(
             "There is no suitable serializer for " + obj.getClass());
       }
       throw new HazelcastInstanceNotActiveException();
     }
     out.writeInt(serializer.getTypeId());
     if (obj instanceof Portable) {
       final Portable portable = (Portable) obj;
       ClassDefinition classDefinition =
           serializationContext.lookupOrRegisterClassDefinition(portable);
       classDefinition.writeData(out);
     }
     serializer.write(out, obj);
   } catch (Throwable e) {
     handleException(e);
   }
 }
 private void registerClassDefinitions(
     final Collection<ClassDefinition> classDefinitions, boolean checkClassDefErrors) {
   final Map<Integer, ClassDefinition> classDefMap =
       new HashMap<Integer, ClassDefinition>(classDefinitions.size());
   for (ClassDefinition cd : classDefinitions) {
     if (classDefMap.containsKey(cd.getClassId())) {
       throw new HazelcastSerializationException(
           "Duplicate registration found for class-id[" + cd.getClassId() + "]!");
     }
     classDefMap.put(cd.getClassId(), cd);
   }
   for (ClassDefinition classDefinition : classDefinitions) {
     registerClassDefinition(classDefinition, classDefMap, checkClassDefErrors);
   }
 }
 private void registerClassDefinition(
     ClassDefinition cd, Map<Integer, ClassDefinition> classDefMap, boolean checkClassDefErrors) {
   for (int i = 0; i < cd.getFieldCount(); i++) {
     FieldDefinition fd = cd.get(i);
     if (fd.getType() == FieldType.PORTABLE || fd.getType() == FieldType.PORTABLE_ARRAY) {
       int classId = fd.getClassId();
       ClassDefinition nestedCd = classDefMap.get(classId);
       if (nestedCd != null) {
         ((ClassDefinitionImpl) cd).addClassDef(nestedCd);
         registerClassDefinition(nestedCd, classDefMap, checkClassDefErrors);
         serializationContext.registerClassDefinition(nestedCd);
       } else if (checkClassDefErrors) {
         throw new HazelcastSerializationException(
             "Could not find registered ClassDefinition for class-id: " + classId);
       }
     }
   }
   serializationContext.registerClassDefinition(cd);
 }