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());
}
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;
}
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;
}
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);
}
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;
}
Aggregations