Search in sources :

Example 21 with Type

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

the class AstUtil method getUnimplementedMethod.

@Nonnull
public static List<MethodDescriptor> getUnimplementedMethod(ClassNode theClass, ObjectType theInterface) {
    ClassType theType = Types.getClassType(theClass);
    MethodDescriptor[] implementedMethods = theType.getMethodDescriptors(theClass, true, false);
    List<MethodDescriptor> list = new LinkedList();
    for (MethodDescriptor m : theInterface.getMethodDescriptors(theClass, false, true)) {
        if (ModifierUtil.isDefault(m.getModifier()))
            continue;
        String name = m.getName();
        Type[] types = m.getParameterTypes();
        MethodDescriptor overridingMd = MethodUtil.getMethodDescriptor(implementedMethods, name, types);
        if (overridingMd == null || // TODO move check to where method declare
        !OverrideUtil.overridingCompatible(overridingMd.getModifier(), m.getModifier()) || !OverrideUtil.returnTypeCompatible(overridingMd.getReturnType(), m.getReturnType()) || !OverrideUtil.exceptionTypeCompatible(overridingMd.getExceptionTypes(), m.getExceptionTypes())) {
            list.add(m);
        }
    }
    return list;
}
Also used : ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) ClassType(kalang.core.ClassType) ClassType(kalang.core.ClassType) MethodDescriptor(kalang.core.MethodDescriptor) LinkedList(java.util.LinkedList) Nonnull(javax.annotation.Nonnull)

Example 22 with Type

use of kalang.core.Type 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 23 with Type

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

the class TypeUtil method equalTypes.

public static boolean equalTypes(Type[] declaredTypes, Type[] argTypes, @Nullable Map<GenericType, Type> genericTypes) {
    if (declaredTypes.length != argTypes.length)
        return false;
    if (declaredTypes.length == 0)
        return true;
    for (int i = 0; i < declaredTypes.length; i++) {
        Type dt = declaredTypes[i];
        if (dt instanceof GenericType) {
            if (genericTypes != null) {
                dt = genericTypes.get((GenericType) dt);
                Objects.requireNonNull(dt);
            }
        }
        if (!equalType(dt, argTypes[i], genericTypes))
            return false;
    }
    return true;
}
Also used : GenericType(kalang.core.GenericType) ClassType(kalang.core.ClassType) Type(kalang.core.Type) PrimitiveType(kalang.core.PrimitiveType) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType)

Example 24 with Type

use of kalang.core.Type 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 25 with Type

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

the class Ast2Class method getMethodDescriptor.

private String getMethodDescriptor(Type returnType, Type[] parameterTypes) {
    String desc = "";
    String retTyp = getTypeDescriptor(returnType);
    if (parameterTypes != null) {
        for (Type t : parameterTypes) {
            desc += getTypeDescriptor(t);
        }
    }
    return "(" + desc + ")" + retTyp;
}
Also used : Type(kalang.core.Type) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType)

Aggregations

ObjectType (kalang.core.ObjectType)44 Type (kalang.core.Type)44 ArrayType (kalang.core.ArrayType)37 ClassType (kalang.core.ClassType)36 GenericType (kalang.core.GenericType)36 PrimitiveType (kalang.core.PrimitiveType)33 WildcardType (kalang.core.WildcardType)30 ExprNode (kalang.ast.ExprNode)17 LinkedList (java.util.LinkedList)7 ExprStmt (kalang.ast.ExprStmt)7 VarExpr (kalang.ast.VarExpr)7 Nullable (javax.annotation.Nullable)6 AssignExpr (kalang.ast.AssignExpr)6 LocalVarNode (kalang.ast.LocalVarNode)6 BlockStmt (kalang.ast.BlockStmt)5 VarDeclStmt (kalang.ast.VarDeclStmt)5 AmbiguousMethodException (kalang.AmbiguousMethodException)4 MethodNotFoundException (kalang.MethodNotFoundException)4 ConstExpr (kalang.ast.ConstExpr)4 MethodNode (kalang.ast.MethodNode)4