-
Notifications
You must be signed in to change notification settings - Fork 11
/
java.ts
399 lines (344 loc) · 10.9 KB
/
java.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import * as reflect from 'jsii-reflect';
import * as transpile from './transpile';
// eslint-disable-next-line @typescript-eslint/no-require-imports
const Case = require('case');
// Helper methods
const toCamelCase = (text?: string) => {
return Case.camel(text ?? '');
};
const toUpperCamelCase = (test?: string) => {
const camelCase = toCamelCase(test);
return camelCase.charAt(0).toUpperCase() + camelCase.slice(1);
};
// [1, 2, 3] -> [[], [1], [1, 2], [1, 2, 3]]
const prefixArrays = <T>(arr: T[]): T[][] => {
const out: T[][] = [[]];
const prefix: T[] = [];
for (const elem of arr) {
prefix.push(elem);
out.push([...prefix]);
}
return out;
};
/**
* Hack to convert a jsii property to a parameter for
* parameter expansion.
*/
const propertyToParameter = (
callable: reflect.Callable,
property: reflect.Property,
): reflect.Parameter => {
return {
docs: property.docs,
method: callable,
name: property.name,
optional: property.optional,
parentType: property.parentType,
spec: property.spec,
system: property.system,
type: property.type,
variadic: false,
};
};
export class JavaTranspile extends transpile.TranspileBase {
constructor() {
super(transpile.Language.JAVA);
}
public moduleLike(
moduleLike: reflect.ModuleLike,
): transpile.TranspiledModuleLike {
const fqn: string = moduleLike.targets?.java?.package;
if (!fqn) {
throw new Error(
`Java is not a supported target for module: ${moduleLike.fqn}`,
);
}
if (moduleLike instanceof reflect.Submodule) {
const parent = this.getParentModule(moduleLike);
if (!parent) {
throw new Error(`Could not find parent module of ${moduleLike.fqn}`);
}
const parentFqn = parent.targets?.java?.package;
if (!fqn.startsWith(parentFqn)) {
throw new Error(`Expected submodule ${fqn} to start with ${parentFqn} since it is its parent module.`);
}
// { name: "software.amazon.awscdk", submodule: "services.ecr" }
return { name: parentFqn, submodule: fqn.substring(parentFqn.length + 1) };
}
return { name: fqn };
}
private getParentModule(moduleLike: reflect.ModuleLike): reflect.Assembly | undefined {
const ts = moduleLike.system;
for (const assembly of ts.assemblies) {
const child = assembly.submodules.find((mod) => mod.fqn === moduleLike.fqn);
if (child) {
return assembly;
}
}
return undefined;
}
public callable(callable: reflect.Callable): transpile.TranspiledCallable {
const type = this.type(callable.parentType);
const parameters = callable.parameters.sort(this.optionalityCompare);
const requiredParams = parameters.filter((p) => !p.optional);
const optionalParams = parameters.filter((p) => p.optional);
const name = callable.name;
// simulate Java method overloading
const inputLists = prefixArrays(optionalParams).map((optionals) => {
return [...requiredParams, ...optionals].map((p) => this.formatParameter(this.parameter(p)));
});
const signatures = inputLists.map((inputs) => {
return this.formatSignature(name, inputs);
});
let invocations;
if (this.isClassBuilderGenerated(callable)) {
const struct = this.extractFirstStruct(parameters);
// render using Java builder syntax (show no overloads)
invocations = [this.formatClassBuilder(type, parameters, struct)];
// flatten out the parameters so the user doesn't have to jump between
// docs of Foo and FooProps
for (const property of struct.allProperties) {
const parameter = propertyToParameter(callable, property);
parameters.push(parameter);
}
} else {
invocations = callable.kind === reflect.MemberKind.Initializer
// render with `new Class` syntax (showing all constructor overloads)
? inputLists.map((inputs) => this.formatClassInitialization(type, inputs))
// render invocation as method calls (showing all method overloads)
: inputLists.map((inputs) => this.formatInvocation(type, inputs, name));
}
return {
name,
parentType: type,
import: this.formatImport(type),
parameters,
signatures,
invocations,
};
}
public class(klass: reflect.ClassType): transpile.TranspiledClass {
return {
name: klass.name,
type: this.type(klass),
};
}
public struct(struct: reflect.InterfaceType): transpile.TranspiledStruct {
const type = this.type(struct);
const indent = ' '.repeat(4);
const inputs = struct.allProperties.map((p) =>
this.formatBuilderMethod(this.property(p), indent),
).flat();
return {
type: type,
name: struct.name,
import: this.formatImport(type),
initialization: this.formatStructBuilder(type, inputs),
};
}
public interface(
iface: reflect.InterfaceType,
): transpile.TranspiledInterface {
return {
name: iface.name,
type: this.type(iface),
};
}
public parameter(
parameter: reflect.Parameter,
): transpile.TranspiledParameter {
const typeRef = this.typeReference(parameter.type);
return {
name: parameter.name,
parentType: this.type(parameter.parentType),
typeReference: typeRef,
optional: parameter.optional,
declaration: this.formatProperty(parameter.name, typeRef),
};
}
public property(property: reflect.Property): transpile.TranspiledProperty {
const typeRef = this.typeReference(property.type);
return {
name: property.name,
parentType: this.type(property.parentType),
typeReference: typeRef,
optional: property.optional,
declaration: this.formatProperty(property.name, typeRef),
};
}
public enum(enu: reflect.EnumType): transpile.TranspiledEnum {
return {
fqn: this.type(enu).fqn,
name: enu.name,
};
}
public enumMember(em: reflect.EnumMember): transpile.TranspiledEnumMember {
return {
fqn: `${this.enum(em.enumType).fqn}.${em.name}`,
name: em.name,
};
}
public unionOf(types: string[]): string {
return types.join(' OR ');
}
public listOf(type: string): string {
return `java.util.List<${type}>`;
}
public mapOf(type: string): string {
return `java.util.Map<java.lang.String, ${type}>`;
}
public any(): string {
return 'java.lang.Object';
}
public str(): string {
return 'java.lang.String';
}
public number(): string {
return 'java.lang.Number';
}
public boolean(): string {
return 'java.lang.Boolean';
}
public json(): string {
return 'com.fasterxml.jackson.databind.node.ObjectNode';
}
public date(): string {
return 'java.time.Instant';
}
public readme(readme: string): string {
return readme;
}
private formatImport(type: transpile.TranspiledType): string {
return `import ${type.fqn};`;
};
private formatParameter(
transpiled: transpile.TranspiledParameter | transpile.TranspiledProperty,
): string {
const tf = transpiled.typeReference.toString({
typeFormatter: (t) => t.name,
});
return `${tf} ${transpiled.name}`;
}
private formatInputs(inputs: string[]): string {
return inputs.join(', ');
};
private formatStructBuilder(type: transpile.TranspiledType, methods: string[]): string {
const builder = `${type.name}.builder()`;
return [
builder,
...methods,
' .build();',
].join('\n');
};
private formatClassInitialization(
type: transpile.TranspiledType,
inputs: string[],
): string {
return `new ${type.name}(${this.formatInputs(inputs)});`;
};
private formatClassBuilder(
type: transpile.TranspiledType,
parameters: reflect.Parameter[],
struct: reflect.InterfaceType,
): string {
const createArgs = this.formatInputs(parameters.map((p) => this.formatParameter(this.parameter(p))));
const indent = ' '.repeat(4);
const methods: string[] = struct.allProperties.map((p) =>
this.formatBuilderMethod(this.property(p), indent),
).flat();
return [
`${type.name}.Builder.create(${createArgs})`,
...methods,
`${indent}.build();`,
].join('\n');
};
private formatSignature(name: string, inputs: string[]) {
return `public ${name}(${this.formatInputs(inputs)})`;
};
private formatBuilderMethod(
transpiled: transpile.TranspiledParameter,
indent: string,
): string[] {
if (transpiled.optional) indent = '//' + indent.slice(2);
const lowerCamel = toCamelCase(transpiled.name);
const base = `${indent}.${lowerCamel}`;
const tf = transpiled.typeReference.toString({
typeFormatter: (t) => t.name,
});
// allow rendering union types as multiple overrided builder methods
if (tf.includes(' OR ')) {
const choices = tf.split(' OR ');
return choices.map((typ) => `${base}(${typ})`);
} else {
return [`${base}(${tf})`];
}
}
private formatInvocation(
type: transpile.TranspiledType,
inputs: string[],
method: string,
): string {
let target = type.submodule ? `${type.namespace}.${type.name}` : type.name;
if (method) {
target = `${target}.${method}`;
}
return `${target}(${this.formatInputs(inputs)})`;
};
private optionalityCompare(
p1: reflect.Parameter,
p2: reflect.Parameter,
): number {
if (!p1.optional && p2.optional) {
return -1;
}
if (!p2.optional && p1.optional) {
return 1;
}
return 0;
}
private isStruct(p: reflect.Parameter): boolean {
return p.type.fqn ? p.system.findFqn(p.type.fqn).isDataType() : false;
}
private isClassBuilderGenerated(
callable: reflect.Callable,
): boolean {
if (callable.kind !== reflect.MemberKind.Initializer) {
return false;
}
const parameters: reflect.Parameter[] = callable.parameters.sort(this.optionalityCompare);
const firstStruct = parameters.find((param) => this.isStruct(param));
// no builder is generated if there is no struct parameter
if (!firstStruct) {
return false;
}
return true;
};
/**
* Extracts the first struct out of a list of parameters (and throws
* if there is none), removing it from the array.
*/
private extractFirstStruct(
parameters: reflect.Parameter[],
): reflect.InterfaceType {
const firstStruct = parameters.find((param) => this.isStruct(param));
if (!firstStruct) {
throw new Error('No struct found in parameter list.');
}
const struct = firstStruct.parentType.system.findInterface(firstStruct.type.fqn!);
parameters.splice(parameters.indexOf(firstStruct), 1);
return struct;
}
private formatProperty(
name: string,
typeReference: transpile.TranspiledTypeReference,
): string {
const tf = typeReference.toString({
typeFormatter: (t) => t.name,
});
if (tf.includes(' OR ')) {
return `public java.lang.Object get${toUpperCamelCase(name)}();`;
} else {
return `public ${tf} get${toUpperCamelCase(name)}();`;
}
}
}