use of kalang.ast.ObjectInvokeExpr in project kalang by kasonyang.
the class AstBuilder method visitMemberInvocationExpr.
@Override
public ExprNode visitMemberInvocationExpr(MemberInvocationExprContext ctx) {
String methodName;
ExprNode target;
ObjectType clazz;
if (ctx.key != null) {
methodName = ctx.key.getText();
} else {
methodName = ctx.Identifier().getText();
}
if (methodName.equals("this")) {
methodName = "<init>";
target = new ThisExpr(this.getThisType());
clazz = this.getThisType();
} else if (methodName.equals("super")) {
methodName = "<init>";
target = new SuperExpr(thisClazz);
clazz = thisClazz.getSuperType();
} else {
target = new ThisExpr(this.getThisType());
clazz = this.getThisType();
}
List<Object> argsList = visitAll(ctx.params);
if (argsList.contains(null))
return null;
ExprNode[] args = argsList.toArray(new ExprNode[argsList.size()]);
ExprNode ie;
if (methodName.equals("<init>")) {
if (clazz == null)
throw Exceptions.unexceptedValue(clazz);
try {
InvocationExpr.MethodSelection apply = InvocationExpr.applyMethod(clazz, methodName, args, clazz.getConstructorDescriptors(thisClazz));
ie = new ObjectInvokeExpr(target, apply.selectedMethod, apply.appliedArguments);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
this.methodNotFound(ctx.start, clazz.getName(), methodName, args);
return null;
}
} else {
ie = getImplicitInvokeExpr(methodName, args, ctx);
}
return ie;
}
use of kalang.ast.ObjectInvokeExpr 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