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