use of php.runtime.reflection.MethodEntity in project jphp by jphp-compiler.
the class RootObject method __isset.
@Reflection.Signature({ @Reflection.Arg("name") })
public Memory __isset(Environment env, Memory... args) {
String name = args[0].toString();
MethodEntity methodEntity = __class__.findMethod("__get" + name.toLowerCase());
return methodEntity == null ? Memory.FALSE : Memory.TRUE;
}
use of php.runtime.reflection.MethodEntity 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.MethodEntity in project jphp by jphp-compiler.
the class MethodDumperTest method testWithArgs.
@Test
public void testWithArgs() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
MethodEntity entity = new MethodEntity(context);
entity.setName("foobar");
entity.setModifier(Modifier.PUBLIC);
entity.setParameters(new ParameterEntity[] { new ParameterEntity(context) {
{
setName("param1");
setType(HintType.ARRAY);
}
}, new ParameterEntity(context) {
{
setName("param2");
}
} });
dumper.save(entity, output);
MethodEntity copyEntity = dumper.load(new ByteArrayInputStream(output.toByteArray()));
Assert.assertEquals("foobar", copyEntity.getName());
Assert.assertEquals(2, copyEntity.getParameters().length);
Assert.assertEquals("param1", copyEntity.getParameters()[0].getName());
Assert.assertEquals(HintType.ARRAY, copyEntity.getParameters()[0].getType());
Assert.assertEquals("param2", copyEntity.getParameters()[1].getName());
}
use of php.runtime.reflection.MethodEntity in project jphp by jphp-compiler.
the class ObjectInvokeHelper method invokeMethod.
public static Memory invokeMethod(Memory object, String methodName, String methodLowerName, Environment env, TraceInfo trace, Memory[] args) throws Throwable {
object = object.toValue();
Memory[] passed = null;
boolean doublePop = false;
if (object.type != Memory.Type.OBJECT) {
env.error(trace, ErrorType.E_RECOVERABLE_ERROR, Messages.ERR_CANNOT_CALL_OF_NON_OBJECT.fetch(methodName));
return Memory.NULL;
}
IObject iObject = ((ObjectMemory) object).value;
ClassEntity clazz = iObject.getReflection();
MethodEntity method;
if (methodName == null) {
method = clazz.methodMagicInvoke;
} else {
method = clazz.findMethod(methodLowerName);
if (method != null && method.isContextDepends()) {
ClassEntity context = env.getLastClassOnStack();
if (context != null) {
MethodEntity contextMethod = context.findMethod(methodLowerName);
if (contextMethod != null) {
method = contextMethod;
}
}
}
if (method == null && ((method = clazz.methodMagicCall) != null)) {
clazz.methodMagicCall.setModifier(Modifier.PUBLIC);
passed = new Memory[] { new StringMemory(methodName), ArrayMemory.of(args) };
doublePop = true;
}
}
String className = clazz.getName();
if (method == null) {
if (methodName == null)
methodName = "__invoke";
env.error(trace, ErrorType.E_ERROR, Messages.ERR_CALL_TO_UNDEFINED_METHOD.fetch(className + "::" + methodName));
return Memory.NULL;
}
InvokeHelper.checkAccess(env, trace, method);
if (passed == null) {
passed = InvokeHelper.makeArguments(env, args, method.getParameters(args == null ? 0 : args.length), className, methodName, trace);
}
Memory result = method.getImmutableResult();
if (result != null) {
return result;
}
try {
if (trace != null) {
String staticClass = className;
if (iObject instanceof Closure) {
staticClass = ((Closure) iObject).getScope();
}
String stackClass = clazz.isHiddenInCallStack() ? staticClass : method.getClazz().getName();
env.pushCall(trace, iObject, args, methodName, stackClass, staticClass);
if (doublePop) {
env.pushCall(trace, iObject, passed, method.getName(), stackClass, staticClass);
}
}
return method.invokeDynamic(iObject, env, passed);
} catch (NoClassDefFoundError e) {
throw new CriticalException("Unable to call method " + className + "::" + methodName + "(), " + e.getMessage());
} finally {
if (trace != null) {
env.popCall();
if (doublePop)
env.popCall();
}
}
}
use of php.runtime.reflection.MethodEntity in project jphp by jphp-compiler.
the class RootObject method __get.
@Reflection.Signature({ @Reflection.Arg("name") })
public Memory __get(Environment env, Memory... args) throws Throwable {
String name = args[0].toString();
MethodEntity methodEntity = __class__.findMethod("__get" + name.toLowerCase());
if (methodEntity != null) {
return ObjectInvokeHelper.invokeMethod(this, methodEntity, env, env.trace(), null);
} else
env.exception(env.trace(), "getting: Unknown getting property - " + args[0].toString());
return Memory.NULL;
}
Aggregations