private static function encodeResponse(MappedClass $methodReturnType, $object, $wasThrown, SerializationPolicy $serializationPolicy) { $stream = new ServerSerializationStreamWriter($serializationPolicy); $stream->prepareToWrite(); if ($methodReturnType->getSignature() != TypeSignatures::$VOID) { //!= void.class $stream->serializeValue($object, $methodReturnType); } $bufferStr = ($wasThrown ? "//EX" : "//OK") . $stream->toString(); return $bufferStr; }
private function deserializeWithDefaultFieldDeserializer(MappedClass $instanceClass, $instance) { /*MappedField[]*/ $declFields = $instanceClass->getDeclaredFields(); /*MappedField[]*/ $serializableFields = SerializabilityUtil::applyFieldSerializationPolicy($declFields); foreach ($serializableFields as $declField) { $value = $this->deserializeValue($declField->getType()); $propName = $declField->getName(); $rClass = $instanceClass->getReflectionClass(); if ($rClass == null) { throw new ClassNotFoundException('MappedClass: ' . $instanceClass->getSignature() . ' do not contains ReflectionClass infomration'); } if (!$rClass->hasProperty($propName)) { throw new SerializationException('MappedClass: ' . $instanceClass->getSignature() . ' do not contains property: ' . $propName . ' Did you mapped all properties?'); } $rProperty = $rClass->getProperty($propName); if ($rProperty->isPublic()) { $rProperty->setValue($instance, $value); } else { // not public access to property, we try invoke setter method $propNameSetter = 'set' . strtoupper($propName[0]) . substr($propName, 1, strlen($propName)); if (!$rClass->hasMethod($propNameSetter)) { throw new SerializationException('MappedClass: ' . $instanceClass->getSignature() . ' do not contains setter method for private property: ' . $propName . '. Mapped object should be in pojo style?'); } $rMethod = $rClass->getMethod($propNameSetter); if ($rMethod->isPublic()) { $rMethod->invoke($instance, $value); } else { throw new SerializationException('MappedClass: ' . $instanceClass->getSignature() . ' do not contains public setter method for private property: ' . $propName . '. Mapped object should be in pojo style?'); } } /* Object value = deserializeValue(declField.getType()); boolean isAccessible = declField.isAccessible(); boolean needsAccessOverride = !isAccessible && !Modifier.isPublic(declField.getModifiers()); if (needsAccessOverride) { // Override access restrictions declField.setAccessible(true); } declField.set(instance, value); if (needsAccessOverride) { // Restore access restrictions declField.setAccessible(isAccessible); } */ } $superClass = $instanceClass->getSuperclass(); if ($superClass != null && $this->getSerializationPolicy()->shouldDeserializeFields($superClass)) { $this->deserializeImpl(SerializabilityUtil::hasCustomFieldSerializer($superClass), $superClass, $instance); } /* Class<?> superClass = instanceClass.getSuperclass(); if (serializationPolicy.shouldDeserializeFields(superClass)) { deserializeImpl(SerializabilityUtil.hasCustomFieldSerializer(superClass), superClass, instance); } */ }
/** * * * @param Object $instance * @param MappedClass $instanceClass * @ throws SerializationException */ private function serializeClass($instance, MappedClass $instanceClass) { assert($instance != null); /*MappedField[]*/ $declFields = $instanceClass->getDeclaredFields(); /*MappedField[]*/ $serializableFields = SerializabilityUtil::applyFieldSerializationPolicy($declFields); foreach ($serializableFields as $declField) { assert($declField != null); $value = null; $propName = $declField->getName(); $rClass = $instanceClass->getReflectionClass(); //$rClass = new ReflectionObject($instance); if ($rClass == null) { throw new ClassNotFoundException('MappedClass: ' . $instanceClass->getSignature() . ' do not contains ReflectionClass infomration'); } if (!$rClass->hasProperty($propName)) { throw new SerializationException('MappedClass: ' . $instanceClass->getSignature() . ' do not contains property: ' . $propName . ' Did you mapped all properties?'); } $rProperty = $rClass->getProperty($propName); if ($rProperty->isPublic()) { $value = $rProperty->getValue($instance); } else { // not public access to property, we try invoke getter method $propNameSetter = 'get' . strtoupper($propName[0]) . substr($propName, 1, strlen($propName)); if (!$rClass->hasMethod($propNameSetter)) { throw new SerializationException('MappedClass: ' . $instanceClass->getSignature() . ' do not contains getter method for private property: ' . $propName . '. Mapped object should be in pojo style?'); } $rMethod = $rClass->getMethod($propNameSetter); if ($rMethod->isPublic()) { $value = $rMethod->invoke($instance); } else { throw new SerializationException('MappedClass: ' . $instanceClass->getSignature() . ' do not contains public getter method for private property: ' . $propName . '. Mapped object should be in pojo style?'); } } $this->serializeValue($value, $declField->getType()); } /* assert (instance != null); Field[] declFields = instanceClass.getDeclaredFields(); Field[] serializableFields = SerializabilityUtil.applyFieldSerializationPolicy(declFields); for (Field declField : serializableFields) { assert (declField != null); boolean isAccessible = declField.isAccessible(); boolean needsAccessOverride = !isAccessible && !Modifier.isPublic(declField.getModifiers()); if (needsAccessOverride) { // Override the access restrictions declField.setAccessible(true); } Object value; try { value = declField.get(instance); serializeValue(value, declField.getType()); } catch (IllegalArgumentException e) { throw new SerializationException(e); } catch (IllegalAccessException e) { throw new SerializationException(e); } if (needsAccessOverride) { // Restore the access restrictions declField.setAccessible(isAccessible); } } Class<?> superClass = instanceClass.getSuperclass(); if (serializationPolicy.shouldSerializeFields(superClass)) { serializeImpl(instance, superClass); }*/ $superClass = $instanceClass->getSuperclass(); if ($superClass != null && $this->serializationPolicy->shouldDeserializeFields($superClass)) { $this->serializeImpl($instance, $superClass); } }
/** * @param MappedClass $instanceClass * @param Object $instance */ private function deserializeArray(MappedClass $instanceClass, BoundedList &$instance) { assert($instanceClass->isArray()); //assert($instance instanceof BoundedList); $s = $instanceClass->getSignature(); /*VectorReader */ $instanceReader = self::$CLASS_TO_VECTOR_READER[$s]; if ($instanceReader !== null) { return $instanceReader->read($this, $instance); } else { return VectorReader::OBJECT_VECTOR()->read($this, $instance); } }
/** * Serialize an instance that is an array. Will default to serializing the * instance as an Object vector if the instance is not a vector of primitives, * Strings or Object. * * @param MappedClass instanceClass * @param Object instance * @throws SerializationException */ private function serializeArray(MappedClass $instanceClass, $instance) { assert($instanceClass->isArray()); $s = $instanceClass->getSignature(); /*VectorWriter */ $instanceReader = self::$CLASS_TO_VECTOR_WRITER[$s]; if ($instanceReader != null) { $instanceReader->write($this, $instance, $instanceClass); } else { VectorWriter::OBJECT_VECTOR()->write($this, $instance, $instanceClass); } }