Search in sources :

Example 11 with PropertyEntity

use of php.runtime.reflection.PropertyEntity in project jphp by jphp-compiler.

the class ClassDumperTest method testComplex.

@Test
public void testComplex() throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    final ClassEntity entity = new ClassEntity(context);
    entity.setId(1);
    entity.setName("Foobar");
    entity.setData(new byte[] { 1 });
    entity.addConstant(new ConstantEntity("const1", Memory.TRUE, true));
    entity.addConstant(new ConstantEntity("const2", Memory.FALSE, true));
    entity.addConstant(new ConstantEntity("const3", Memory.NULL, true));
    entity.addProperty(new PropertyEntity(context) {

        {
            setName("prop1");
            setStatic(true);
            setDefaultValue(Memory.CONST_INT_3);
        }
    });
    entity.addProperty(new PropertyEntity(context) {

        {
            setName("prop2");
            setStatic(false);
            setDefaultValue(Memory.CONST_INT_5);
        }
    });
    entity.addMethod(new MethodEntity(context) {

        {
            setName("method1");
            setModifier(Modifier.PUBLIC);
            setClazz(entity);
        }
    }, null);
    dumper.save(entity, output);
    ClassEntity copyEntity = dumper.load(new ByteArrayInputStream(output.toByteArray()));
    Assert.assertEquals("Foobar", copyEntity.getName());
    Assert.assertArrayEquals(new byte[] { 1 }, copyEntity.getData());
    Assert.assertEquals(3, copyEntity.getConstants().size());
    Assert.assertEquals(1, copyEntity.staticProperties.size());
    Assert.assertEquals(1, copyEntity.properties.size());
    Assert.assertEquals(1, copyEntity.getMethods().size());
    Assert.assertEquals("const1", copyEntity.findConstant("const1").getName());
    Assert.assertEquals("const2", copyEntity.findConstant("const2").getName());
    Assert.assertEquals("const3", copyEntity.findConstant("const3").getName());
    Assert.assertEquals("prop1", copyEntity.staticProperties.get("prop1").getName());
    Assert.assertEquals("prop2", copyEntity.properties.get("prop2").getName());
    Assert.assertEquals("method1", copyEntity.findMethod("method1").getName());
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) PropertyEntity(php.runtime.reflection.PropertyEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) MethodEntity(php.runtime.reflection.MethodEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ConstantEntity(php.runtime.reflection.ConstantEntity) Test(org.junit.Test)

Example 12 with PropertyEntity

use of php.runtime.reflection.PropertyEntity in project jphp by jphp-compiler.

the class PrintR method printObject.

@Override
protected void printObject(ObjectMemory value, int level, Set<Integer> used) {
    ClassEntity classEntity = value.getReflection();
    writeObjectHeader(classEntity.getName());
    if (used.contains(value.getPointer())) {
        printer.write(" *RECURSION*");
    } else {
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        writeOpen();
        level += 1;
        used.add(value.getPointer());
        ArrayMemory props;
        if (env != null && classEntity.methodMagicDebugInfo != null) {
            try {
                Memory tmp = env.invokeMethod(value.value, classEntity.methodMagicDebugInfo.getName());
                if (tmp.isArray()) {
                    props = tmp.toValue(ArrayMemory.class);
                } else {
                    props = new ArrayMemory();
                }
            } catch (RuntimeException e) {
                throw e;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        } else {
            props = value.getProperties();
        }
        if (classEntity.methodMagicDebugInfo != null) {
            for (PropertyEntity entity : classEntity.getProperties()) {
                if (entity.getGetter() != null && !entity.isHiddenInDebugInfo()) {
                    printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
                    printer.write("[");
                    printer.write(entity.getName());
                    printer.write(":getter] => ");
                    try {
                        print(entity.getValue(env, TraceInfo.UNKNOWN, value.value));
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Throwable throwable) {
                        throw new RuntimeException(throwable);
                    }
                    writeSeparator(false);
                }
            }
        }
        if (props != null) {
            ForeachIterator iterator = props.foreachIterator(false, false);
            int i = 0;
            int size = classEntity.properties.size();
            while (iterator.next()) {
                Memory iteratorValue = iterator.getValue();
                if (iteratorValue.isUninitialized())
                    continue;
                printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
                Object key = iterator.getKey();
                printer.write('[');
                String realKey = key.toString();
                int pos;
                Modifier modifier = Modifier.PUBLIC;
                String className = "";
                if ((pos = realKey.lastIndexOf("\0")) > -1) {
                    if (realKey.startsWith("\0*\0")) {
                        modifier = Modifier.PROTECTED;
                    } else {
                        modifier = Modifier.PRIVATE;
                        className = realKey.substring(1, pos);
                    }
                    realKey = realKey.substring(pos + 1);
                }
                printer.write(realKey);
                switch(modifier) {
                    case PRIVATE:
                        printer.write(":" + className + ":private");
                        break;
                    case PROTECTED:
                        printer.write(":protected");
                }
                printer.write("] => ");
                print(iteratorValue, level + 1, used);
                writeSeparator(i == size - 1);
                i++;
            }
        }
        level -= 1;
        printer.write(StringUtils.repeat(' ', level * PRINT_INDENT));
        writeClose();
        used.remove(value.getPointer());
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ForeachIterator(php.runtime.lang.ForeachIterator) PropertyEntity(php.runtime.reflection.PropertyEntity) Memory(php.runtime.Memory) Modifier(php.runtime.common.Modifier)

Example 13 with PropertyEntity

use of php.runtime.reflection.PropertyEntity in project jphp by jphp-compiler.

the class Serializer method writeObject.

public void writeObject(ObjectMemory memory, Set<Integer> used) {
    if (used.add(memory.getPointer())) {
        IObject object = memory.value;
        ClassEntity reflection = object.getReflection();
        if (object instanceof Serializable) {
            Memory result;
            result = object.callMethod(env, "serialize");
            if (result.isNull()) {
                writeNull();
                return;
            }
            if (result.isString()) {
                String value = result.toString();
                printer.append("C:").append(reflection.getName().length()).append(":\"").append(reflection.getName()).append("\":").append(value.length()).append(":{").append(value).append("}");
                return;
            } else {
                env.exception(trace, reflection.getName() + "::serialize() must return a string or NULL");
            }
        }
        ArrayMemory only = null;
        if (reflection.methodMagicSleep != null) {
            env.pushCall(trace, object, reflection.methodMagicSleep.getName());
            try {
                Memory result = reflection.methodMagicSleep.invokeDynamic(object, env, trace);
                if (!result.isArray()) {
                    env.error(ErrorType.E_NOTICE, "serialize(): __sleep() should return an array only containing the names of instance-variables to serialize");
                    writeNull();
                    return;
                } else {
                    ForeachIterator iterator = result.getNewIterator(env, false, false);
                    only = new ArrayMemory(true);
                    ArrayMemory props = memory.getPropertiesForChange();
                    Set<String> need = new LinkedHashSet<String>();
                    while (iterator.next()) {
                        if (iterator.getValue().isNumber())
                            continue;
                        need.add(iterator.getValue().toString());
                    }
                    for (PropertyEntity e : reflection.getProperties()) {
                        if (need.contains(e.getName())) {
                            props.refOfIndex(e.getSpecificName());
                        } else {
                            props.removeByScalar(e.getSpecificName());
                        }
                    }
                    iterator = result.getNewIterator(env, false, false);
                    while (iterator.next()) {
                        Memory value = iterator.getValue().toValue();
                        PropertyEntity entity = reflection.findProperty(value.toString());
                        value = entity == null ? props.valueOfIndex(value).toValue() : props.valueOfIndex(entity.getSpecificName()).toValue();
                        if (value == Memory.UNDEFINED) {
                            env.error(trace, ErrorType.E_NOTICE, "serialize(): \"%s\" returned as member variable from __sleep() but does not exist", iterator.getValue().toString());
                        }
                        if (entity != null)
                            only.put(entity.getSpecificName(), value);
                        else
                            only.refOfIndex(iterator.getValue()).assign(value);
                    }
                }
            } catch (RuntimeException e) {
                throw e;
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            } finally {
                env.popCall();
            }
        }
        printer.append("O:");
        printer.append(String.valueOf(reflection.getName().length()));
        printer.append(":\"");
        printer.append(reflection.getName());
        printer.append("\":");
        if (reflection.getProperties() == null)
            writeArray(new ArrayMemory(), used, false);
        else
            writeArray(only == null ? object.getProperties() : only, used, false);
    } else
        writeNull();
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) LinkedHashSet(java.util.LinkedHashSet) Serializable(php.runtime.lang.spl.Serializable) ForeachIterator(php.runtime.lang.ForeachIterator) IObject(php.runtime.lang.IObject) PropertyEntity(php.runtime.reflection.PropertyEntity) Memory(php.runtime.Memory)

Aggregations

PropertyEntity (php.runtime.reflection.PropertyEntity)13 ClassEntity (php.runtime.reflection.ClassEntity)10 Memory (php.runtime.Memory)6 ObjectMemory (php.runtime.memory.ObjectMemory)4 ForeachIterator (php.runtime.lang.ForeachIterator)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Test (org.junit.Test)2 Modifier (php.runtime.common.Modifier)2 ICloneableObject (php.runtime.lang.support.ICloneableObject)2 IComparableObject (php.runtime.lang.support.IComparableObject)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 LinkedHashSet (java.util.LinkedHashSet)1 IObject (php.runtime.lang.IObject)1 Serializable (php.runtime.lang.spl.Serializable)1 Traversable (php.runtime.lang.spl.Traversable)1 Iterator (php.runtime.lang.spl.iterator.Iterator)1 IteratorAggregate (php.runtime.lang.spl.iterator.IteratorAggregate)1 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)1 LongMemory (php.runtime.memory.LongMemory)1