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;
}
}
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);
}
}
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);
}
}
Aggregations