Search in sources :

Example 1 with JTypeWithNullability

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;
}
Also used : JUnionType(cz.habarta.typescript.generator.type.JUnionType) GenericArrayType(java.lang.reflect.GenericArrayType) Arrays(java.util.Arrays) Date(java.util.Date) WildcardType(java.lang.reflect.WildcardType) OptionalDouble(java.util.OptionalDouble) JTypeWithNullability(cz.habarta.typescript.generator.type.JTypeWithNullability) OptionalInt(java.util.OptionalInt) GenericsResolver(cz.habarta.typescript.generator.util.GenericsResolver) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) Calendar(java.util.Calendar) Map(java.util.Map) Method(java.lang.reflect.Method) TypeVariable(java.lang.reflect.TypeVariable) Collection(java.util.Collection) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Optional(java.util.Optional) Symbol(cz.habarta.typescript.generator.compiler.Symbol) Utils(cz.habarta.typescript.generator.util.Utils) Temporal(java.time.temporal.Temporal) Symbol(cz.habarta.typescript.generator.compiler.Symbol) ArrayList(java.util.ArrayList) ParameterizedType(java.lang.reflect.ParameterizedType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType) Method(java.lang.reflect.Method) OptionalDouble(java.util.OptionalDouble) JUnionType(cz.habarta.typescript.generator.type.JUnionType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) JTypeWithNullability(cz.habarta.typescript.generator.type.JTypeWithNullability) JUnionType(cz.habarta.typescript.generator.type.JUnionType)

Aggregations

Symbol (cz.habarta.typescript.generator.compiler.Symbol)1 JTypeWithNullability (cz.habarta.typescript.generator.type.JTypeWithNullability)1 JUnionType (cz.habarta.typescript.generator.type.JUnionType)1 GenericsResolver (cz.habarta.typescript.generator.util.GenericsResolver)1 Utils (cz.habarta.typescript.generator.util.Utils)1 GenericArrayType (java.lang.reflect.GenericArrayType)1 Method (java.lang.reflect.Method)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 WildcardType (java.lang.reflect.WildcardType)1 Temporal (java.time.temporal.Temporal)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1