use of kalang.MethodNotFoundException in project kalang by kasonyang.
the class AstBuilder method visitAssertStmt.
@Override
public Object visitAssertStmt(KalangParser.AssertStmtContext ctx) {
ExprNode failExpr = visitExpression(ctx.testCondition);
if (failExpr == null)
return null;
failExpr = BoxUtil.assign(failExpr, failExpr.getType(), Types.BOOLEAN_TYPE);
if (failExpr == null) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "boolean type expected", ctx.testCondition);
return null;
}
failExpr = new UnaryExpr(failExpr, UnaryExpr.OPERATION_LOGIC_NOT);
ExprNode failMsgExpr = null;
if (ctx.failMessage != null) {
failMsgExpr = visitExpression(ctx.failMessage);
if (failMsgExpr == null)
return null;
if (Types.VOID_TYPE.equals(failMsgExpr.getType())) {
this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "non-void type expected", ctx.failMessage);
return null;
}
}
BlockStmt body = this.newBlock();
NewObjectExpr newErrorExpr;
try {
newErrorExpr = new NewObjectExpr(Types.requireAssertionErrorClassType(), failMsgExpr != null ? new ExprNode[] { failMsgExpr } : new ExprNode[0]);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
body.statements.add(new ThrowStmt(newErrorExpr));
popBlock();
return new IfStmt(failExpr, body, null);
}
use of kalang.MethodNotFoundException in project kalang by kasonyang.
the class StaticInvokeExpr method create.
public static StaticInvokeExpr create(ClassReference clazz, String methodName, ExprNode[] args, @Nullable ClassNode caller) throws MethodNotFoundException, AmbiguousMethodException {
ObjectType clazzType = Types.getClassType(clazz.getReferencedClassNode());
// TODO static only
// TODO what about generic static method?
MethodDescriptor[] candidates = clazzType.getMethodDescriptors(caller, true, true);
MethodSelection ms = applyMethod(Types.getClassType(clazz.getReferencedClassNode()), methodName, args, candidates);
ExecutableDescriptor md = ms.selectedMethod;
if (!AstUtil.isStatic(md.getModifier())) {
throw new MethodNotFoundException(methodName + " is not static");
}
return new StaticInvokeExpr(clazz, md, ms.appliedArguments);
}
use of kalang.MethodNotFoundException in project kalang by kasonyang.
the class ObjectInvokeExpr method create.
public static ObjectInvokeExpr create(ExprNode target, String methodName, ExprNode[] args, @Nullable ClassNode caller) throws MethodNotFoundException, AmbiguousMethodException {
ObjectType targetType = (ObjectType) target.getType();
ClassNode clazz = targetType.getClassNode();
boolean recursive = !"<init>".equals(methodName);
// MethodNode[] candidates = AstUtil.listAccessibleMethods(clazz, caller , recursive);
List<ExecutableDescriptor> candidates = new LinkedList();
candidates.addAll(Arrays.asList(targetType.getMethodDescriptors(caller, recursive, true)));
candidates.addAll(Arrays.asList(targetType.getConstructorDescriptors(caller)));
MethodSelection ms = applyMethod(targetType, methodName, args, candidates.toArray(new ExecutableDescriptor[candidates.size()]));
ExecutableDescriptor md = ms.selectedMethod;
if (AstUtil.isStatic(md.getModifier())) {
throw new MethodNotFoundException(methodName + " is static");
}
return new ObjectInvokeExpr(target, ms.selectedMethod, ms.appliedArguments);
}
use of kalang.MethodNotFoundException in project kalang by kasonyang.
the class AstUtil method createScriptMainMethodIfNotExists.
public static void createScriptMainMethodIfNotExists(ClassNode clazz) {
ClassType clazzType = Types.getClassType(clazz);
MethodDescriptor[] methods = clazzType.getMethodDescriptors(null, false, false);
Type[] argTypes = new Type[] { Types.getArrayType(Types.getStringClassType()) };
MethodDescriptor mainMethod = MethodUtil.getMethodDescriptor(methods, "main", argTypes);
if (mainMethod == null) {
MethodNode m = clazz.createMethodNode(Types.VOID_TYPE, "main", Modifier.PUBLIC + Modifier.STATIC);
ParameterNode p = m.createParameter(argTypes[0], "arg");
BlockStmt body = m.getBody();
try {
NewObjectExpr newScriptExpr = new NewObjectExpr(clazzType);
ObjectInvokeExpr invokeExpr = ObjectInvokeExpr.create(newScriptExpr, "run", new ExprNode[] { new ParameterExpr(p) });
body.statements.add(new ExprStmt(invokeExpr));
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
}
}
Aggregations