Search in sources :

Example 6 with MethodDescriptor

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

MethodDescriptor (kalang.core.MethodDescriptor)6 ClassType (kalang.core.ClassType)4 ObjectType (kalang.core.ObjectType)4 GenericType (kalang.core.GenericType)3 Type (kalang.core.Type)3 MethodNotFoundException (kalang.MethodNotFoundException)2 MethodNode (kalang.ast.MethodNode)2 ArrayType (kalang.core.ArrayType)2 LinkedList (java.util.LinkedList)1 Nonnull (javax.annotation.Nonnull)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1 KalangParser (kalang.antlr.KalangParser)1 AnnotationNode (kalang.ast.AnnotationNode)1 BlockStmt (kalang.ast.BlockStmt)1 ExprStmt (kalang.ast.ExprStmt)1 NewObjectExpr (kalang.ast.NewObjectExpr)1 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)1 ParameterExpr (kalang.ast.ParameterExpr)1 ParameterNode (kalang.ast.ParameterNode)1 ExecutableDescriptor (kalang.core.ExecutableDescriptor)1