use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.
the class ExpressionStmtCompiler method writeSysCall.
@SuppressWarnings("unchecked")
void writeSysCall(Class clazz, int INVOKE_TYPE, String method, Class returnClazz, Class... paramClasses) {
if (INVOKE_TYPE != INVOKESPECIAL && clazz != null) {
if (compiler.getScope().isDebugMode()) {
if (!methodExists(clazz, method, paramClasses)) {
throw new NoSuchMethodException(clazz, method, paramClasses);
}
}
}
Type[] args = new Type[paramClasses.length];
if (INVOKE_TYPE == INVOKEVIRTUAL || INVOKE_TYPE == INVOKEINTERFACE)
// this
stackPop();
for (int i = 0; i < args.length; i++) {
args[i] = Type.getType(paramClasses[i]);
stackPop();
}
String owner = clazz == null ? this.method.clazz.node.name : Type.getInternalName(clazz);
if (clazz == null && this.method.clazz.entity.isTrait())
throw new CriticalException("[Compiler Error] Cannot use current classname in Trait");
code.add(new MethodInsnNode(INVOKE_TYPE, owner, method, Type.getMethodDescriptor(Type.getType(returnClazz), args), clazz != null && clazz.isInterface()));
if (returnClazz != void.class) {
stackPush(null, StackItem.Type.valueOf(returnClazz));
}
}
use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.
the class ExpressionStmtCompiler method getCompiler.
@SuppressWarnings("unchecked")
public <T extends Token> BaseStatementCompiler<T> getCompiler(Class<T> clazz) {
if (compilers == null) {
compilers = new HashMap<Class<? extends Token>, BaseStatementCompiler>();
}
BaseStatementCompiler<T> r = compilers.get(clazz);
if (r != null)
return r;
Class<? extends BaseStatementCompiler<T>> rule = (Class<? extends BaseStatementCompiler<T>>) compilerRules.get(clazz);
if (rule == null)
return null;
try {
r = rule.getConstructor(ExpressionStmtCompiler.class).newInstance(this);
compilers.put(clazz, r);
return r;
} catch (Exception e) {
throw new CriticalException(e);
}
}
use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.
the class MethodNodeImpl method duplicate.
public static MethodNodeImpl duplicate(MethodNode node) {
MethodNodeImpl self = new MethodNodeImpl();
self.name = node.name;
self.desc = node.desc;
self.access = node.access;
self.annotationDefault = node.annotationDefault;
self.attrs = node.attrs;
self.exceptions = node.exceptions;
self.tryCatchBlocks = node.tryCatchBlocks;
self.localVariables = node.localVariables;
self.instructions = node.instructions;
self.invisibleAnnotations = node.invisibleAnnotations;
self.invisibleParameterAnnotations = node.invisibleParameterAnnotations;
self.visibleAnnotations = node.visibleAnnotations;
self.visibleParameterAnnotations = node.visibleParameterAnnotations;
self.maxLocals = node.maxLocals;
self.maxStack = node.maxStack;
self.localVariables = node.localVariables;
// it's needed for copied trait method, else it will generate an error in duplicated usage
try {
Field field = node.getClass().getDeclaredField("visited");
field.setAccessible(true);
field.setBoolean(self, true);
} catch (NoSuchFieldException e) {
throw new CriticalException(e);
} catch (IllegalAccessException e) {
throw new CriticalException(e);
}
return self;
}
use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.
the class MemoryOperation method get.
@SuppressWarnings("unchecked")
public static MemoryOperation get(final Class<?> type, Type genericTypes, boolean includeParents) {
MemoryOperation operation = null;
if (genericTypes instanceof ParameterizedType) {
operation = genericOperations.get(new ParametrizedClass(type, ((ParameterizedType) genericTypes).getActualTypeArguments()));
}
if (operation == null) {
operation = operations.get(type);
if (operation == null) {
if (type.isArray()) {
MemoryOperation arrayMemoryOperation = new ArrayMemoryOperation(type);
register(arrayMemoryOperation);
return arrayMemoryOperation;
}
if (Enum.class.isAssignableFrom(type)) {
return new MemoryOperation() {
@Override
public Class<?>[] getOperationClasses() {
return new Class<?>[] { Enum.class };
}
@Override
@SuppressWarnings("unchecked")
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
return arg.isNull() ? null : Enum.valueOf((Class<? extends Enum>) type, arg.toString());
}
@Override
public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
return arg == null ? Memory.NULL : StringMemory.valueOf(((Enum) arg).name());
}
@Override
public void applyTypeHinting(ParameterEntity parameter) {
parameter.setTypeEnum((Class<? extends Enum>) type);
}
};
}
final Class<? extends BaseWrapper> wrapperClass = wrappers.get(type);
if (wrapperClass != null) {
Constructor<BaseWrapper> constructor;
try {
constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, type);
} catch (NoSuchMethodException e) {
try {
constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, Object.class);
} catch (NoSuchMethodException e1) {
throw new CriticalException(e);
}
}
final Constructor<BaseWrapper> finalConstructor = constructor;
return new MemoryOperation() {
@Override
public Class<?>[] getOperationClasses() {
return new Class<?>[0];
}
@Override
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
if (arg.isNull()) {
return null;
}
return arg.toObject(BaseWrapper.class).getWrappedObject();
}
@Override
public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
if (arg == null) {
return Memory.NULL;
}
Constructor<BaseWrapper> constructorContext = finalConstructor;
Class<? extends BaseWrapper> wrapperClassContext = wrapperClass;
if (arg.getClass() != type) {
wrapperClassContext = wrappers.get(arg.getClass());
}
if (wrapperClassContext != null && wrapperClassContext != wrapperClass) {
constructorContext = (Constructor<BaseWrapper>) wrapperClassContext.getConstructor(Environment.class, arg.getClass());
}
try {
BaseWrapper instance = constructorContext.newInstance(env, arg);
return ObjectMemory.valueOf(instance.__getOriginInstance());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new CriticalException(e);
}
}
@Override
public void applyTypeHinting(ParameterEntity parameter) {
parameter.setTypeNativeClass(type);
}
};
} else if (IObject.class.isAssignableFrom(type)) {
return new MemoryOperation() {
@Override
public Class<?>[] getOperationClasses() {
return new Class<?>[] { IObject.class };
}
@Override
@SuppressWarnings("unchecked")
public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
if (arg.isNull()) {
return null;
}
return arg.toObject((Class<? extends IObject>) type);
}
@Override
public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
if (arg == null) {
return Memory.NULL;
}
return ObjectMemory.valueOf((IObject) arg);
}
@Override
public void applyTypeHinting(ParameterEntity parameter) {
parameter.setType(ReflectionUtils.getClassName(type));
}
};
} else {
Class<?> superType = type.getSuperclass();
if (Object.class != superType && (includeParents || type.isAnonymousClass())) {
return get(superType, type.getGenericSuperclass(), includeParents);
}
}
}
}
if (operation == null) {
return null;
}
if (genericTypes instanceof ParameterizedType) {
return operation.instance(((ParameterizedType) genericTypes).getActualTypeArguments());
}
return operation;
}
use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.
the class ObjectMemory method manualUnset.
@Override
public void manualUnset(Environment env) {
ClassEntity entity = value.getReflection();
if (entity.methodDestruct != null) {
if (!value.isFinalized()) {
value.doFinalize();
env.pushCall(value, entity.methodDestruct.getName());
try {
if (value instanceof IManualDestructable) {
((IManualDestructable) value).onManualDestruct(env);
}
entity.methodDestruct.invokeDynamic(value, env);
} catch (InvocationTargetException e) {
env.__throwException(e);
} catch (RuntimeException e) {
throw e;
} catch (Throwable throwable) {
throw new CriticalException(throwable);
} finally {
env.popCall();
}
}
}
}
Aggregations