use of kalang.AmbiguousMethodException 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.AmbiguousMethodException 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