Search in sources :

Example 1 with WildcardType

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

the class JvmClassNode method transType.

// TODO why transType could be null?
@Nullable
private Type transType(java.lang.reflect.Type t, Map<TypeVariable, GenericType> genericTypes) {
    if (t instanceof TypeVariable) {
        GenericType vt = genericTypes.get((TypeVariable) t);
        // FIXME it may be null if TypeVariable comes from method
        if (vt != null) {
            return vt;
        }
        return null;
    } else if (t instanceof java.lang.reflect.ParameterizedType) {
        java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) t;
        Type rawType = transType(pt.getRawType(), genericTypes);
        if (!(rawType instanceof ObjectType)) {
            return null;
        }
        java.lang.reflect.Type[] typeArgs = pt.getActualTypeArguments();
        Type[] gTypes = transType(typeArgs, genericTypes);
        if (gTypes == null) {
            return null;
        }
        return Types.getClassType(((ObjectType) rawType).getClassNode(), gTypes);
    } else if (t instanceof java.lang.reflect.WildcardType) {
        java.lang.reflect.WildcardType wt = (java.lang.reflect.WildcardType) t;
        Type[] upperBounds = transType(wt.getUpperBounds(), genericTypes);
        if (upperBounds == null) {
            return null;
        }
        Type[] lowerBounds = transType(wt.getLowerBounds(), genericTypes);
        if (lowerBounds == null) {
            return null;
        }
        return new WildcardType(upperBounds, lowerBounds);
    } else if (t instanceof GenericArrayType) {
        GenericArrayType gt = (GenericArrayType) t;
        Type ct = transType(gt.getGenericComponentType(), genericTypes);
        if (ct == null) {
            return null;
        }
        return Types.getArrayType(ct, NullableKind.NONNULL);
    } else if (t instanceof Class) {
        Class type = (Class) t;
        if (type.isPrimitive()) {
            return Types.getPrimitiveType(type.getTypeName());
        } else if (type.isArray()) {
            Type ct = transType(type.getComponentType(), genericTypes);
            if (ct == null) {
                return null;
            }
            return Types.getArrayType(ct);
        } else {
            try {
                return Types.getClassType(this.astLoader.findAst(type));
            } catch (AstNotFoundException ex) {
                throw new RuntimeException(ex);
            }
        }
    } else {
        return null;
    }
}
Also used : GenericType(kalang.core.GenericType) GenericArrayType(java.lang.reflect.GenericArrayType) ObjectType(kalang.core.ObjectType) GenericArrayType(java.lang.reflect.GenericArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ClassType(kalang.core.ClassType) WildcardType(kalang.core.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) AstNotFoundException(kalang.AstNotFoundException) Nullable(javax.annotation.Nullable)

Example 2 with WildcardType

use of kalang.core.WildcardType 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 3 with WildcardType

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

Aggregations

ClassType (kalang.core.ClassType)3 GenericType (kalang.core.GenericType)3 ObjectType (kalang.core.ObjectType)3 Type (kalang.core.Type)3 WildcardType (kalang.core.WildcardType)3 Nullable (javax.annotation.Nullable)2 ArrayType (kalang.core.ArrayType)2 PrimitiveType (kalang.core.PrimitiveType)2 GenericArrayType (java.lang.reflect.GenericArrayType)1 TypeVariable (java.lang.reflect.TypeVariable)1 AstNotFoundException (kalang.AstNotFoundException)1