Search in sources :

Example 6 with GenericType

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

the class Ast2Class method typeSignature.

@Nullable
private String typeSignature(Type type) {
    if (type instanceof GenericType) {
        return "T" + type.getName() + ";";
    } else if (type instanceof ClassType) {
        ClassType pt = (ClassType) type;
        String ptypes = "";
        for (Type p : pt.getTypeArguments()) {
            ptypes += typeSignature(p);
        }
        if (!ptypes.isEmpty())
            ptypes = "<" + ptypes + ">";
        return "L" + pt.getClassNode().name.replace('.', '/') + ptypes + ";";
    } else if (type instanceof PrimitiveType) {
        return getTypeDescriptor(type);
    } else if (type instanceof ArrayType) {
        return "[" + typeSignature(((ArrayType) type).getComponentType());
    } else if (type instanceof WildcardType) {
        WildcardType wt = (WildcardType) type;
        Type[] lbs = wt.getLowerBounds();
        Type[] ubs = wt.getUpperBounds();
        if (lbs.length > 0) {
            // FIXME handle other lowerBounds
            return "-" + typeSignature(lbs[0]);
        } else if (ubs.length > 0) {
            // FIXME handle other lowerBounds
            return "+" + typeSignature(ubs[0]);
        } else {
            return "*";
        }
    } else {
        throw Exceptions.unsupportedTypeException(type);
    }
}
Also used : ArrayType(kalang.core.ArrayType) GenericType(kalang.core.GenericType) 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) WildcardType(kalang.core.WildcardType) PrimitiveType(kalang.core.PrimitiveType) ClassType(kalang.core.ClassType) Nullable(javax.annotation.Nullable)

Example 7 with GenericType

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

the class AstBuilder method parseClassType.

@Nullable
protected ObjectType parseClassType(KalangParser.ClassTypeContext ctx) {
    NullableKind nullable = ctx.nullable == null ? NullableKind.NONNULL : NullableKind.NULLABLE;
    Token rawTypeToken = ctx.rawClass;
    List<String> classNameParts = new LinkedList();
    for (Token p : ctx.paths) {
        classNameParts.add(p.getText());
    }
    if (ctx.innerClass != null) {
        classNameParts.add(rawTypeToken.getText() + "$" + ctx.innerClass.getText());
    } else {
        classNameParts.add(rawTypeToken.getText());
    }
    String rawType = String.join(".", classNameParts);
    for (GenericType gt : thisClazz.getGenericTypes()) {
        if (rawType.equals(gt.getName()))
            return gt;
    }
    ObjectType clazzType = requireClassType(rawType, rawTypeToken);
    if (clazzType == null)
        return null;
    ClassNode clazzNode = clazzType.getClassNode();
    GenericType[] clzDeclaredGenericTypes = clazzNode.getGenericTypes();
    List<KalangParser.ParameterizedElementTypeContext> parameterTypes = ctx.parameterTypes;
    if (parameterTypes != null && !parameterTypes.isEmpty()) {
        Type[] typeArguments = new Type[parameterTypes.size()];
        if (parameterTypes != null && parameterTypes.size() > 0) {
            if (clzDeclaredGenericTypes.length != parameterTypes.size()) {
                diagnosisReporter.report(Diagnosis.Kind.ERROR, "wrong number of type arguments", ctx);
                return null;
            }
            for (int i = 0; i < typeArguments.length; i++) {
                typeArguments[i] = parseParameterizedElementType(parameterTypes.get(i));
                // TODO should return null?
                if (typeArguments[i] == null)
                    return null;
            }
        }
        return Types.getClassType(clazzType.getClassNode(), typeArguments, nullable);
    } else {
        return Types.getClassType(clazzType.getClassNode(), nullable);
    }
}
Also used : ClassNode(kalang.ast.ClassNode) GenericType(kalang.core.GenericType) NullableKind(kalang.core.NullableKind) Token(org.antlr.v4.runtime.Token) LinkedList(java.util.LinkedList) 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) Nullable(javax.annotation.Nullable)

Example 8 with GenericType

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

the class JvmClassNode method getGenericTypeMap.

private Map<TypeVariable, GenericType> getGenericTypeMap() {
    if (this.genericTypeMap == null) {
        Map<TypeVariable, GenericType> gTypesMap = new HashMap();
        TypeVariable[] typeParameters = clazz.getTypeParameters();
        if (typeParameters.length > 0) {
            for (TypeVariable pt : typeParameters) {
                ObjectType[] bounds = castToClassTypes(transType(pt.getBounds(), gTypesMap));
                ObjectType superType;
                ObjectType[] interfaces;
                if (bounds != null && bounds.length > 0) {
                    if (ModifierUtil.isInterface(bounds[0].getModifier())) {
                        superType = Types.getRootType();
                        interfaces = bounds;
                    } else {
                        superType = bounds[0];
                        interfaces = new ObjectType[bounds.length - 1];
                        System.arraycopy(bounds, 1, interfaces, 0, interfaces.length);
                    }
                } else {
                    superType = Types.getRootType();
                    interfaces = bounds;
                }
                GenericType gt = new GenericType(pt.getName(), superType, interfaces, NullableKind.NONNULL);
                gTypesMap.put(pt, gt);
                declareGenericType(gt);
            }
        }
        this.genericTypeMap = gTypesMap;
    }
    return this.genericTypeMap;
}
Also used : ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) TypeVariable(java.lang.reflect.TypeVariable) HashMap(java.util.HashMap)

Aggregations

GenericType (kalang.core.GenericType)8 ObjectType (kalang.core.ObjectType)8 Nullable (javax.annotation.Nullable)5 ClassType (kalang.core.ClassType)5 Type (kalang.core.Type)5 PrimitiveType (kalang.core.PrimitiveType)4 WildcardType (kalang.core.WildcardType)4 ArrayType (kalang.core.ArrayType)3 TypeVariable (java.lang.reflect.TypeVariable)2 ClassNode (kalang.ast.ClassNode)2 ExprNode (kalang.ast.ExprNode)2 Token (org.antlr.v4.runtime.Token)2 GenericArrayType (java.lang.reflect.GenericArrayType)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 AmbiguousMethodException (kalang.AmbiguousMethodException)1 AstNotFoundException (kalang.AstNotFoundException)1 MethodNotFoundException (kalang.MethodNotFoundException)1 KalangParser (kalang.antlr.KalangParser)1 AssignExpr (kalang.ast.AssignExpr)1