Ejemplo n.º 1
0
 private static function printTypeName(Clazz $type)
 {
     // Primitives
     //
     if ($type === Integer::typeClass()) {
         return 'int';
     } else {
         if ($type === Long::typeClass()) {
             return 'long';
         } else {
             if ($type === Short::typeClass()) {
                 return 'short';
             } else {
                 if ($type === Byte::typeClass()) {
                     return 'byte';
                 } else {
                     if ($type === Character::typeClass()) {
                         return 'char';
                     } else {
                         if ($type === Boolean::typeClass()) {
                             return 'boolean';
                         } else {
                             if ($type === Float::typeClass()) {
                                 return 'float';
                             } else {
                                 if ($type === Double::typeClass()) {
                                     return 'double';
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // Arrays
     //
     if ($type->isArray()) {
         $componentType = $type->getComponentType();
         return self::printTypeName($componentType) . '[]';
     }
     // Everything else
     //
     return str_replace('$', '.', $type->getName());
 }
 private function makeArray(Clazz $type, array &$value)
 {
     $toReturn = new ArrayValueCommand($type->getComponentType());
     $this->identityMap->put($value, $toReturn);
     for ($i = 0, $j = ArrayType::getLength($value); $i < $j; $i++) {
         $arrayValue = ArrayType::get($value, $i);
         if (is_null($arrayValue)) {
             $toReturn->add(NullValueCommand::INSTANCE());
         } else {
             $valueType = $type->getComponentType()->isPrimitive() ? $type->getComponentType() : Classes::classOfValue($arrayValue);
             $toReturn->add($this->makeValue($valueType, $arrayValue));
         }
     }
     return $toReturn;
 }
 private function isInstantiable(Clazz $clazz)
 {
     if ($clazz->isPrimitive()) {
         return true;
     }
     if ($clazz->isArray()) {
         return $this->isInstantiable($clazz->getComponentType());
     }
     if (Classes::classOf('IsSerializable')->isAssignableFrom($clazz)) {
         return true;
     }
     return Serializability::hasCustomFieldSerializer($clazz) != null;
 }
 private function instantiate(Clazz $customSerializer = null, Clazz $instanceClass)
 {
     if (!is_null($customSerializer)) {
         $customFieldSerialize = SerializabilityUtilEx::loadCustomFieldSerializer($customSerializer);
         if (is_null($customFieldSerialize)) {
             foreach ($customSerializer->getMethods() as $method) {
                 if ($method->getName() === 'instantiate') {
                     return $method->invoke($this);
                 }
             }
             // Ok to not have one
         } else {
             if ($customFieldSerialize->hasCustomInstantiateInstance()) {
                 return $customFieldSerialize->instantiateInstance($this);
             }
         }
     }
     if ($instanceClass->isArray()) {
         $length = $this->readInt();
         // We don't pre-allocate the array; this prevents an allocation attack
         return new SSSR_BoundedList($instanceClass->getComponentType(), $length);
     } else {
         if ($instanceClass->isEnum()) {
             // Bypass enum transformation
             $ordinal = $this->readInt();
             $values = $instanceClass->getEnumValues();
             assert(in_array($ordinal, $values, true));
             return $ordinal;
         } else {
             $constructor = $instanceClass->getConstructor();
             $constructor->setAccessible(true);
             return $constructor->newInstance();
         }
     }
 }
 public static function jsniName(Clazz $clazz)
 {
     if ($clazz->isPrimitive()) {
         if ($clazz === Boolean::typeClass()) {
             return 'Z';
         } else {
             if ($clazz === Byte::typeClass()) {
                 return 'B';
             } else {
                 if ($clazz === Character::typeClass()) {
                     return 'C';
                 } else {
                     if ($clazz === Short::typeClass()) {
                         return 'S';
                     } else {
                         if ($clazz === Integer::typeClass()) {
                             return 'I';
                         } else {
                             if ($clazz === Long::typeClass()) {
                                 return 'J';
                             } else {
                                 if ($clazz === Float::typeClass()) {
                                     return 'F';
                                 } else {
                                     if ($clazz === Double::typeClass()) {
                                         return 'D';
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         throw new RuntimeException('Unhandled primitive tye ' + $clazz->getName());
     } else {
         if ($clazz->isArray()) {
             return '[' . self::jsniName($clazz->getComponentType());
         } else {
             return 'L' . str_replace('.', '/', $clazz->getFullName()) . ';';
         }
     }
 }