Search in sources :

Example 6 with ParameterExpr

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

ParameterExpr (kalang.ast.ParameterExpr)6 ParameterNode (kalang.ast.ParameterNode)5 BlockStmt (kalang.ast.BlockStmt)4 ExprNode (kalang.ast.ExprNode)4 ExprStmt (kalang.ast.ExprStmt)4 MethodNode (kalang.ast.MethodNode)4 GenericType (kalang.core.GenericType)4 ObjectType (kalang.core.ObjectType)4 ThisExpr (kalang.ast.ThisExpr)3 ArrayType (kalang.core.ArrayType)3 ClassType (kalang.core.ClassType)3 Type (kalang.core.Type)3 AssignExpr (kalang.ast.AssignExpr)2 ClassReference (kalang.ast.ClassReference)2 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)2 VarExpr (kalang.ast.VarExpr)2 Nullable (javax.annotation.Nullable)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 KalangParser (kalang.antlr.KalangParser)1