Search in sources :

Example 6 with ClassType

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

the class AstBuilder method getVarObjectType.

private Type getVarObjectType(VarObject p) {
    Type type = this.overrideTypes.get(p);
    if (type == null)
        type = p.getType();
    // TODO handle other object type
    if (type instanceof ClassType) {
        Integer ns = nullState.get(p);
        NullableKind nullable;
        if (ns == null) {
            nullable = ((ObjectType) type).getNullable();
        } else if (ns == NULLSTATE_MUST_NONNULL) {
            nullable = NullableKind.NONNULL;
        } else if (ns == NULLSTATE_UNKNOWN) {
            nullable = NullableKind.UNKNOWN;
        } else if (ns == NULLSTATE_MUST_NULL || ns == NULLSTATE_NULLABLE) {
            nullable = NullableKind.NULLABLE;
        } else {
            throw Exceptions.unexceptedValue(ns);
        }
        return Types.getClassType((ClassType) type, nullable);
    } else {
        return type;
    }
}
Also used : 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) NullableKind(kalang.core.NullableKind) ClassType(kalang.core.ClassType)

Example 7 with ClassType

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

the class AstBuilder method parseWildcardType.

private Type parseWildcardType(KalangParser.WildcardTypeContext ctx) {
    ObjectType classType = parseClassType(ctx.classType());
    if (classType == null)
        return null;
    Type[] bounds = new Type[] { classType };
    String boundKind = ctx.boundKind.getText();
    if (boundKind.equals("super")) {
        return new WildcardType(new Type[] { Types.getRootType() }, bounds);
    } else {
        return new WildcardType(bounds, null);
    }
}
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) WildcardType(kalang.core.WildcardType)

Example 8 with ClassType

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

the class AstUtil method hasGetter.

public static boolean hasGetter(ClassNode clazz, FieldNode field) {
    ClassType type = Types.getClassType(clazz);
    MethodDescriptor md = MethodUtil.getMethodDescriptor(type.getMethodDescriptors(clazz, true, true), "get" + NameUtil.firstCharToUpperCase(field.getName()), null);
    return md != null;
}
Also used : ClassType(kalang.core.ClassType) MethodDescriptor(kalang.core.MethodDescriptor)

Example 9 with ClassType

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

the class AstUtil method createScriptMainMethodIfNotExists.

public static void createScriptMainMethodIfNotExists(ClassNode clazz) {
    ClassType clazzType = Types.getClassType(clazz);
    MethodDescriptor[] methods = clazzType.getMethodDescriptors(null, false, false);
    Type[] argTypes = new Type[] { Types.getArrayType(Types.getStringClassType()) };
    MethodDescriptor mainMethod = MethodUtil.getMethodDescriptor(methods, "main", argTypes);
    if (mainMethod == null) {
        MethodNode m = clazz.createMethodNode(Types.VOID_TYPE, "main", Modifier.PUBLIC + Modifier.STATIC);
        ParameterNode p = m.createParameter(argTypes[0], "arg");
        BlockStmt body = m.getBody();
        try {
            NewObjectExpr newScriptExpr = new NewObjectExpr(clazzType);
            ObjectInvokeExpr invokeExpr = ObjectInvokeExpr.create(newScriptExpr, "run", new ExprNode[] { new ParameterExpr(p) });
            body.statements.add(new ExprStmt(invokeExpr));
        } catch (MethodNotFoundException | AmbiguousMethodException ex) {
            throw Exceptions.unexceptedException(ex);
        }
    }
}
Also used : ParameterExpr(kalang.ast.ParameterExpr) BlockStmt(kalang.ast.BlockStmt) NewObjectExpr(kalang.ast.NewObjectExpr) ClassType(kalang.core.ClassType) MethodDescriptor(kalang.core.MethodDescriptor) ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ClassType(kalang.core.ClassType) MethodNode(kalang.ast.MethodNode) ParameterNode(kalang.ast.ParameterNode) ExprStmt(kalang.ast.ExprStmt) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) MethodNotFoundException(kalang.MethodNotFoundException) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 10 with ClassType

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

ClassType (kalang.core.ClassType)10 ObjectType (kalang.core.ObjectType)7 ArrayType (kalang.core.ArrayType)5 GenericType (kalang.core.GenericType)5 Type (kalang.core.Type)5 MethodDescriptor (kalang.core.MethodDescriptor)4 PrimitiveType (kalang.core.PrimitiveType)3 WildcardType (kalang.core.WildcardType)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 BlockStmt (kalang.ast.BlockStmt)1 ClassNode (kalang.ast.ClassNode)1 ExprStmt (kalang.ast.ExprStmt)1 MethodNode (kalang.ast.MethodNode)1 NewObjectExpr (kalang.ast.NewObjectExpr)1 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)1