Search in sources :

Example 11 with MethodEntity

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;
}
Also used : MethodEntity(php.runtime.reflection.MethodEntity)

Example 12 with MethodEntity

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());
}
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 13 with MethodEntity

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());
}
Also used : ParameterEntity(php.runtime.reflection.ParameterEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) MethodEntity(php.runtime.reflection.MethodEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 14 with MethodEntity

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();
        }
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) IObject(php.runtime.lang.IObject) Closure(php.runtime.lang.Closure) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) MethodEntity(php.runtime.reflection.MethodEntity) StringMemory(php.runtime.memory.StringMemory) CriticalException(php.runtime.exceptions.CriticalException)

Example 15 with MethodEntity

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;
}
Also used : MethodEntity(php.runtime.reflection.MethodEntity)

Aggregations

MethodEntity (php.runtime.reflection.MethodEntity)16 ClassEntity (php.runtime.reflection.ClassEntity)9 ObjectMemory (php.runtime.memory.ObjectMemory)6 Memory (php.runtime.Memory)4 ArrayMemory (php.runtime.memory.ArrayMemory)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Test (org.junit.Test)3 StringMemory (php.runtime.memory.StringMemory)3 CriticalException (php.runtime.exceptions.CriticalException)2 IObject (php.runtime.lang.IObject)2 ReferenceMemory (php.runtime.memory.ReferenceMemory)2 ParameterEntity (php.runtime.reflection.ParameterEntity)2 ClassStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.ClassStmtToken)1 MethodStmtToken (org.develnext.jphp.core.tokenizer.token.stmt.MethodStmtToken)1 WrapScopeValue (org.develnext.jphp.swing.classes.WrapScopeValue)1 Closure (php.runtime.lang.Closure)1 DumpException (php.runtime.loader.dump.io.DumpException)1 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)1 LongMemory (php.runtime.memory.LongMemory)1