Search in sources :

Example 1 with ObjectType

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

the class Ast2Class method visitMethodNode.

@Override
public Object visitMethodNode(MethodNode node) {
    int access = node.getModifier();
    md = classWriter.visitMethod(access, internalName(node.getName()), getMethodDescriptor(node), methodSignature(node), internalName(node.getExceptionTypes()));
    if (node.getType() instanceof ObjectType) {
        annotationNullable(md, (ObjectType) node.getType());
    }
    annotation(md, node.getAnnotations());
    this.methodStartLabel = new Label();
    this.methodEndLabel = new Label();
    if (AstUtil.isStatic(node.getModifier())) {
        varIdCounter = 0;
    } else {
        varIdCounter = 1;
    }
    BlockStmt body = node.getBody();
    ParameterNode[] parameters = node.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        ParameterNode p = parameters[i];
        visit(p);
        if (p.getType() instanceof ObjectType) {
            md.visitParameterAnnotation(i, getClassDescriptor(getNullableAnnotation((ObjectType) p.getType())), true).visitEnd();
        }
    }
    md.visitLabel(methodStartLabel);
    if (body != null) {
        visit(body);
        if (node.getType().equals(VOID_TYPE)) {
            md.visitInsn(RETURN);
        }
        md.visitLabel(methodEndLabel);
        try {
            md.visitMaxs(0, 0);
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        // throw new RuntimeException("exception when visit method:" + node.name, ex);
        }
    }
    md.visitEnd();
    return null;
}
Also used : ObjectType(kalang.core.ObjectType) ParameterNode(kalang.ast.ParameterNode) BlockStmt(kalang.ast.BlockStmt) Label(org.objectweb.asm.Label) AstNotFoundException(kalang.AstNotFoundException)

Example 2 with ObjectType

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

the class Ast2Class method classSignature.

@Nullable
private String classSignature(ClassNode c) {
    GenericType[] genericTypes = c.getGenericTypes();
    if (genericTypes == null || genericTypes.length == 0) {
        return null;
    }
    String gnrTypeStr = "";
    for (GenericType t : genericTypes) {
        gnrTypeStr += t.getName() + ":" + "Ljava/lang/Object;";
    }
    String superTypeStr = "";
    if (c.getSuperType() != null)
        superTypeStr += typeSignature(c.getSuperType());
    for (ObjectType itf : c.getInterfaces()) {
        superTypeStr += typeSignature(itf);
    }
    return "<" + gnrTypeStr + ">" + superTypeStr;
}
Also used : ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) Nullable(javax.annotation.Nullable)

Example 3 with ObjectType

use of kalang.core.ObjectType 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 4 with ObjectType

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

the class AstBuilder method onAssign.

private void onAssign(ExprNode to, ExprNode expr) {
    removeOverrideType(to);
    if (to instanceof VarExpr) {
        ((VarExpr) to).removeOverrideType();
    } else if (to instanceof ParameterExpr) {
        ((ParameterExpr) to).removeOverrideType();
    }
    VarObject key = getOverrideTypeKey(to);
    if (key != null) {
        Type toType = to.getType();
        if (toType instanceof ObjectType) {
            Type type = expr.getType();
            int ns;
            if (Types.NULL_TYPE.equals(type)) {
                ns = NULLSTATE_MUST_NULL;
            } else if (type instanceof ObjectType) {
                ns = getNullState(((ObjectType) type).getNullable());
            } else {
                throw Exceptions.unexceptedValue(type);
            }
            nullState.put(key, ns);
        }
    }
}
Also used : ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) ParameterExpr(kalang.ast.ParameterExpr) VarExpr(kalang.ast.VarExpr) VarObject(kalang.ast.VarObject)

Example 5 with ObjectType

use of kalang.core.ObjectType 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;
}
Also used : ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) PrimitiveCastExpr(kalang.ast.PrimitiveCastExpr) CastExpr(kalang.ast.CastExpr) MethodNotFoundException(kalang.MethodNotFoundException) UnknownInvocationExpr(kalang.ast.UnknownInvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException) Nullable(javax.annotation.Nullable)

Aggregations

ObjectType (kalang.core.ObjectType)32 GenericType (kalang.core.GenericType)15 ClassType (kalang.core.ClassType)14 Type (kalang.core.Type)14 ExprNode (kalang.ast.ExprNode)11 ArrayType (kalang.core.ArrayType)11 PrimitiveType (kalang.core.PrimitiveType)9 WildcardType (kalang.core.WildcardType)9 MethodNotFoundException (kalang.MethodNotFoundException)8 ClassNode (kalang.ast.ClassNode)7 Nullable (javax.annotation.Nullable)6 AmbiguousMethodException (kalang.AmbiguousMethodException)6 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)6 LinkedList (java.util.LinkedList)4 FieldNotFoundException (kalang.FieldNotFoundException)4 BlockStmt (kalang.ast.BlockStmt)4 ThisExpr (kalang.ast.ThisExpr)4 ExprStmt (kalang.ast.ExprStmt)3 InvocationExpr (kalang.ast.InvocationExpr)3 MethodNode (kalang.ast.MethodNode)3