Search in sources :

Example 1 with ExecutableDescriptor

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

use of kalang.core.ExecutableDescriptor in project kalang by kasonyang.

the class StaticInvokeExpr method create.

public static StaticInvokeExpr create(ClassReference clazz, String methodName, ExprNode[] args, @Nullable ClassNode caller) throws MethodNotFoundException, AmbiguousMethodException {
    ObjectType clazzType = Types.getClassType(clazz.getReferencedClassNode());
    // TODO static only
    // TODO what about generic static method?
    MethodDescriptor[] candidates = clazzType.getMethodDescriptors(caller, true, true);
    MethodSelection ms = applyMethod(Types.getClassType(clazz.getReferencedClassNode()), methodName, args, candidates);
    ExecutableDescriptor md = ms.selectedMethod;
    if (!AstUtil.isStatic(md.getModifier())) {
        throw new MethodNotFoundException(methodName + " is not static");
    }
    return new StaticInvokeExpr(clazz, md, ms.appliedArguments);
}
Also used : ObjectType(kalang.core.ObjectType) MethodDescriptor(kalang.core.MethodDescriptor) MethodNotFoundException(kalang.MethodNotFoundException) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 3 with ExecutableDescriptor

use of kalang.core.ExecutableDescriptor in project kalang by kasonyang.

the class ObjectInvokeExpr method create.

public static ObjectInvokeExpr create(ExprNode target, String methodName, ExprNode[] args, @Nullable ClassNode caller) throws MethodNotFoundException, AmbiguousMethodException {
    ObjectType targetType = (ObjectType) target.getType();
    ClassNode clazz = targetType.getClassNode();
    boolean recursive = !"<init>".equals(methodName);
    // MethodNode[] candidates = AstUtil.listAccessibleMethods(clazz, caller , recursive);
    List<ExecutableDescriptor> candidates = new LinkedList();
    candidates.addAll(Arrays.asList(targetType.getMethodDescriptors(caller, recursive, true)));
    candidates.addAll(Arrays.asList(targetType.getConstructorDescriptors(caller)));
    MethodSelection ms = applyMethod(targetType, methodName, args, candidates.toArray(new ExecutableDescriptor[candidates.size()]));
    ExecutableDescriptor md = ms.selectedMethod;
    if (AstUtil.isStatic(md.getModifier())) {
        throw new MethodNotFoundException(methodName + " is static");
    }
    return new ObjectInvokeExpr(target, ms.selectedMethod, ms.appliedArguments);
}
Also used : ObjectType(kalang.core.ObjectType) MethodNotFoundException(kalang.MethodNotFoundException) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 4 with ExecutableDescriptor

use of kalang.core.ExecutableDescriptor in project kalang by kasonyang.

the class AstUtil method isConstructorCallStatement.

public static boolean isConstructorCallStatement(Statement stmt) {
    try {
        ExprStmt exprStmt = (ExprStmt) stmt;
        InvocationExpr invExpr = (InvocationExpr) exprStmt.getExpr();
        ExecutableDescriptor method = invExpr.getMethod();
        return method.getName().equals("<init>") && !Modifier.isStatic(method.getModifier());
    } catch (ClassCastException ex) {
        return false;
    }
}
Also used : ExprStmt(kalang.ast.ExprStmt) InvocationExpr(kalang.ast.InvocationExpr) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Aggregations

ExecutableDescriptor (kalang.core.ExecutableDescriptor)4 ObjectType (kalang.core.ObjectType)3 MethodNotFoundException (kalang.MethodNotFoundException)2 ExprNode (kalang.ast.ExprNode)1 ExprStmt (kalang.ast.ExprStmt)1 InvocationExpr (kalang.ast.InvocationExpr)1 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)1 StaticInvokeExpr (kalang.ast.StaticInvokeExpr)1 SuperExpr (kalang.ast.SuperExpr)1 MethodDescriptor (kalang.core.MethodDescriptor)1