Search in sources :

Example 11 with MethodNotFoundException

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);
}
Also used : ExprNode(kalang.ast.ExprNode) IfStmt(kalang.ast.IfStmt) BlockStmt(kalang.ast.BlockStmt) NewObjectExpr(kalang.ast.NewObjectExpr) UnaryExpr(kalang.ast.UnaryExpr) MethodNotFoundException(kalang.MethodNotFoundException) ThrowStmt(kalang.ast.ThrowStmt) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 12 with MethodNotFoundException

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);
}
Also used : ObjectType(kalang.core.ObjectType) MethodDescriptor(kalang.core.MethodDescriptor) MethodNotFoundException(kalang.MethodNotFoundException) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 13 with MethodNotFoundException

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);
}
Also used : ObjectType(kalang.core.ObjectType) MethodNotFoundException(kalang.MethodNotFoundException) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 14 with MethodNotFoundException

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);
        }
    }
}
Also used : ParameterExpr(kalang.ast.ParameterExpr) BlockStmt(kalang.ast.BlockStmt) NewObjectExpr(kalang.ast.NewObjectExpr) ClassType(kalang.core.ClassType) MethodDescriptor(kalang.core.MethodDescriptor) ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ClassType(kalang.core.ClassType) MethodNode(kalang.ast.MethodNode) ParameterNode(kalang.ast.ParameterNode) ExprStmt(kalang.ast.ExprStmt) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) MethodNotFoundException(kalang.MethodNotFoundException) AmbiguousMethodException(kalang.AmbiguousMethodException)

Aggregations

MethodNotFoundException (kalang.MethodNotFoundException)14 AmbiguousMethodException (kalang.AmbiguousMethodException)12 ObjectType (kalang.core.ObjectType)10 ExprNode (kalang.ast.ExprNode)9 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)5 UnknownInvocationExpr (kalang.ast.UnknownInvocationExpr)5 BlockStmt (kalang.ast.BlockStmt)4 InvocationExpr (kalang.ast.InvocationExpr)4 NewObjectExpr (kalang.ast.NewObjectExpr)4 ArrayType (kalang.core.ArrayType)4 ClassType (kalang.core.ClassType)4 GenericType (kalang.core.GenericType)4 Type (kalang.core.Type)4 ExprStmt (kalang.ast.ExprStmt)3 PrimitiveType (kalang.core.PrimitiveType)3 WildcardType (kalang.core.WildcardType)3 LinkedList (java.util.LinkedList)2 Nullable (javax.annotation.Nullable)2 AssignExpr (kalang.ast.AssignExpr)2 CastExpr (kalang.ast.CastExpr)2