Search in sources :

Example 1 with StaticInvokeExpr

use of kalang.ast.StaticInvokeExpr in project kalang by kasonyang.

the class Ast2Class method visitInvocationExpr.

@Override
public Object visitInvocationExpr(InvocationExpr node) {
    int opc;
    ExecutableDescriptor method = node.getMethod();
    // = internalName(node.getMethod().classNode);
    String ownerClass;
    if (node instanceof StaticInvokeExpr) {
        opc = INVOKESTATIC;
        ownerClass = internalName(((StaticInvokeExpr) node).getInvokeClass().getReferencedClassNode());
    } else if (node instanceof ObjectInvokeExpr) {
        ObjectInvokeExpr oie = (ObjectInvokeExpr) node;
        ObjectType targetType = (ObjectType) oie.getInvokeTarget().getType();
        ownerClass = internalName(targetType);
        ExprNode target = oie.getInvokeTarget();
        visit(target);
        if (Modifier.isPrivate(method.getModifier()) || (target instanceof SuperExpr) || method.getName().equals("<init>")) {
            opc = INVOKESPECIAL;
        } else {
            opc = ModifierUtil.isInterface(targetType.getClassNode().modifier) ? INVOKEINTERFACE : INVOKEVIRTUAL;
        }
    } else {
        throw Exceptions.unsupportedTypeException(node);
    }
    visitAll(node.getArguments());
    md.visitMethodInsn(opc, ownerClass, method.getName(), getMethodDescriptor(method.getMethodNode()));
    return null;
}
Also used : StaticInvokeExpr(kalang.ast.StaticInvokeExpr) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) SuperExpr(kalang.ast.SuperExpr) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 2 with StaticInvokeExpr

use of kalang.ast.StaticInvokeExpr 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;
}
Also used : FieldNotFoundException(kalang.FieldNotFoundException) ExprNode(kalang.ast.ExprNode) StaticInvokeExpr(kalang.ast.StaticInvokeExpr) ObjectType(kalang.core.ObjectType) StaticFieldExpr(kalang.ast.StaticFieldExpr) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) ClassReference(kalang.ast.ClassReference) MethodNotFoundException(kalang.MethodNotFoundException) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) InvocationExpr(kalang.ast.InvocationExpr) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) ThisExpr(kalang.ast.ThisExpr) AmbiguousMethodException(kalang.AmbiguousMethodException) Nullable(javax.annotation.Nullable)

Aggregations

ExprNode (kalang.ast.ExprNode)2 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)2 StaticInvokeExpr (kalang.ast.StaticInvokeExpr)2 ObjectType (kalang.core.ObjectType)2 Nullable (javax.annotation.Nullable)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1 FieldNotFoundException (kalang.FieldNotFoundException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 ClassReference (kalang.ast.ClassReference)1 InvocationExpr (kalang.ast.InvocationExpr)1 StaticFieldExpr (kalang.ast.StaticFieldExpr)1 SuperExpr (kalang.ast.SuperExpr)1 ThisExpr (kalang.ast.ThisExpr)1 UnknownInvocationExpr (kalang.ast.UnknownInvocationExpr)1 ExecutableDescriptor (kalang.core.ExecutableDescriptor)1