use of io.sundr.model.PrimitiveRefBuilder in project sundrio by sundrio.
the class TypeToTypeRef method apply.
@Override
public TypeRef apply(Type item) {
if (item == null) {
return new VoidRefBuilder().build();
} else if (item instanceof WildcardType) {
return new WildcardRefBuilder().withBounds(Arrays.asList(((WildcardType) item).getLowerBounds()).stream().map(t -> apply(t)).collect(Collectors.toList())).build();
} else if (item instanceof TypeVariable) {
return new TypeParamRefBuilder().withName(((TypeVariable) item).getName()).build();
} else if (item instanceof GenericArrayType) {
Type target = item;
int dimensions = 0;
while (target instanceof GenericArrayType) {
target = ((GenericArrayType) target).getGenericComponentType();
dimensions++;
}
if (target instanceof Class) {
references.add((Class) target);
}
TypeRef targetRef = apply(target);
return targetRef.withDimensions(dimensions + targetRef.getDimensions());
} else if (item instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) item;
Type rawType = parameterizedType.getRawType();
List<TypeRef> arguments = new ArrayList<TypeRef>();
for (Type arg : parameterizedType.getActualTypeArguments()) {
arguments.add(apply(arg));
if (arg instanceof Class) {
references.add((Class) arg);
}
}
if (rawType instanceof Class) {
references.add((Class) rawType);
}
return new ClassRefBuilder((ClassRef) apply(rawType)).withArguments(arguments).build();
} else if (Object.class.equals(item)) {
return ClassRef.OBJECT;
} else if (item instanceof Class) {
Class c = (Class) item;
if (c.isArray()) {
Class target = c;
int dimensions = 0;
while (target.isArray()) {
target = ((Class) target).getComponentType();
dimensions++;
}
TypeRef targetRef = apply(target);
references.add(target);
return targetRef.withDimensions(dimensions + targetRef.getDimensions());
}
if (c.isPrimitive()) {
return new PrimitiveRefBuilder().withName(c.getName()).withDimensions(0).build();
} else {
List<TypeRef> arguments = new ArrayList<TypeRef>();
for (TypeVariable v : c.getTypeParameters()) {
arguments.add(apply(v));
}
references.add((Class) item);
String fqcn = c.getName().replaceAll(Pattern.quote("$"), ".");
return new ClassRefBuilder().withFullyQualifiedName(fqcn).withArguments(arguments).build();
}
}
throw new IllegalArgumentException("Can't convert type:" + item + " to a TypeRef");
}
use of io.sundr.model.PrimitiveRefBuilder in project sundrio by sundrio.
the class TypeToTypeRef method apply.
@Override
public TypeRef apply(Type type) {
if (type instanceof VoidType) {
return new VoidRef();
} else if (type instanceof WildcardType) {
return new WildcardRef();
} else if (type instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) type;
int dimensions = referenceType.getArrayCount();
TypeRef typeRef = apply(referenceType.getType());
if (dimensions == 0) {
return typeRef;
} else if (typeRef instanceof ClassRef) {
return new ClassRefBuilder((ClassRef) typeRef).withDimensions(dimensions).build();
} else if (typeRef instanceof PrimitiveRef) {
return new PrimitiveRefBuilder((PrimitiveRef) typeRef).withDimensions(dimensions).build();
} else if (typeRef instanceof TypeParamRef) {
return new TypeParamRefBuilder((TypeParamRef) typeRef).withDimensions(dimensions).build();
}
} else if (type instanceof PrimitiveType) {
PrimitiveType primitiveType = (PrimitiveType) type;
return new PrimitiveRefBuilder().withName(primitiveType.getType().name()).build();
} else if (type instanceof ClassOrInterfaceType) {
return classOrInterfaceToTypeRef.apply((ClassOrInterfaceType) type);
}
throw new IllegalArgumentException("Can't handle type:[" + type + "].");
}
Aggregations