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