use of kalang.ast.UnknownInvocationExpr in project kalang by kasonyang.
the class AstBuilder method getObjectInvokeExpr.
@Nullable
private ExprNode getObjectInvokeExpr(ExprNode target, String methodName, ExprNode[] args, ParserRuleContext ctx) {
if ("<init>".equals(methodName)) {
throw Exceptions.unexceptedException("Don't get constructor by this method.");
}
Type targetType = target.getType();
if (!(targetType instanceof ObjectType)) {
handleSyntaxError("class type required.", ctx);
return null;
}
ObjectType targetClassType = (ObjectType) targetType;
if (targetClassType.getNullable() == NullableKind.NULLABLE) {
handleSyntaxError("expression may be null", ctx);
return null;
}
ExprNode expr;
try {
ObjectInvokeExpr invoke = ObjectInvokeExpr.create(target, methodName, args, thisClazz);
if (invoke.getMethod().getMethodNode().getType() instanceof GenericType) {
Type invokeType = invoke.getType();
if (invokeType instanceof ObjectType) {
expr = new CastExpr(invokeType, invoke);
} else {
expr = invoke;
}
} else {
expr = invoke;
}
} catch (MethodNotFoundException ex) {
methodNotFound(ctx.start, className, methodName, args);
expr = new UnknownInvocationExpr(target, methodName, args);
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.start, ex);
return null;
}
mapAst(expr, ctx);
return expr;
}
use of kalang.ast.UnknownInvocationExpr in project kalang by kasonyang.
the class AstBuilder method getImplicitInvokeExpr.
@Nullable
private ExprNode getImplicitInvokeExpr(String methodName, ExprNode[] args, ParserRuleContext ctx) {
ExprNode expr = null;
try {
ObjectType clazzType = getThisType();
InvocationExpr.MethodSelection ms = InvocationExpr.applyMethod(clazzType, methodName, args, clazzType.getMethodDescriptors(thisClazz, true, true));
if (Modifier.isStatic(ms.selectedMethod.getModifier())) {
expr = new StaticInvokeExpr(new ClassReference(thisClazz), ms.selectedMethod, ms.appliedArguments);
} else {
expr = new ObjectInvokeExpr(new ThisExpr(getThisType()), ms.selectedMethod, ms.appliedArguments);
}
} catch (MethodNotFoundException ex) {
if (args.length == 1 && (methodName.equals("print") || methodName.equals("println"))) {
try {
StaticFieldExpr fieldExpr = StaticFieldExpr.create(new ClassReference(Types.requireClassType("java.lang.System").getClassNode()), "out", null);
expr = getObjectInvokeExpr(fieldExpr, methodName, args, ctx);
} catch (FieldNotFoundException fnfEx) {
throw Exceptions.unexceptedException(fnfEx);
}
}
if (expr == null) {
this.methodNotFound(ctx.getStart(), className, methodName, args);
expr = new UnknownInvocationExpr(null, methodName, args);
}
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.start, ex);
return null;
}
mapAst(expr, ctx);
return expr;
}
use of kalang.ast.UnknownInvocationExpr in project kalang by kasonyang.
the class AstBuilder method getStaticInvokeExpr.
private ExprNode getStaticInvokeExpr(ClassReference clazz, String methodName, ExprNode[] argumentsCtx, ParserRuleContext ctx) {
ExprNode[] args = argumentsCtx;
ExprNode expr;
try {
expr = StaticInvokeExpr.create(clazz, methodName, args);
} catch (MethodNotFoundException ex) {
methodNotFound(ctx.start, className, methodName, args);
expr = new UnknownInvocationExpr(clazz, methodName, args);
} catch (AmbiguousMethodException ex) {
methodIsAmbiguous(ctx.start, ex);
return null;
}
mapAst(expr, ctx);
return expr;
}
Aggregations