use of cz.habarta.typescript.generator.type.JTypeWithNullability in project typescript-generator by vojtechhabarta.
the class DefaultTypeProcessor method processType.
@Override
public Result processType(Type javaType, Context context) {
if (Objects.equals(javaType, Object.class)) {
return new Result(TsType.Any);
}
if (javaType instanceof Class) {
final Class<?> javaClass = (Class<?>) javaType;
if (isAssignableFrom(known.stringClasses, javaClass)) {
return new Result(TsType.String);
}
if (isAssignableFrom(known.numberClasses, javaClass)) {
return new Result(TsType.Number);
}
if (isAssignableFrom(known.booleanClasses, javaClass)) {
return new Result(TsType.Boolean);
}
if (isAssignableFrom(known.dateClasses, javaClass)) {
return new Result(TsType.Date);
}
if (isAssignableFrom(known.voidClasses, javaClass)) {
return new Result(TsType.Void);
}
}
if (javaType instanceof Class) {
final Class<?> javaClass = (Class<?>) javaType;
final Symbol importedSymbol = context.getSymbolIfImported(javaClass);
if (importedSymbol != null) {
return new Result(new TsType.ReferenceType(importedSymbol));
}
}
if (javaType instanceof Class) {
final Class<?> javaClass = (Class<?>) javaType;
if (isAssignableFrom(known.anyClasses, javaClass)) {
return new Result(TsType.Any);
}
if (javaClass.isArray()) {
final Result result = context.processTypeInsideCollection(javaClass.getComponentType());
return new Result(new TsType.BasicArrayType(result.getTsType()), result.getDiscoveredClasses());
}
if (javaClass.isEnum()) {
return new Result(new TsType.EnumReferenceType(context.getSymbol(javaClass)), javaClass);
}
// list, map, optional, wrapper
final Result knownGenericTypeResult = processKnownGenericType(javaClass, javaClass, context);
if (knownGenericTypeResult != null) {
return knownGenericTypeResult;
}
if (OptionalInt.class.isAssignableFrom(javaClass) || OptionalLong.class.isAssignableFrom(javaClass) || OptionalDouble.class.isAssignableFrom(javaClass)) {
return new Result(TsType.Number.optional());
}
// generic structural type used without type arguments
if (javaClass.getTypeParameters().length > 0) {
final List<TsType> tsTypeArguments = new ArrayList<>();
for (int i = 0; i < javaClass.getTypeParameters().length; i++) {
tsTypeArguments.add(TsType.Any);
}
return new Result(new TsType.GenericReferenceType(context.getSymbol(javaClass), tsTypeArguments));
}
// structural type
return new Result(new TsType.ReferenceType(context.getSymbol(javaClass)), javaClass);
}
if (javaType instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) javaType;
if (parameterizedType.getRawType() instanceof Class) {
final Class<?> javaClass = (Class<?>) parameterizedType.getRawType();
// list, map, optional, wrapper
final Result knownGenericTypeResult = processKnownGenericType(javaType, javaClass, context);
if (knownGenericTypeResult != null) {
return knownGenericTypeResult;
}
// generic structural type
final List<Class<?>> discoveredClasses = new ArrayList<>();
discoveredClasses.add(javaClass);
final List<TsType> tsTypeArguments = new ArrayList<>();
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
final TypeProcessor.Result typeArgumentResult = context.processType(typeArgument);
tsTypeArguments.add(typeArgumentResult.getTsType());
discoveredClasses.addAll(typeArgumentResult.getDiscoveredClasses());
}
return new Result(new TsType.GenericReferenceType(context.getSymbol(javaClass), tsTypeArguments), discoveredClasses);
}
}
if (javaType instanceof GenericArrayType) {
final GenericArrayType genericArrayType = (GenericArrayType) javaType;
final Result result = context.processTypeInsideCollection(genericArrayType.getGenericComponentType());
return new Result(new TsType.BasicArrayType(result.getTsType()), result.getDiscoveredClasses());
}
if (javaType instanceof TypeVariable) {
final TypeVariable<?> typeVariable = (TypeVariable<?>) javaType;
if (typeVariable.getGenericDeclaration() instanceof Method) {
// example method: public <T extends Number> T getData();
return context.processType(typeVariable.getBounds()[0]);
}
return new Result(new TsType.GenericVariableType(typeVariable.getName()));
}
if (javaType instanceof WildcardType) {
final WildcardType wildcardType = (WildcardType) javaType;
final Type[] upperBounds = wildcardType.getUpperBounds();
return upperBounds.length > 0 ? context.processType(upperBounds[0]) : new Result(TsType.Any);
}
if (javaType instanceof JUnionType) {
final JUnionType unionType = (JUnionType) javaType;
final List<Result> results = unionType.getTypes().stream().map(type -> context.processType(type)).collect(Collectors.toList());
return new Result(new TsType.UnionType(results.stream().map(result -> result.getTsType()).collect(Collectors.toList())), results.stream().flatMap(result -> result.getDiscoveredClasses().stream()).collect(Collectors.toList()));
}
if (javaType instanceof JTypeWithNullability) {
final JTypeWithNullability typeWithNullability = (JTypeWithNullability) javaType;
final Result result = context.processType(typeWithNullability.getType());
return new Result(typeWithNullability.isNullable() ? new TsType.NullableType(result.getTsType()) : result.getTsType(), result.getDiscoveredClasses());
}
return null;
}
Aggregations