Skip to content

Commit be3c8fc

Browse files
committed
recursive generic writing
1 parent c264fa2 commit be3c8fc

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

http-generator-client/src/main/java/io/avaje/http/generator/client/ClientMethodWriter.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.stream.Stream;
1616

1717
import javax.lang.model.element.TypeElement;
18+
import javax.lang.model.type.TypeKind;
1819
import javax.lang.model.util.ElementFilter;
1920

2021
import io.avaje.http.generator.core.APContext;
@@ -28,13 +29,15 @@
2829
import io.avaje.http.generator.core.PathSegments.Segment;
2930
import io.avaje.http.generator.core.ProcessingContext;
3031
import io.avaje.http.generator.core.RequestTimeoutPrism;
31-
import io.avaje.http.generator.core.UType;
3232
import io.avaje.http.generator.core.Util;
3333
import io.avaje.http.generator.core.WebMethod;
34+
import io.avaje.prism.GenerateUtils;
35+
import io.avaje.http.generator.client.UType;
3436

3537
/**
3638
* Write code to register Web route for a given controller method.
3739
*/
40+
@GenerateUtils
3841
final class ClientMethodWriter {
3942
private static final KnownResponse KNOWN_RESPONSE = new KnownResponse();
4043
private static final String BODY_HANDLER = "java.net.http.HttpResponse.BodyHandler";
@@ -61,7 +64,7 @@ final class ClientMethodWriter {
6164
this.method = method;
6265
this.writer = writer;
6366
this.webMethod = method.webMethod();
64-
this.returnType = Util.parseType(method.returnType());
67+
this.returnType = UType.parse(method.returnType());
6568
this.timeout = method.timeout();
6669
this.useConfig = ProcessingContext.typeElement("io.avaje.config.Config") != null;
6770

@@ -195,7 +198,7 @@ private void writeTimeout(RequestTimeoutPrism p) {
195198
private void writeEnd() {
196199
final var webMethod = method.webMethod();
197200
writer.append(" .%s()", webMethod.name()).eol();
198-
if (returnType == UType.VOID) {
201+
if (returnType.kind() == TypeKind.VOID) {
199202
writer.append(" .asVoid();").eol();
200203
} else {
201204
String known = KNOWN_RESPONSE.get(returnType.full());
@@ -219,17 +222,17 @@ private void writeSyncResponse() {
219222

220223
private void writeAsyncResponse() {
221224
writer.append(" .async()");
222-
writeResponse(returnType.paramRaw());
225+
writeResponse(returnType.param0());
223226
}
224227

225228
private void writeCallResponse() {
226229
writer.append(" .call()");
227-
writeResponse(returnType.paramRaw());
230+
writeResponse(returnType.param0());
228231
}
229232

230233
private void writeResponse(UType type) {
231234
final var mainType = type.mainType();
232-
final var param1 = type.paramRaw();
235+
final var param1 = type.param0();
233236
if (isList(mainType)) {
234237
writer.append(".list(");
235238
writeGeneric(param1);
@@ -240,13 +243,13 @@ private void writeResponse(UType type) {
240243
writer.append(");").eol();
241244
} else if (isHttpResponse(mainType)) {
242245
if (bodyHandlerParam == null) {
243-
final UType paramType = type.paramRaw();
246+
final UType paramType = type.param0();
244247
if ("java.util.List".equals(paramType.mainType())) {
245248
writer.append(".asList(");
246-
writeGeneric(paramType.paramRaw());
249+
writeGeneric(paramType.param0());
247250
} else if ("java.util.stream.Stream".equals(paramType.mainType())) {
248251
writer.append(".asStream(");
249-
writeGeneric(paramType.paramRaw());
252+
writeGeneric(paramType.param0());
250253
} else {
251254
writer.append(".as(");
252255
writeGeneric(paramType);
@@ -266,22 +269,31 @@ void writeGeneric(UType type) {
266269
if (type.isGeneric() && useInject) {
267270
writer.append("new GenericType<%s>() {}.type()", type.shortType());
268271
} else if (type.isGeneric() && useJsonb) {
269-
final var params =
270-
type.importTypes().stream()
271-
.skip(1)
272-
.map(Util::shortName)
273-
.collect(Collectors.joining(".class, "));
274-
275-
writer.append(
276-
"Types.newParameterizedType(%s.class, %s.class)",
277-
Util.shortName(type.mainType()), params);
272+
writer.append(jsonbGeneric(type));
278273
} else if (type.isGeneric() && useJackson) {
279274
writer.append("new TypeReference<%s>() {}.getType()", type.shortType());
280275
} else {
281276
writer.append("%s.class", Util.shortName(type.mainType()));
282277
}
283278
}
284279

280+
private String jsonbGeneric(UType type) {
281+
final var params =
282+
type.componentTypes().stream()
283+
.map(
284+
c -> {
285+
if (c.isGeneric()) {
286+
return jsonbGeneric(c);
287+
} else {
288+
return c.shortWithoutAnnotations() + ".class";
289+
}
290+
})
291+
.collect(Collectors.joining(", "));
292+
293+
return String.format(
294+
"Types.newParameterizedType(%s.class, %s)", Util.shortName(type.mainType()), params);
295+
}
296+
285297
private void writeQueryParams(PathSegments pathSegments) {
286298
boolean clientImportError = false;
287299
for (final MethodParam param : method.params()) {
@@ -373,7 +385,7 @@ private void writeBody() {
373385
writer.append(" .body(%s)", param.name()).eol();
374386
} else {
375387
writer.append(" .body(%s, ", param.name());
376-
writeGeneric(param.utype());
388+
writeGeneric(UType.parse(param.element().asType()));
377389
writer.append(")").eol();
378390
}
379391
return;

0 commit comments

Comments
 (0)