11package io .avaje .http .generator .core ;
22
3+ import static java .util .stream .Collectors .toList ;
4+
35import java .io .IOException ;
6+ import java .util .List ;
47import java .util .Set ;
58import java .util .TreeSet ;
69
710import javax .lang .model .element .TypeElement ;
811import javax .lang .model .type .TypeMirror ;
912
10-
1113public class TestClientWriter {
1214
1315 private static final String AT_GENERATED = "@Generated(\" avaje-http-generator\" )" ;
@@ -18,6 +20,7 @@ public class TestClientWriter {
1820 private String packageName ;
1921 private String fullName ;
2022 private Append writer ;
23+ private List <MethodReader > methods ;
2124
2225 TestClientWriter (ControllerReader reader ) throws IOException {
2326
@@ -27,6 +30,17 @@ public class TestClientWriter {
2730 this .shortName = origin .getSimpleName ().toString ();
2831 this .packageName = initPackageName (originName );
2932 this .fullName = packageName + "." + shortName + "$TestAPI" ;
33+ this .methods =
34+ reader .methods ().stream ()
35+ .filter (MethodReader ::isWebMethod )
36+ .filter (
37+ m ->
38+ m .webMethod () instanceof CoreWebMethod
39+ && m .webMethod () != CoreWebMethod .ERROR
40+ && m .webMethod () != CoreWebMethod .FILTER
41+ && m .webMethod () != CoreWebMethod .OTHER )
42+ .collect (toList ());
43+ if (methods .isEmpty ()) return ;
3044 writer = new Append (APContext .createSourceFile (fullName , reader .beanType ()).openWriter ());
3145 }
3246
@@ -36,6 +50,7 @@ protected String initPackageName(String originName) {
3650 }
3751
3852 void write () {
53+ if (methods .isEmpty ()) return ;
3954 writePackage ();
4055 writeImports ();
4156 writeClassStart ();
@@ -50,18 +65,14 @@ protected void writePackage() {
5065
5166 protected void writeImports () {
5267 importTypes .add ("java.net.http.HttpResponse" );
68+ importTypes .add ("io.avaje.http.api.*" );
5369
54- reader
55- .methods ()
56- .forEach (
57- m -> {
58- importTypes .addAll (UType .parse (m .returnType ()).importTypes ());
59- m .params ()
60- .forEach (
61- p -> importTypes .addAll (UType .parse (p .element ().asType ()).importTypes ()));
62- });
63-
64- importTypes .addAll (reader .importTypes ());
70+ methods .forEach (
71+ m -> {
72+ importTypes .addAll (UType .parse (m .returnType ()).importTypes ());
73+ m .params ()
74+ .forEach (p -> importTypes .addAll (UType .parse (p .element ().asType ()).importTypes ()));
75+ });
6576
6677 importTypes .removeIf (
6778 i ->
@@ -82,15 +93,7 @@ private void writeClassStart() {
8293
8394 private void writeAddRoutes () {
8495
85- reader .methods ().stream ()
86- .filter (MethodReader ::isWebMethod )
87- .filter (
88- m ->
89- m .webMethod () instanceof CoreWebMethod
90- && m .webMethod () != CoreWebMethod .ERROR
91- && m .webMethod () != CoreWebMethod .FILTER
92- && m .webMethod () != CoreWebMethod .OTHER )
93- .forEach (this ::writeRoute );
96+ methods .forEach (this ::writeRoute );
9497
9598 writer .append ("}" ).eol ();
9699 writer .close ();
@@ -102,6 +105,12 @@ private void writeRoute(MethodReader method) {
102105 AnnotationCopier .copyAnnotations (writer , method .element (), true );
103106
104107 var returnTypeStr = PrimitiveUtil .wrap (UType .parse (returnType ).shortType ());
108+
109+ if (returnTypeStr .contains ("CompletableFuture" )) {
110+ returnTypeStr =
111+ returnTypeStr .substring (0 , returnTypeStr .length () - 1 ).replace ("CompletableFuture<" , "" );
112+ }
113+
105114 writer .append (
106115 "HttpResponse<%s> %s(" , isJstache ? "String" : returnTypeStr , method .simpleName ());
107116 boolean first = true ;
0 commit comments