|
10 | 10 | import cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures; |
11 | 11 | import cz.habarta.typescript.generator.emitter.TsAssignmentExpression; |
12 | 12 | import cz.habarta.typescript.generator.emitter.TsBeanModel; |
| 13 | +import cz.habarta.typescript.generator.emitter.TsCallExpression; |
13 | 14 | import cz.habarta.typescript.generator.emitter.TsConstructorModel; |
14 | 15 | import cz.habarta.typescript.generator.emitter.TsEnumModel; |
15 | 16 | import cz.habarta.typescript.generator.emitter.TsExpression; |
|
22 | 23 | import cz.habarta.typescript.generator.emitter.TsPropertyModel; |
23 | 24 | import cz.habarta.typescript.generator.emitter.TsStatement; |
24 | 25 | import cz.habarta.typescript.generator.emitter.TsStringLiteral; |
| 26 | +import cz.habarta.typescript.generator.emitter.TsSuperExpression; |
25 | 27 | import cz.habarta.typescript.generator.emitter.TsThisExpression; |
26 | 28 |
|
27 | 29 | import java.util.ArrayList; |
28 | 30 | import java.util.Arrays; |
| 31 | +import java.util.HashMap; |
29 | 32 | import java.util.List; |
30 | 33 | import java.util.Map; |
31 | 34 | import java.util.Optional; |
@@ -55,38 +58,60 @@ public void setConfiguration(Map<String, String> configuration) throws RuntimeEx |
55 | 58 |
|
56 | 59 | @Override |
57 | 60 | public List<TransformerDefinition> getTransformers() { |
58 | | - return Arrays.asList(new TransformerDefinition(ModelCompiler.TransformationPhase.BeforeSymbolResolution, new ModelTransformer() { |
| 61 | + return Arrays.asList(new TransformerDefinition(ModelCompiler.TransformationPhase.AfterDeclarationSorting, new ModelTransformer() { |
59 | 62 | @Override |
60 | 63 | public TsModel transformModel(SymbolTable symbolTable, TsModel model) { |
61 | 64 | List<TsBeanModel> beans = new ArrayList<>(); |
| 65 | + Map<String, TsConstructorModel> generatedConstructors = new HashMap<>(); |
62 | 66 | for (TsBeanModel bean : model.getBeans()) { |
63 | | - TsBeanModel newBean = transformBean(bean, model); |
| 67 | + TsBeanModel newBean = transformBean(bean, model, generatedConstructors); |
64 | 68 | beans.add(newBean); |
65 | 69 | } |
66 | 70 | return model.withBeans(beans); |
67 | 71 | } |
68 | 72 | })); |
69 | 73 | } |
70 | 74 |
|
71 | | - private TsBeanModel transformBean(TsBeanModel bean, TsModel model) { |
| 75 | + private TsBeanModel transformBean(TsBeanModel bean, TsModel model, |
| 76 | + Map<String, TsConstructorModel> generatedConstructors) { |
72 | 77 | if (!bean.isClass() || bean.getConstructor() != null) { |
73 | 78 | return bean; |
74 | 79 | } |
75 | | - Optional<TsConstructorModel> constructorOption = createConstructor(bean, model); |
| 80 | + Optional<TsConstructorModel> constructorOption = createConstructor(bean, model, generatedConstructors); |
76 | 81 | if (!constructorOption.isPresent()) { |
77 | 82 | return bean; |
78 | 83 | } |
79 | 84 | if (classes != null && !classes.contains(bean.getOrigin().getCanonicalName())) { |
80 | 85 | return bean; |
81 | 86 | } |
82 | | - return bean.withConstructor(constructorOption.get()); |
| 87 | + TsConstructorModel constructor = constructorOption.get(); |
| 88 | + generatedConstructors.put(bean.getName().getFullName(), constructor); |
| 89 | + return bean.withConstructor(constructor); |
83 | 90 | } |
84 | 91 |
|
85 | | - private static Optional<TsConstructorModel> createConstructor(TsBeanModel bean, TsModel model) { |
| 92 | + private static Optional<TsConstructorModel> createConstructor(TsBeanModel bean, TsModel model, |
| 93 | + Map<String, TsConstructorModel> generatedConstructors) { |
86 | 94 | List<TsParameterModel> parameters = new ArrayList<>(); |
87 | 95 | List<TsStatement> body = new ArrayList<>(); |
88 | | - if (bean.getParent() != null) { |
89 | | - throw new IllegalStateException("Creating constructors for inherited beans is not currently supported"); |
| 96 | + TsType parent = bean.getParent(); |
| 97 | + if (parent != null) { |
| 98 | + if (!(parent instanceof TsType.ReferenceType)) { |
| 99 | + throw new IllegalStateException("Generating constructor for non-reference parent types is not currently supported"); |
| 100 | + } |
| 101 | + TsType.ReferenceType referenceParent = (TsType.ReferenceType) parent; |
| 102 | + TsConstructorModel parentConstructor = generatedConstructors.get(referenceParent.symbol.getFullName()); |
| 103 | + if (parentConstructor == null) { |
| 104 | + throw new IllegalStateException("Generating constructor for class with non-generated constructor is not currently supported"); |
| 105 | + } |
| 106 | + List<TsParameterModel> parentParameters = parentConstructor.getParameters(); |
| 107 | + TsIdentifierReference[] callParameters = new TsIdentifierReference[parentParameters.size()]; |
| 108 | + int i = 0; |
| 109 | + for (TsParameterModel parentParameter : parentParameters) { |
| 110 | + parameters.add(parentParameter); |
| 111 | + callParameters[i] = new TsIdentifierReference(parentParameter.name); |
| 112 | + i++; |
| 113 | + } |
| 114 | + body.add(new TsExpressionStatement(new TsCallExpression(new TsSuperExpression(), callParameters))); |
90 | 115 | } |
91 | 116 | for (TsPropertyModel property : bean.getProperties()) { |
92 | 117 | if (!property.modifiers.isReadonly) { |
|
0 commit comments