Search in sources :

Example 6 with ObjectType

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

Example 7 with ObjectType

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

the class AstBuilder method getObjectFieldExpr.

@Nullable
protected ExprNode getObjectFieldExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
    ExprNode ret;
    Type type = expr.getType();
    if (!(type instanceof ObjectType)) {
        // AstBuilder.this.handleSyntaxError("unsupported type", rule==null ? ParserRuleContext.EMPTY : rule);
        return null;
    }
    ObjectType exprType = (ObjectType) type;
    if ((exprType instanceof ArrayType)) {
        return null;
    } else {
        try {
            ret = ObjectFieldExpr.create(expr, fieldName, exprType.getClassNode());
        } catch (FieldNotFoundException ex) {
            return null;
        }
    }
    if (rule != null)
        mapAst(ret, rule);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) 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) FieldNotFoundException(kalang.FieldNotFoundException) Nullable(javax.annotation.Nullable)

Example 8 with ObjectType

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

the class ClassNodeMetaBuilder method visitClassDef.

@Override
public Object visitClassDef(KalangParser.ClassDefContext ctx) {
    thisClazz.annotations.addAll(astBuilder.getAnnotations(ctx.annotation()));
    thisClazz.modifier = astBuilder.parseModifier(ctx.varModifier());
    List<Token> gnrTypes = ctx.genericTypes;
    if (gnrTypes != null && !gnrTypes.isEmpty()) {
        for (Token g : gnrTypes) {
            // TODO suport generic type bounds in syntax
            GenericType gt = new GenericType(g.getText(), Types.getRootType(), null, NullableKind.NONNULL);
            thisClazz.declareGenericType(gt);
        }
    }
    ObjectType superType = null;
    if (ctx.parentClass != null) {
        ObjectType parentClass = astBuilder.parseClassType(ctx.parentClass);
        if (parentClass != null) {
            superType = parentClass;
        }
    } else {
        superType = Types.getRootType();
    }
    if (Modifier.isInterface(thisClazz.modifier)) {
        // TODO update syntax to support:interface extends T1,T2...
        thisClazz.addInterface(superType);
    } else {
        thisClazz.setSuperType(superType);
    }
    if (ctx.interfaces != null && ctx.interfaces.size() > 0) {
        for (KalangParser.ClassTypeContext itf : ctx.interfaces) {
            ObjectType itfClz = astBuilder.parseClassType(itf);
            if (itfClz != null) {
                thisClazz.addInterface(itfClz);
            }
        }
    }
    if (this.isDeclaringNonStaticInnerClass()) {
        ClassNode parentClass = thisClazz.enclosingClass;
        if (parentClass == null) {
            throw Exceptions.unexceptedValue(parentClass);
        }
        thisClazz.createField(Types.getClassType(parentClass), "this$0", Modifier.PRIVATE | ModifierConstant.SYNTHETIC);
    }
    visit(ctx.classBody());
    if (!ModifierUtil.isInterface(thisClazz.modifier) && !AstUtil.containsConstructor(thisClazz) && !AstUtil.createEmptyConstructor(thisClazz)) {
        this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "failed to create constructor with no parameters", ctx);
    }
    MethodNode[] methods = thisClazz.getDeclaredMethodNodes();
    for (int i = 0; i < methods.length; i++) {
        MethodNode node = methods[i];
        BlockStmt body = node.getBody();
        if (body != null) {
            if (AstUtil.isConstructor(node)) {
                // constructor
                if (this.isDeclaringNonStaticInnerClass()) {
                    ClassNode enclosingClass = thisClazz.enclosingClass;
                    if (enclosingClass == null) {
                        throw Exceptions.unexceptedValue(enclosingClass);
                    }
                    ParameterNode outerInstanceParam = node.createParameter(0, Types.getClassType(enclosingClass), "this$0");
                    ExprNode parentFieldExpr = astBuilder.getObjectFieldExpr(new ThisExpr(Types.getClassType(thisClazz)), "this$0", ParserRuleContext.EMPTY);
                    if (parentFieldExpr == null) {
                        throw Exceptions.unexceptedValue(parentFieldExpr);
                    }
                    body.statements.add(1, new ExprStmt(new AssignExpr((AssignableExpr) parentFieldExpr, new ParameterExpr(outerInstanceParam))));
                }
            }
        }
    }
    for (FieldNode fieldNode : thisClazz.getFields()) {
        int mdf = fieldNode.modifier;
        if (!AstUtil.hasGetter(thisClazz, fieldNode)) {
            AstUtil.createGetter(thisClazz, fieldNode, mdf);
        }
        if (!AstUtil.hasSetter(thisClazz, fieldNode)) {
            AstUtil.createSetter(thisClazz, fieldNode, mdf);
        }
        fieldNode.modifier = ModifierUtil.setPrivate(mdf);
    }
    return null;
}
Also used : ClassNode(kalang.ast.ClassNode) GenericType(kalang.core.GenericType) FieldNode(kalang.ast.FieldNode) ParameterExpr(kalang.ast.ParameterExpr) BlockStmt(kalang.ast.BlockStmt) Token(org.antlr.v4.runtime.Token) AssignExpr(kalang.ast.AssignExpr) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) KalangParser(kalang.antlr.KalangParser) MethodNode(kalang.ast.MethodNode) ParameterNode(kalang.ast.ParameterNode) ExprStmt(kalang.ast.ExprStmt) ThisExpr(kalang.ast.ThisExpr)

