use of cz.habarta.typescript.generator.compiler.Symbol in project typescript-generator by vojtechhabarta.
the class JsonDeserializationExtension method createDeserializationGenericFunctionConstructor.
private static TsMethodModel createDeserializationGenericFunctionConstructor(SymbolTable symbolTable, TsModel tsModel, TsBeanModel bean) {
final Symbol beanIdentifier = symbolTable.getSymbol(bean.getOrigin());
List<TsType.GenericVariableType> typeParameters = getTypeParameters(bean.getOrigin());
final TsType.ReferenceType dataType = new TsType.GenericReferenceType(beanIdentifier, typeParameters);
final List<TsParameterModel> constructorFnOfParameters = getConstructorFnOfParameters(typeParameters);
final List<TsExpression> arguments = new ArrayList<>();
arguments.add(new TsIdentifierReference("data"));
for (TsParameterModel constructorFnOfParameter : constructorFnOfParameters) {
arguments.add(new TsIdentifierReference(constructorFnOfParameter.name));
}
final List<TsStatement> body = new ArrayList<>();
body.add(new TsReturnStatement(new TsArrowFunction(Arrays.asList(new TsParameter("data", null)), new TsCallExpression(new TsMemberExpression(new TsTypeReferenceExpression(new TsType.ReferenceType(beanIdentifier)), "fromData"), null, arguments))));
return new TsMethodModel("fromDataFn", TsModifierFlags.None.setStatic(), typeParameters, constructorFnOfParameters, new TsType.FunctionType(Arrays.asList(new TsParameter("data", dataType)), dataType), body, null);
}
use of cz.habarta.typescript.generator.compiler.Symbol in project typescript-generator by vojtechhabarta.
the class OnePossiblePropertyValueAssigningExtension method findOnlyValueForEnumReferenceType.
private static Optional<TsExpression> findOnlyValueForEnumReferenceType(TsModel model, TsType.EnumReferenceType propertyType) {
Symbol symbol = propertyType.symbol;
Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream().filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())).findAny();
if (!enumModelOption.isPresent()) {
return Optional.empty();
}
TsEnumModel enumModel = enumModelOption.get();
if (enumModel.getMembers().size() != 1) {
return Optional.empty();
}
EnumMemberModel singleElement = enumModel.getMembers().iterator().next();
Object enumValue = singleElement.getEnumValue();
TsStringLiteral expression = new TsStringLiteral((String) enumValue);
return Optional.of(expression);
}
use of cz.habarta.typescript.generator.compiler.Symbol 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;
}
use of cz.habarta.typescript.generator.compiler.Symbol in project typescript-generator by vojtechhabarta.
the class RequiredPropertyConstructorExtension method getPredefinedValueForProperty.
private static Optional<TsExpression> getPredefinedValueForProperty(TsPropertyModel property, TsModel model) {
if (property.tsType instanceof TsType.UnionType) {
List<TsType> unionTypeElements = ((TsType.UnionType) property.tsType).types;
if (unionTypeElements.size() != 1) {
return Optional.empty();
}
TsType onlyElement = unionTypeElements.iterator().next();
if (!(onlyElement instanceof TsType.StringLiteralType)) {
return Optional.empty();
}
TsType.StringLiteralType onlyValue = (TsType.StringLiteralType) onlyElement;
TsStringLiteral expression = new TsStringLiteral(onlyValue.literal);
return Optional.of(expression);
}
if (property.tsType instanceof TsType.EnumReferenceType) {
Symbol symbol = ((TsType.EnumReferenceType) property.tsType).symbol;
Optional<TsEnumModel> enumModelOption = model.getOriginalStringEnums().stream().filter(candidate -> candidate.getName().getFullName().equals(symbol.getFullName())).findAny();
if (!enumModelOption.isPresent()) {
return Optional.empty();
}
TsEnumModel enumModel = enumModelOption.get();
if (enumModel.getMembers().size() != 1) {
return Optional.empty();
}
EnumMemberModel singleElement = enumModel.getMembers().iterator().next();
Object enumValue = singleElement.getEnumValue();
TsStringLiteral expression = new TsStringLiteral((String) enumValue);
return Optional.of(expression);
}
return Optional.empty();
}
use of cz.habarta.typescript.generator.compiler.Symbol in project typescript-generator by vojtechhabarta.
the class PropertyPolymorphismExtension method getTransformers.
@Override
public List<TransformerDefinition> getTransformers() {
return Arrays.asList(new TransformerDefinition(TransformationPhase.BeforeSymbolResolution, new ModelTransformer() {
@Override
public TsModel transformModel(SymbolTable symbolTable, TsModel model) {
List<TsBeanModel> newBeans = new ArrayList<>();
for (TsBeanModel bean : model.getBeans()) {
// replace references
List<TsPropertyModel> newProperties = new ArrayList<>();
for (TsPropertyModel property : bean.getProperties()) {
if (property.tsType instanceof ReferenceType) {
ReferenceType type = (ReferenceType) property.tsType;
TsBeanModel referencedBean = model.getBean(type.symbol);
if (isPolymorphicBase.test(referencedBean.getOrigin())) {
Symbol refSymbol = symbolTable.addSuffixToSymbol(type.symbol, "Ref");
newProperties.add(property.withTsType(new TsType.ReferenceType(refSymbol)));
continue;
}
}
newProperties.add(property);
}
newBeans.add(bean.withProperties(newProperties));
}
// add reference beans
{
List<TsBeanModel> bases = new ArrayList<>();
Map<Class<?>, Set<Class<?>>> subTypes = new HashMap<>();
Map<Class<?>, TsBeanModel> beanByOrigin = new HashMap<>();
for (TsBeanModel bean : model.getBeans()) {
Class<?> origin = bean.getOrigin();
if (origin == null) {
continue;
}
beanByOrigin.put(origin, bean);
if (isPolymorphicBase.test(origin)) {
bases.add(bean);
}
fillSubTypes(origin, subTypes, origin, new HashSet<>());
}
for (TsBeanModel base : bases) {
List<TsPropertyModel> refProperties = new ArrayList<>();
for (Class<?> subType : subTypes.getOrDefault(base.getOrigin(), Collections.emptySet())) {
refProperties.add(new TsPropertyModel(getPropertyName.apply(subType), new ReferenceType(symbolTable.getSymbol(subType)), null, true, null));
}
newBeans.add(new TsBeanModel(base.getOrigin(), TsBeanCategory.Data, false, symbolTable.addSuffixToSymbol(base.getName(), "Ref"), null, null, null, null, refProperties, null, null, null));
}
}
return model.withBeans(newBeans);
}
}));
}
Aggregations