Search in sources :

Example 16 with ClassNode

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

the class JavaAstLoader method buildFromClass.

/**
 * build ast from java class
 *
 * @param clz the java class
 * @return the ast built from java class
 * @throws AstNotFoundException
 */
@Nonnull
public ClassNode buildFromClass(@Nonnull Class clz) throws AstNotFoundException {
    ClassNode cn = new JvmClassNode(clz, this);
    loadedClasses.put(clz.getName(), cn);
    return cn;
}
Also used : ClassNode(kalang.ast.ClassNode) Nonnull(javax.annotation.Nonnull)

Example 17 with ClassNode

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

the class JavaAstLoader method findAst.

public ClassNode findAst(Class clazz) throws AstNotFoundException {
    String name = clazz.getName();
    ClassNode ast = loadedClasses.get(name);
    if (ast != null) {
        return ast;
    }
    return buildFromClass(clazz);
}
Also used : ClassNode(kalang.ast.ClassNode)

Example 18 with ClassNode

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

the class KalangCompiler method findAst.

@Override
protected ClassNode findAst(@Nonnull String className) throws AstNotFoundException {
    String[] classNameInfo = className.split("\\$", 2);
    String topClassName = classNameInfo[0];
    if (compilationUnits.containsKey(topClassName)) {
        CompilationUnit compilationUnit = compilationUnits.get(topClassName);
        ClassNode clazz = compilationUnit.getAst();
        if (classNameInfo.length == 1) {
            return clazz;
        } else {
            ClassNode c = findInnerClass(clazz, className);
            if (c != null) {
                return c;
            }
        }
    }
    SourceLoader srcLoader = getSourceLoader();
    if (srcLoader != null) {
        KalangSource source = srcLoader.loadSource(className);
        if (source != null) {
            return createCompilationUnit(source).getAst();
        }
    }
    return super.findAst(className);
}
Also used : ClassNode(kalang.ast.ClassNode) AntlrErrorString(kalang.util.AntlrErrorString)

Example 19 with ClassNode

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

the class AstUtil method createClassNodeWithInterfaces.

public static ClassNode createClassNodeWithInterfaces(String name, @Nullable ObjectType superType, @Nullable ObjectType... interfaces) {
    ClassNode cn = new ClassNode();
    cn.name = name;
    cn.setSuperType(superType == null ? Types.getRootType() : superType);
    if (interfaces != null) {
        for (ObjectType itf : interfaces) cn.addInterface(itf);
    }
    return cn;
}
Also used : ClassNode(kalang.ast.ClassNode) ObjectType(kalang.core.ObjectType)

Example 20 with ClassNode

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

the class ObjectTypeTest method test.

@Test
public void test() throws AstNotFoundException {
    ClassType type = Types.getClassType(ObjectTypeTest.class.getName());
    ClassNode classNode = type.getClassNode();
    ObjectType rootType = Types.getRootType();
    ClassNode rootClassNode = rootType.getClassNode();
    assertEquals(null, type.getFieldDescriptor(rootClassNode, "privateField"));
    assertEquals(null, type.getFieldDescriptor(rootClassNode, "protectedField"));
    assertNotEquals(null, type.getFieldDescriptor(rootClassNode, "publicField"));
    assertNotEquals(null, type.getFieldDescriptor(classNode, "privateField"));
    assertNotEquals(null, type.getFieldDescriptor(classNode, "protectedField"));
    assertNotEquals(null, type.getFieldDescriptor(classNode, "publicField"));
}
Also used : ClassNode(kalang.ast.ClassNode) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) Test(org.junit.Test)

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