Search in sources :

Example 1 with ClassEntity

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

the class ModuleOpcodePrinter method toOutput.

public void toOutput(Writer writer, int flags) {
    try {
        OpcodePrinter opcodePrinter = new OpcodePrinter(module);
        writer.write("#### Module class: " + module.getContext().getModuleName() + "\n");
        opcodePrinter.toWriter(writer, flags);
        writer.write("#### /Module class \n\n\n");
        for (ClosureEntity closure : module.getClosures()) {
            opcodePrinter = new OpcodePrinter(closure);
            writer.write("#### Closure: " + closure.getInternalName() + "\n");
            opcodePrinter.toWriter(writer);
            writer.write("#### /Closure \n\n\n");
        }
        for (GeneratorEntity generator : module.getGenerators()) {
            opcodePrinter = new OpcodePrinter(generator);
            writer.write("#### Generator: " + generator.getInternalName() + "\n");
            opcodePrinter.toWriter(writer);
            writer.write("#### /Generator \n\n\n");
        }
        for (ClassEntity clazz : module.getClasses()) {
            if (clazz.isClass() || clazz.isTrait()) {
                opcodePrinter = new OpcodePrinter(clazz);
                writer.write("#### " + (clazz.isTrait() ? "Trait" : "Class") + ": " + clazz.getName() + "\n");
                opcodePrinter.toWriter(writer);
                writer.write("#### /" + (clazz.isTrait() ? "Trait" : "Class") + " \n\n\n");
            }
        }
        for (FunctionEntity function : module.getFunctions()) {
            opcodePrinter = new OpcodePrinter(function);
            writer.write("#### Function: " + function.getName() + "\n");
            opcodePrinter.toWriter(writer);
            writer.write("#### /Function \n\n\n");
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) GeneratorEntity(php.runtime.reflection.helper.GeneratorEntity) FunctionEntity(php.runtime.reflection.FunctionEntity) ClosureEntity(php.runtime.reflection.helper.ClosureEntity)

Example 2 with ClassEntity

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

the class JvmCompilerCase method run.

protected Memory run(String code, boolean returned) {
    runIndex += 1;
    Environment environment = new Environment(newScope());
    code = "class TestClass { static function test(){ " + (returned ? "return " : "") + code + "; } }";
    Context context = new Context(code);
    JvmCompiler compiler = new JvmCompiler(environment, context, getSyntax(context));
    ModuleEntity module = compiler.compile();
    environment.getScope().loadModule(module);
    ClassEntity entity = module.findClass("TestClass");
    try {
        return entity.findMethod("test").invokeStatic(environment);
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ModuleEntity(php.runtime.reflection.ModuleEntity)

Example 3 with ClassEntity

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

the class JavaReflection method exception.

public static void exception(Environment env, Throwable e) {
    if (cachedThClasses == null)
        cachedThClasses = new ConcurrentHashMap<Class<? extends Throwable>, Class<? extends JavaException>>();
    if (constructors == null)
        constructors = new ConcurrentHashMap<Class<? extends JavaException>, Constructor<? extends JavaException>>();
    Class tmp = e.getClass();
    Class<? extends JavaException> clazz = cachedThClasses.get(tmp);
    if (clazz == null) {
        while (tmp != null) {
            clazz = env.scope.findJavaException(tmp);
            if (clazz != null) {
                cachedThClasses.put(e.getClass(), clazz);
                break;
            }
            tmp = tmp.getSuperclass();
        }
    }
    if (clazz == null) {
        ClassEntity context = env.getLastClassOnStack();
        while (context != null) {
            clazz = env.scope.findJavaExceptionForContext(context.getLowerName());
            if (clazz != null)
                break;
            context = context.getParent();
        }
    }
    JavaException exception;
    if (clazz == null) {
        exception = new JavaException(env, e);
    } else {
        try {
            Constructor<? extends JavaException> constructor = constructors.get(clazz);
            if (constructor == null) {
                constructors.put(clazz, constructor = clazz.getConstructor(Environment.class, Throwable.class));
            }
            exception = constructor.newInstance(env, e);
        } catch (InvocationTargetException e1) {
            throw new CriticalException(e1.getCause());
        } catch (Exception e1) {
            throw new CriticalException(e1);
        }
    }
    env.__throwException(exception, false);
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CriticalException(php.runtime.exceptions.CriticalException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CriticalException(php.runtime.exceptions.CriticalException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with ClassEntity

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

the class SPLFunctions method class_implements.

public static Memory class_implements(Environment env, TraceInfo trace, Memory object, boolean autoLoad) {
    ClassEntity entity;
    if (object.isObject()) {
        entity = object.toValue(ObjectMemory.class).getReflection();
    } else {
        entity = env.fetchClass(object.toString(), autoLoad);
    }
    if (entity == null) {
        env.warning(trace, "class_implements(): Class %s does not exist and could not be loaded", object.toString());
        return Memory.FALSE;
    }
    ArrayMemory result = new ArrayMemory();
    do {
        for (ClassEntity el : entity.getInterfaces().values()) {
            result.refOfIndex(el.getName()).assign(el.getName());
        }
        entity = entity.getParent();
        if (entity == null)
            break;
    } while (true);
    return result.toConstant();
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) ArrayMemory(php.runtime.memory.ArrayMemory)

Example 5 with ClassEntity

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

the class DynamicMethodInvoker method valueOf.

public static DynamicMethodInvoker valueOf(Environment env, TraceInfo trace, IObject object, String methodName) {
    String methodNameL = methodName.toLowerCase();
    int pos;
    MethodEntity methodEntity;
    if ((pos = methodName.indexOf("::")) > -1) {
        String className = methodNameL.substring(0, pos);
        methodNameL = methodNameL.substring(pos + 2);
        ClassEntity classEntity = object.getReflection();
        ClassEntity clazz = env.fetchClass(methodName.substring(0, pos), className, false);
        if (!classEntity.isInstanceOf(clazz)) {
            return null;
        }
        methodEntity = clazz.findMethod(methodNameL);
    } else
        methodEntity = object.getReflection().findMethod(methodNameL);
    if (methodEntity == null) {
        if (object.getReflection().methodMagicCall != null) {
            return new MagicDynamicMethodInvoker(env, trace, object, object.getReflection().methodMagicCall, methodName);
        }
        if (trace == null) {
            return null;
        }
        env.error(trace, Messages.ERR_CALL_TO_UNDEFINED_METHOD.fetch(object.getReflection().getName() + "::" + methodName));
    }
    return new DynamicMethodInvoker(env, trace, object, methodEntity);
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) MethodEntity(php.runtime.reflection.MethodEntity)

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