private function makeObject(Clazz $type, $value) { // no anonymous class, and no local class in php $manualType = $type; $customSerializer = null; do { $customSerializer = SerializabilityUtil::hasCustomFieldSerializer($manualType); if ($customSerializer != null) { break; } $manualType = $manualType->getSuperClass(); } while ($manualType != null); $ins = null; if ($customSerializer != null) { $ins = $this->serializeWithCustomSerializer($customSerializer, $value, $type, $manualType); } else { $ins = new InstantiateCommand($type); $this->identityMap->put($value, $ins); } if ($type != $manualType) { if (!Classes::classOf('GWTSerializable')->isAssignableFrom($type) && !Classes::classOf('IsSerializable')->isAssignableFrom($type)) { throw new SerializationException($type->getName() . ' is not a serializable type'); } } while ($type != $manualType) { $serializableFields = $this->clientOracle->getOperableFields($type); foreach ($serializableFields as $declField) { assert($declField != null); //echo '[' . $declField->getName() . ' = ' . $declField->getType() . ']<br />'; $accessor = Accessors::get($declField->getType()); $valueCommand = null; $fieldValue = $accessor->get($value, $declField); if (is_null($fieldValue)) { $valueCommand = NullValueCommand::INSTANCE(); } else { $fieldType = $declField->getType()->isPrimitive() ? $declField->getType() : ($declField->hasType() ? $declField->getType() : Classes::classOfValue($fieldValue)); $valueCommand = $this->makeValue($fieldType, $fieldValue); } //echo '{set ' . $declField->getDeclaringClass()->getName() . ' / ' . $declField->getName() . ' / ' . $valueCommand . '}'; $ins->set($declField->getDeclaringClass(), $declField->getName(), $valueCommand); } $type = $type->getSuperClass(); } return $ins; }
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); } */ }
private function decodeCommand() { $command = $this->next(); if ($command == NL_CHAR) { $command = $this->next(); } $token = $this->token(); switch ($command) { case BOOLEAN_TYPE: $this->pushScalar(new BooleanValueCommand($token == '1')); break; case BYTE_TYPE: $this->pushScalar(new ByteValueCommand(Byte::valueOf($token))); break; case CHAR_TYPE: $this->pushScalar(new CharValueCommand(Character::chr(intval($token)))); break; case DOUBLE_TYPE: $this->pushScalar(new DoubleValueCommand(Double::valueOf($token))); break; case FLOAT_TYPE: $this->pushScalar(new FloatValueCommand(Float::valueOf($token))); break; case INT_TYPE: $this->pushScalar(new IntValueCommand(Integer::valueOf($token))); break; case LONG_TYPE: $this->pushScalar(new LongValueCommand(Long::valueOf($token))); break; case VOID_TYPE: $this->pushScalar(NullValueCommand::INSTANCE()); break; case SHORT_TYPE: $this->pushScalar(new ShortValueCommand(Short::valueOf($token))); break; case STRING_TYPE: // "4~abcd $length = Integer::valueOf($token); $value = $this->nextCount($length); if ($this->next() != RPC_SEPARATOR_CHAR) { throw new RuntimeException('Overran string'); } $this->pushString(new StringValueCommand($value)); break; case ENUM_TYPE: // ETypeSeedName~IOrdinal~ $ordinal = $this->readCommand('IntValueCommand')->getValue(); $clazz = $this->findClass($token); $x = new EnumValueCommand($clazz); $this->pushIdentity($x); $x->setValue($ordinal); break; case ARRAY_TYPE: // Encoded as (leafType, dimensions, length, ...) $leaf = $this->findClass($token); $numDims = $this->readCommand('IntValueCommand')->getValue(); $clazz = null; if ($numDims > 1) { $clazz = ArrayType::clazz($leaf, $numDims); } else { $clazz = $leaf; } $x = new ArrayValueCommand($clazz); $this->pushIdentity($x); $length = $this->readCommand('IntValueCommand')->getValue(); for ($i = 0; $i < $length; $i++) { $x->add($this->readCommand('ValueCommand')); } break; case OBJECT_TYPE: // LTypeSeedName~3... N-many setters ... $clazz = $this->findClass($token); $x = new InstantiateCommand($clazz); $this->pushIdentity($x); $this->readSetters($clazz, $x); break; case INVOKE_TYPE: // !TypeSeedName~Number of objects written by CFS~...CFS objects...~ // Number of extra fields~...N-many setters... $clazz = $this->findClass($token); $serializerClass = null; $manualType = $clazz; while ($manualType != null) { $serializerClass = SerializabilityUtil::hasCustomFieldSerializer($manualType); if ($serializerClass != null) { break; } $manualType = $manualType->getSuperClass(); } if ($serializerClass == null) { throw new IncompatibleRemoteServiceException('Class [' . $clazz->getName() . '] has no custom serializer on server'); } $x = new InvokeCustomFieldSerializerCommand($clazz, $serializerClass, $manualType); $this->pushIdentity($x); $this->readFields($x); $this->readSetters($clazz, $x); break; case RETURN_TYPE: // R4~...values... $this->toReturn = new ReturnCommand(); $toRead = Integer::valueOf($token); for ($i = 0; $i < $toRead; $i++) { $this->toReturn->addValue($this->readCommand('ValueCommand')); } break; case THROW_TYPE: // T...value... $this->toThrow = $this->readCommand('ValueCommand'); break; case BACKREF_TYPE: // @backrefNumber~ $x = $this->backRefs[Integer::valueOf($token)]; assert($x != null); array_push($this->commands, $x); break; case RPC_SEPARATOR_CHAR: throw new RuntimeException('Segmentation overrun at ' + $this->idx); default: throw new RuntimeException('Unknown Command ' + $command); } }
/** * Instantiable types are primitives, {@line IsSerializable}, types with * custom serializers, and any arrays of those types. Merely * {@link Serializable} types cannot be instantiated or serialized directly * (only as super types of legacy serializable types). * @param MappedClass $mappedClass * @return boolean */ private function isInstantiable(MappedClass $mappedClass) { if ($mappedClass->isPrimitive()) { return true; } if ($mappedClass->isArray()) { return $this->isInstantiable($mappedClass->getComponentType()); } if ($mappedClass->getReflectionClass() != null && $mappedClass->getReflectionClass()->isSubclassOf(SerializationPolicy::$IS_SERIALIZABLE_INTERFACE_CLASS_NAME)) { return true; } //if (IsSerializable.class.isAssignableFrom(clazz)) { // return true; //} return SerializabilityUtil::hasCustomFieldSerializer($mappedClass) != null; }
/** * * * @param Object $instance * @param MappedClass $instanceClass * @throws SerializationException */ private function serializeImpl($instance, MappedClass $instanceClass) { assert($instance !== null); /*ReflectionClass*/ $customSerializer = SerializabilityUtil::hasCustomFieldSerializer($instanceClass); if ($customSerializer != null) { $this->serializeWithCustomSerializer($customSerializer, $instance, $instanceClass); } else { // Arrays are serialized using custom serializers so we should never get // here for array types. // // assert (!$customSerializer->isArray()); $this->serializeClass($instance, $instanceClass); } }