Search in sources :

Example 6 with ClassNode

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

the class ClassNodeMetaBuilder method build.

public void build(ClassNode cn, boolean inScriptMode) {
    this.thisClazz = cn;
    astBuilder.thisClazz = cn;
    ParserRuleContext ctx = classNodeBuilder.getClassNodeDefContext(cn);
    if (ctx != null)
        visit(ctx);
    for (ClassNode c : cn.classes) {
        build(c, false);
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ClassNode(kalang.ast.ClassNode)

Example 7 with ClassNode

use of kalang.ast.ClassNode 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 8 with ClassNode

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

the class AstUtil method createArrayAst.

public static ClassNode createArrayAst(String component) {
    ClassNode clazz = new ClassNode();
    clazz.name = component + "[]";
    clazz.setSuperType(Types.getRootType());
    return clazz;
}
Also used : ClassNode(kalang.ast.ClassNode)

Example 9 with ClassNode

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

the class ClassTypeTest method test.

@Test
public void test() throws AstNotFoundException {
    AstLoader astLoader = new AstLoader();
    ClassNode listClass = astLoader.loadAst("java.util.List");
    ClassNode arrayListClass = astLoader.loadAst("java.util.ArrayList");
    Type[] paramTypes = new Type[] { Types.getRootType() };
    ObjectType listType = Types.getClassType(listClass, paramTypes);
    ArrayType listArrayType = Types.getArrayType(listType);
    ObjectType arrayListType = Types.getClassType(arrayListClass, paramTypes);
    ArrayType arrayListArrayType = Types.getArrayType(arrayListType);
    assertTrue(arrayListType.isSubTypeOf(listType));
    assertTrue(listArrayType.isAssignableFrom(arrayListArrayType));
    String mdDescriptors = Arrays.toString(listType.getMethodDescriptors(listClass, false, true));
    assertEquals("[public abstract boolean contains(java.lang.Object arg0), public abstract boolean addAll(int arg0,java.util.Collection<? extends java.lang.Object> arg1), public void sort(java.util.Comparator<? super java.lang.Object> arg0), public abstract java.util.ListIterator<java.lang.Object> listIterator(int arg0), public abstract java.util.ListIterator<java.lang.Object> listIterator(), public abstract boolean retainAll(java.util.Collection<? extends java.lang.Object> arg0), public abstract java.lang.Object[] toArray(), public abstract java.lang.Object remove(int arg0), public abstract boolean addAll(java.util.Collection<? extends java.lang.Object> arg0), public boolean removeIf(java.util.function.Predicate<? super java.lang.Object> arg0), public abstract int hashCode(), public abstract boolean removeAll(java.util.Collection<? extends java.lang.Object> arg0), public java.util.stream.Stream<java.lang.Object> parallelStream(), public abstract int lastIndexOf(java.lang.Object arg0), public void replaceAll(java.util.function.UnaryOperator<java.lang.Object> arg0), public abstract void clear(), public abstract boolean containsAll(java.util.Collection<? extends java.lang.Object> arg0), public abstract int indexOf(java.lang.Object arg0), public abstract boolean add(java.lang.Object arg0), public abstract boolean isEmpty(), public abstract java.util.List<java.lang.Object> subList(int arg0,int arg1), public abstract java.lang.Object get(int arg0), public abstract java.lang.Object set(int arg0,java.lang.Object arg1), public abstract void add(int arg0,java.lang.Object arg1), public abstract java.util.Iterator<java.lang.Object> iterator(), public java.util.Spliterator<java.lang.Object> spliterator(), public void forEach(java.util.function.Consumer<? super java.lang.Object> arg0), public abstract java.lang.Object[] toArray(java.lang.Object[] arg0), public abstract boolean remove(java.lang.Object arg0), public abstract boolean equals(java.lang.Object arg0), public java.util.stream.Stream<java.lang.Object> stream(), public abstract int size()]", mdDescriptors);
// assertTrue(listType.isSubTypeOf(Types.getRootType()));
}
Also used : ArrayType(kalang.core.ArrayType) AstLoader(kalang.compiler.AstLoader) ClassNode(kalang.ast.ClassNode) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) Type(kalang.core.Type) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) Test(org.junit.Test)

Example 10 with ClassNode

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

the class InheritanceUtil method isSubclassOf.

public static boolean isSubclassOf(ClassNode childClass, ClassNode parentClass) {
    ObjectType[] interfaces = childClass.getInterfaces();
    ObjectType superType = childClass.getSuperType();
    List<ObjectType> list = new ArrayList(interfaces.length + 1);
    list.addAll(Arrays.asList(interfaces));
    if (superType != null) {
        list.add(superType);
    }
    for (ObjectType it : list) {
        ClassNode clz = it.getClassNode();
        if (clz.equals(parentClass)) {
            return true;
        }
        if (isSubclassOf(clz, parentClass)) {
            return true;
        }
    }
    return false;
}
Also used : ObjectType(kalang.core.ObjectType) ClassNode(kalang.ast.ClassNode) ArrayList(java.util.ArrayList)

Aggregations

ClassNode (kalang.ast.ClassNode)21 ObjectType (kalang.core.ObjectType)8 Nullable (javax.annotation.Nullable)4 ClassType (kalang.core.ClassType)4 Token (org.antlr.v4.runtime.Token)4 BlockStmt (kalang.ast.BlockStmt)3 ArrayType (kalang.core.ArrayType)3 GenericType (kalang.core.GenericType)3 Type (kalang.core.Type)3 LinkedList (java.util.LinkedList)2 Nonnull (javax.annotation.Nonnull)2 AssignExpr (kalang.ast.AssignExpr)2 ConstExpr (kalang.ast.ConstExpr)2 ExprNode (kalang.ast.ExprNode)2 ExprStmt (kalang.ast.ExprStmt)2 MethodNode (kalang.ast.MethodNode)2 Statement (kalang.ast.Statement)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1