Search in sources :

Example 11 with ObjectType

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

the class AstUtil method createEmptyConstructor.

public static boolean createEmptyConstructor(ClassNode clazzNode) {
    ObjectType supType = clazzNode.getSuperType();
    if (supType == null) {
        throw new RuntimeException("super type is null:" + clazzNode.name);
    }
    ConstructorDescriptor[] constructors = supType.getConstructorDescriptors(clazzNode);
    ConstructorDescriptor m = MethodUtil.getConstructorDescriptor(constructors, null);
    if (m != null) {
        MethodNode mm = clazzNode.createMethodNode(Types.VOID_TYPE, m.getName(), m.getModifier());
        for (Type e : m.getExceptionTypes()) mm.addExceptionType(e);
        ParameterDescriptor[] pds = m.getParameterDescriptors();
        for (ParameterDescriptor pd : pds) {
            ParameterNode p = mm.createParameter(pd.getType(), pd.getName());
            // TODO update mm.createParameter
            p.modifier = pd.getModifier();
        }
        BlockStmt body = mm.getBody();
        ParameterNode[] parameters = mm.getParameters();
        ExprNode[] params = new ExprNode[parameters.length];
        for (int i = 0; i < params.length; i++) {
            params[i] = new ParameterExpr(parameters[i]);
        }
        body.statements.add(new ExprStmt(new ObjectInvokeExpr(new SuperExpr(clazzNode), m, params)));
        return true;
    } else {
        return false;
    }
}
Also used : SuperExpr(kalang.ast.SuperExpr) ParameterExpr(kalang.ast.ParameterExpr) ParameterDescriptor(kalang.core.ParameterDescriptor) BlockStmt(kalang.ast.BlockStmt) ConstructorDescriptor(kalang.core.ConstructorDescriptor) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) 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)

Example 12 with ObjectType

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

the class BoxUtil method castPrimitive2Object.

private static ExprNode castPrimitive2Object(ExprNode expr, PrimitiveType fromType) {
    ObjectType classType = Types.getClassType(fromType);
    if (classType == null) {
        throw new UnknownError("unknown primitive type:" + fromType);
    }
    InvocationExpr inv;
    try {
        inv = StaticInvokeExpr.create(new ClassReference(classType.getClassNode()), "valueOf", new ExprNode[] { expr });
    } catch (MethodNotFoundException | AmbiguousMethodException ex) {
        throw new RuntimeException(ex);
    }
    return inv;
}
Also used : ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) ClassReference(kalang.ast.ClassReference) MethodNotFoundException(kalang.MethodNotFoundException) InvocationExpr(kalang.ast.InvocationExpr) AmbiguousMethodException(kalang.AmbiguousMethodException)

Example 13 with ObjectType

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

the class ParameterizedTypeTest method test.

@Test
public void test() throws AstNotFoundException {
    // TODO optimize
    ClassType pt = Types.getClassType(Types.getClassType("java.util.LinkedList").getClassNode(), new Type[] { Types.getIntClassType() });
    ObjectType supType = pt.getSuperType();
    assertTrue(supType instanceof ClassType);
    assertEquals("java.util.AbstractSequentialList<java.lang.Integer>", supType.toString());
// MethodDescriptor[] mds = supType.getMethodDescriptors(null, true,true);
// System.out.print(Arrays.toString(mds));
}
Also used : ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) Test(org.junit.Test)

Example 14 with ObjectType

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

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

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