Search in sources :

Example 31 with ObjectType

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

Example 32 with ObjectType

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

the class Ast2Java method visitClassNode.

@Override
public String visitClassNode(ClassNode node) {
    cls = node;
    // visit(node.imports).join("\r\n")
    String imports = "";
    String fs = "";
    String mdf = visitModifier(node.modifier);
    String pkg = "";
    String name = node.name;
    int dotIdx = name.lastIndexOf('.');
    if (dotIdx > 0) {
        pkg = name.substring(0, dotIdx);
        name = name.substring(dotIdx + 1);
    }
    String pkgStr = pkg.length() > 0 ? "package " + pkg + ";" : "";
    String parentStr = "";
    if (node.getSuperType() != null) {
        parentStr = "extends " + node.getSuperType().getName();
    }
    String impStr = "";
    if (node.getInterfaces().length > 0) {
        List<String> interfaces = new LinkedList();
        for (ObjectType itf : node.getInterfaces()) {
            interfaces.add(itf.getName());
        }
        impStr = "implements " + String.join(",", interfaces);
    }
    String classType = Modifier.isInterface(node.modifier) ? "interface" : "class";
    c(pkgStr + "\r\n" + imports + "\r\n" + mdf + " " + classType + " " + name + " " + parentStr + " " + impStr + " {");
    for (FieldNode f : node.getFields()) {
        c(this.getVarStr(f) + ";\r\n");
    }
    visitAll(Arrays.asList(node.getDeclaredMethodNodes()));
    c("}");
    return null;
}
Also used : ObjectType(kalang.core.ObjectType) LinkedList(java.util.LinkedList)

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