11package io .avaje .http .generator .client ;
22
3- import io . avaje . http . generator . core .* ;
3+ import java . util . Set ;
44
55import javax .lang .model .element .TypeElement ;
6- import java .util .Set ;
76
8- /**
9- * Write code to register Web route for a given controller method.
10- */
7+ import io .avaje .http .generator .core .Append ;
8+ import io .avaje .http .generator .core .BeanParamReader ;
9+ import io .avaje .http .generator .core .ControllerReader ;
10+ import io .avaje .http .generator .core .MethodParam ;
11+ import io .avaje .http .generator .core .MethodReader ;
12+ import io .avaje .http .generator .core .ParamType ;
13+ import io .avaje .http .generator .core .PathSegments ;
14+ import io .avaje .http .generator .core .ProcessingContext ;
15+ import io .avaje .http .generator .core .UType ;
16+ import io .avaje .http .generator .core .Util ;
17+ import io .avaje .http .generator .core .WebMethod ;
18+
19+ /** Write code to register Web route for a given controller method. */
1120class ClientMethodWriter {
1221
1322 private static final KnownResponse KNOWN_RESPONSE = new KnownResponse ();
@@ -37,21 +46,22 @@ void addImportTypes(ControllerReader reader) {
3746 final var type = param .utype ();
3847 final var type0 = type .param0 ();
3948 final var type1 = type .param1 ();
40- reader .addImportType (type .mainType ());
41- if (type0 != null ) reader .addImportType (type0 );
42- if (type1 != null ) reader .addImportType (type1 );
49+ reader .addImportType (type .mainType (). replace ( "[]" , "" ) );
50+ if (type0 != null ) reader .addImportType (type0 . replace ( "[]" , "" ) );
51+ if (type1 != null ) reader .addImportType (type1 . replace ( "[]" , "" ) );
4352 }
4453 }
4554
4655 private void methodStart (Append writer ) {
47- for (MethodParam param : method .params ()) {
56+ for (final MethodParam param : method .params ()) {
4857 checkBodyHandler (param );
4958 }
5059 writer .append (" // %s %s" , webMethod , method .webMethodPath ()).eol ();
5160 writer .append (" @Override" ).eol ();
52- writer .append (" public %s%s %s(" , methodGenericParams , returnType .shortType (), method .simpleName ());
53- int count = 0 ;
54- for (MethodParam param : method .params ()) {
61+ writer .append (
62+ " public %s%s %s(" , methodGenericParams , returnType .shortType (), method .simpleName ());
63+ var count = 0 ;
64+ for (final MethodParam param : method .params ()) {
5565 if (count ++ > 0 ) {
5666 writer .append (", " );
5767 }
@@ -61,9 +71,7 @@ private void methodStart(Append writer) {
6171 writer .append (") {" ).eol ();
6272 }
6373
64- /**
65- * Assign a method parameter as *the* BodyHandler.
66- */
74+ /** Assign a method parameter as *the* BodyHandler. */
6775 private void checkBodyHandler (MethodParam param ) {
6876 if (param .rawType ().startsWith (BODY_HANDLER )) {
6977 param .setResponseHandler ();
@@ -80,8 +88,8 @@ void write() {
8088 }
8189 writer .append ("clientContext.request()" ).eol ();
8290
83- PathSegments pathSegments = method .pathSegments ();
84- Set < PathSegments . Segment > segments = pathSegments .segments ();
91+ final var pathSegments = method .pathSegments ();
92+ final var segments = pathSegments .segments ();
8593
8694 writeHeaders ();
8795 writePaths (segments );
@@ -93,45 +101,43 @@ void write() {
93101 }
94102
95103 private void writeEnd () {
96- WebMethod webMethod = method .webMethod ();
104+ final var webMethod = method .webMethod ();
97105 writer .append (" .%s()" , webMethod .name ()).eol ();
98106 if (returnType == UType .VOID ) {
99107 writer .append (" .asVoid();" ).eol ();
100108 } else {
101- String known = KNOWN_RESPONSE .get (returnType .full ());
109+ final var known = KNOWN_RESPONSE .get (returnType .full ());
102110 if (known != null ) {
103111 writer .append (" %s" , known ).eol ();
104- } else {
105- if (COMPLETABLE_FUTURE .equals (returnType .mainType ())) {
106- writeAsyncResponse ();
107- } else if (HTTP_CALL .equals (returnType .mainType ())) {
108- writeCallResponse ();
109- } else {
110- writeSyncResponse ();
111- }
112- }
112+ }else if (COMPLETABLE_FUTURE .equals (returnType .mainType ())) {
113+ writeAsyncResponse ();
114+ } else if (HTTP_CALL .equals (returnType .mainType ())) {
115+ writeCallResponse ();
116+ } else {
117+ writeSyncResponse ();
118+ }
113119 }
114120 writer .append (" }" ).eol ().eol ();
115121 }
116122
117123 private void writeSyncResponse () {
118124 writer .append (" " );
119- String type0 = returnType .mainType ();
120- String type1 = returnType .param0 ();
125+ final var type0 = returnType .mainType ();
126+ final var type1 = returnType .param0 ();
121127 writeResponse (type0 , type1 );
122128 }
123129
124130 private void writeAsyncResponse () {
125131 writer .append (" .async()" );
126- String type0 = returnType .param0 ();
127- String type1 = returnType .param1 ();
132+ final var type0 = returnType .param0 ();
133+ final var type1 = returnType .param1 ();
128134 writeResponse (type0 , type1 );
129135 }
130136
131137 private void writeCallResponse () {
132138 writer .append (" .call()" );
133- String type0 = returnType .param0 ();
134- String type1 = returnType .param1 ();
139+ final var type0 = returnType .param0 ();
140+ final var type1 = returnType .param1 ();
135141 writeResponse (type0 , type1 );
136142 }
137143
@@ -140,7 +146,7 @@ private void writeResponse(String type0, String type1) {
140146 writer .append (".list(%s.class);" , Util .shortName (type1 )).eol ();
141147 } else if (isStream (type0 )) {
142148 writer .append (".stream(%s.class);" , Util .shortName (type1 )).eol ();
143- } else if (isHttpResponse (type0 )){
149+ } else if (isHttpResponse (type0 )) {
144150 writeWithHandler ();
145151 } else {
146152 writer .append (".bean(%s.class);" , Util .shortName (type0 )).eol ();
@@ -156,23 +162,21 @@ private void writeWithHandler() {
156162 }
157163
158164 private void writeQueryParams (PathSegments pathSegments ) {
159- for (MethodParam param : method .params ()) {
160- ParamType paramType = param .paramType ();
161- if (paramType == ParamType .QUERYPARAM ) {
162- if (pathSegments .segment (param .paramName ()) == null ) {
163- if (isMap (param )) {
164- writer .append (" .queryParam(%s)" , param .name ()).eol ();
165- } else {
166- writer .append (" .queryParam(\" %s\" , %s)" , param .paramName (), param .name ()).eol ();
167- }
168- }
169- }
165+ for (final MethodParam param : method .params ()) {
166+ final var paramType = param .paramType ();
167+ if ((paramType == ParamType .QUERYPARAM ) && (pathSegments .segment (param .paramName ()) == null )) {
168+ if (isMap (param )) {
169+ writer .append (" .queryParam(%s)" , param .name ()).eol ();
170+ } else {
171+ writer .append (" .queryParam(\" %s\" , %s)" , param .paramName (), param .name ()).eol ();
172+ }
173+ }
170174 }
171175 }
172176
173177 private void writeHeaders () {
174- for (MethodParam param : method .params ()) {
175- ParamType paramType = param .paramType ();
178+ for (final MethodParam param : method .params ()) {
179+ final var paramType = param .paramType ();
176180 if (paramType == ParamType .HEADER ) {
177181 if (isMap (param )) {
178182 writer .append (" .header(%s)" , param .name ()).eol ();
@@ -184,23 +188,25 @@ private void writeHeaders() {
184188 }
185189
186190 private void writeBeanParams (PathSegments segments ) {
187- for (MethodParam param : method .params ()) {
188- final String varName = param .name ();
189- ParamType paramType = param .paramType ();
190- PathSegments . Segment segment = segments .segment (varName );
191+ for (final MethodParam param : method .params ()) {
192+ final var varName = param .name ();
193+ final var paramType = param .paramType ();
194+ final var segment = segments .segment (varName );
191195 if (segment == null && paramType == ParamType .BEANPARAM ) {
192- TypeElement formBeanType = ctx .typeElement (param .rawType ());
193- BeanParamReader form = new BeanParamReader (ctx , formBeanType , param .name (), param .shortType (), ParamType .QUERYPARAM );
196+ final var formBeanType = ctx .typeElement (param .rawType ());
197+ final var form =
198+ new BeanParamReader (
199+ ctx , formBeanType , param .name (), param .shortType (), ParamType .QUERYPARAM );
194200 form .writeFormParams (writer );
195201 }
196202 }
197203 }
198204
199205 private void writeFormParams (PathSegments segments ) {
200- for (MethodParam param : method .params ()) {
201- final String varName = param .name ();
202- ParamType paramType = param .paramType ();
203- PathSegments . Segment segment = segments .segment (varName );
206+ for (final MethodParam param : method .params ()) {
207+ final var varName = param .name ();
208+ final var paramType = param .paramType ();
209+ final var segment = segments .segment (varName );
204210 if (segment == null ) {
205211 // not a path or matrix parameter
206212 writeFormParam (param , paramType );
@@ -216,15 +222,17 @@ private void writeFormParam(MethodParam param, ParamType paramType) {
216222 writer .append (" .formParam(\" %s\" , %s)" , param .paramName (), param .name ()).eol ();
217223 }
218224 } else if (paramType == ParamType .FORM ) {
219- TypeElement formBeanType = ctx .typeElement (param .rawType ());
220- BeanParamReader form = new BeanParamReader (ctx , formBeanType , param .name (), param .shortType (), ParamType .FORMPARAM );
225+ final var formBeanType = ctx .typeElement (param .rawType ());
226+ final var form =
227+ new BeanParamReader (
228+ ctx , formBeanType , param .name (), param .shortType (), ParamType .FORMPARAM );
221229 form .writeFormParams (writer );
222230 }
223231 }
224232
225233 private void writeBody () {
226- for (MethodParam param : method .params ()) {
227- ParamType paramType = param .paramType ();
234+ for (final MethodParam param : method .params ()) {
235+ final var paramType = param .paramType ();
228236 if (paramType == ParamType .BODY ) {
229237 writer .append (" .body(%s)" , param .name ()).eol ();
230238 }
@@ -235,12 +243,12 @@ private void writePaths(Set<PathSegments.Segment> segments) {
235243 if (!segments .isEmpty ()) {
236244 writer .append (" " );
237245 }
238- for (PathSegments .Segment segment : segments ) {
246+ for (final PathSegments .Segment segment : segments ) {
239247 if (segment .isLiteral ()) {
240248 writer .append (".path(\" " ).append (segment .literalSection ()).append ("\" )" );
241249 } else {
242250 writer .append (".path(" ).append (segment .name ()).append (")" );
243- //TODO: matrix params
251+ // TODO: matrix params
244252 }
245253 }
246254 if (!segments .isEmpty ()) {
@@ -253,19 +261,18 @@ private boolean isMap(MethodParam param) {
253261 }
254262
255263 private boolean isMap (String type0 ) {
256- return type0 . equals ( "java.util.Map" );
264+ return "java.util.Map" . equals ( type0 );
257265 }
258266
259267 private boolean isList (String type0 ) {
260- return type0 . equals ( "java.util.List" );
268+ return "java.util.List" . equals ( type0 );
261269 }
262270
263271 private boolean isStream (String type0 ) {
264- return type0 . equals ( "java.util.stream.Stream" );
272+ return "java.util.stream.Stream" . equals ( type0 );
265273 }
266274
267275 private boolean isHttpResponse (String type0 ) {
268- return type0 . equals ( "java.net.http.HttpResponse" );
276+ return "java.net.http.HttpResponse" . equals ( type0 );
269277 }
270-
271278}
0 commit comments