Search in sources :

Example 31 with ClassEntity

use of php.runtime.reflection.ClassEntity 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.constants.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 32 with ClassEntity

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

the class JsonProcessor method onClassSerialize.

@Signature({ @Arg("className"), @Arg(value = "callback", type = HintType.CALLABLE, optional = @Optional("null")) })
public Memory onClassSerialize(Environment env, Memory... args) {
    ClassEntity entity = env.fetchClass(args[0].toString(), true);
    if (entity == null)
        throw new IllegalArgumentException("Class not found - " + args[0]);
    MemorySerializer.Handler handler = null;
    if (!args[1].isNull()) {
        final Invoker invoker = Invoker.valueOf(env, env.trace(), args[1]);
        handler = new MemorySerializer.Handler() {

            @Override
            public Memory call(Environment env, Memory value) {
                return invoker.callNoThrow(value);
            }
        };
    }
    memorySerializer.setClassHandler(entity.getName(), handler);
    return Memory.NULL;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) MemorySerializer(org.develnext.jphp.json.gson.MemorySerializer) Invoker(php.runtime.invoke.Invoker) Memory(php.runtime.Memory) Environment(php.runtime.env.Environment)

Example 33 with ClassEntity

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

the class ReflectionFunction method getParameters.

@Override
@Signature
public Memory getParameters(Environment env, Memory... args) {
    if (cachedParameters != null)
        return cachedParameters;
    if (functionEntity instanceof CompileFunctionEntity)
        exception(env, "Cannot get parameters for internal function %s()", functionEntity.getName());
    ParameterEntity[] parameters = closureEntity == null ? functionEntity.getParameters() : closureEntity.parameters;
    ClassEntity entity = env.fetchClass("ReflectionParameter");
    ArrayMemory result = new ArrayMemory();
    int i = 0;
    for (ParameterEntity param : parameters) {
        ReflectionParameter e = new ReflectionParameter(env, entity);
        e.setEntity(param);
        e.setFunctionEntity(functionEntity);
        e.setPosition(i);
        i++;
        result.add(new ObjectMemory(e));
    }
    return cachedParameters = result;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory) ParameterEntity(php.runtime.reflection.ParameterEntity) ObjectMemory(php.runtime.memory.ObjectMemory) CompileFunctionEntity(php.runtime.reflection.CompileFunctionEntity)

Example 34 with ClassEntity

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

the class ReflectionMethod method getPrototype.

@Signature
public Memory getPrototype(Environment env, Memory... args) {
    if (methodEntity.getPrototype() == null)
        return Memory.NULL;
    ClassEntity classEntity = env.fetchClass("ReflectionMethod");
    ReflectionMethod r = new ReflectionMethod(env, classEntity);
    r.setEntity(methodEntity.getPrototype());
    return new ObjectMemory(r);
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ObjectMemory(php.runtime.memory.ObjectMemory)

Example 35 with ClassEntity

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

the class ReflectionMethod method getParameters.

@Override
@Signature
public Memory getParameters(Environment env, Memory... args) {
    if (cachedParameters != null)
        return cachedParameters;
    ParameterEntity[] parameters = methodEntity.getParameters(Integer.MAX_VALUE);
    ClassEntity entity = env.fetchClass("ReflectionParameter");
    ArrayMemory result = new ArrayMemory();
    int i = 0;
    for (ParameterEntity param : parameters) {
        ReflectionParameter e = new ReflectionParameter(env, entity);
        e.setEntity(param);
        e.setFunctionEntity(methodEntity);
        e.setPosition(i);
        i++;
        result.add(new ObjectMemory(e));
    }
    return cachedParameters = result;
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory) ParameterEntity(php.runtime.reflection.ParameterEntity) ObjectMemory(php.runtime.memory.ObjectMemory)

Aggregations

ClassEntity (php.runtime.reflection.ClassEntity)54 ObjectMemory (php.runtime.memory.ObjectMemory)21 Memory (php.runtime.Memory)14 ArrayMemory (php.runtime.memory.ArrayMemory)14 PropertyEntity (php.runtime.reflection.PropertyEntity)10 MethodEntity (php.runtime.reflection.MethodEntity)9 StringMemory (php.runtime.memory.StringMemory)8 ConstantEntity (php.runtime.reflection.ConstantEntity)5 FunctionEntity (php.runtime.reflection.FunctionEntity)5 CriticalException (php.runtime.exceptions.CriticalException)4 Closure (php.runtime.lang.Closure)4 IObject (php.runtime.lang.IObject)4 ReferenceMemory (php.runtime.memory.ReferenceMemory)4 ClosureEntity (php.runtime.reflection.helper.ClosureEntity)4 GeneratorEntity (php.runtime.reflection.helper.GeneratorEntity)4 ForeachIterator (php.runtime.lang.ForeachIterator)3 ModuleEntity (php.runtime.reflection.ModuleEntity)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2