Example 9 with ObjectType

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

the class JvmClassNode method transType.

// TODO why transType could be null?
@Nullable
private Type transType(java.lang.reflect.Type t, Map<TypeVariable, GenericType> genericTypes) {
    if (t instanceof TypeVariable) {
        GenericType vt = genericTypes.get((TypeVariable) t);
        // FIXME it may be null if TypeVariable comes from method
        if (vt != null) {
            return vt;
        }
        return null;
    } else if (t instanceof java.lang.reflect.ParameterizedType) {
        java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) t;
        Type rawType = transType(pt.getRawType(), genericTypes);
        if (!(rawType instanceof ObjectType)) {
            return null;
        }
        java.lang.reflect.Type[] typeArgs = pt.getActualTypeArguments();
        Type[] gTypes = transType(typeArgs, genericTypes);
        if (gTypes == null) {
            return null;
        }
        return Types.getClassType(((ObjectType) rawType).getClassNode(), gTypes);
    } else if (t instanceof java.lang.reflect.WildcardType) {
        java.lang.reflect.WildcardType wt = (java.lang.reflect.WildcardType) t;
        Type[] upperBounds = transType(wt.getUpperBounds(), genericTypes);
        if (upperBounds == null) {
            return null;
        }
        Type[] lowerBounds = transType(wt.getLowerBounds(), genericTypes);
        if (lowerBounds == null) {
            return null;
        }
        return new WildcardType(upperBounds, lowerBounds);
    } else if (t instanceof GenericArrayType) {
        GenericArrayType gt = (GenericArrayType) t;
        Type ct = transType(gt.getGenericComponentType(), genericTypes);
        if (ct == null) {
            return null;
        }
        return Types.getArrayType(ct, NullableKind.NONNULL);
    } else if (t instanceof Class) {
        Class type = (Class) t;
        if (type.isPrimitive()) {
            return Types.getPrimitiveType(type.getTypeName());
        } else if (type.isArray()) {
            Type ct = transType(type.getComponentType(), genericTypes);
            if (ct == null) {
                return null;
            }
            return Types.getArrayType(ct);
        } else {
            try {
                return Types.getClassType(this.astLoader.findAst(type));
            } catch (AstNotFoundException ex) {
                throw new RuntimeException(ex);
            }
        }
    } else {
        return null;
    }
}
Also used : GenericType(kalang.core.GenericType) GenericArrayType(java.lang.reflect.GenericArrayType) ObjectType(kalang.core.ObjectType) GenericArrayType(java.lang.reflect.GenericArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ClassType(kalang.core.ClassType) WildcardType(kalang.core.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) AstNotFoundException(kalang.AstNotFoundException) Nullable(javax.annotation.Nullable)

Example 10 with ObjectType

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

the class AstUtil method getUnimplementedMethod.

@Nonnull
public static List<MethodDescriptor> getUnimplementedMethod(ClassNode theClass, ObjectType theInterface) {
    ClassType theType = Types.getClassType(theClass);
    MethodDescriptor[] implementedMethods = theType.getMethodDescriptors(theClass, true, false);
    List<MethodDescriptor> list = new LinkedList();
    for (MethodDescriptor m : theInterface.getMethodDescriptors(theClass, false, true)) {
        if (ModifierUtil.isDefault(m.getModifier()))
            continue;
        String name = m.getName();
        Type[] types = m.getParameterTypes();
        MethodDescriptor overridingMd = MethodUtil.getMethodDescriptor(implementedMethods, name, types);
        if (overridingMd == null || // TODO move check to where method declare
        !OverrideUtil.overridingCompatible(overridingMd.getModifier(), m.getModifier()) || !OverrideUtil.returnTypeCompatible(overridingMd.getReturnType(), m.getReturnType()) || !OverrideUtil.exceptionTypeCompatible(overridingMd.getExceptionTypes(), m.getExceptionTypes())) {
            list.add(m);
        }
    }
    return list;
}
Also used : ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ClassType(kalang.core.ClassType) ClassType(kalang.core.ClassType) MethodDescriptor(kalang.core.MethodDescriptor) LinkedList(java.util.LinkedList) Nonnull(javax.annotation.Nonnull)

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