5
5
import java .util .Arrays ;
6
6
import java .util .List ;
7
7
import java .util .Optional ;
8
+ import java .util .function .Predicate ;
8
9
import java .util .stream .Collectors ;
9
10
import java .util .stream .Stream ;
10
11
@@ -60,6 +61,7 @@ public class MethodReader {
60
61
61
62
private final PathSegments pathSegments ;
62
63
private final boolean hasValid ;
64
+ private final List <ExecutableElement > superMethods ;
63
65
64
66
MethodReader (ControllerReader bean , ExecutableElement element , ExecutableType actualExecutable , ProcessingContext ctx ) {
65
67
this .ctx = ctx ;
@@ -69,18 +71,46 @@ public class MethodReader {
69
71
this .actualParams = (actualExecutable == null ) ? null : actualExecutable .getParameterTypes ();
70
72
this .isVoid = element .getReturnType ().getKind () == TypeKind .VOID ;
71
73
this .methodRoles = Util .findRoles (element );
72
- this .javadoc = Javadoc .parse (ctx .docComment (element ));
73
74
this .produces = produces (bean );
74
- this .apiResponses = getApiResponses ();
75
75
initWebMethodViaAnnotation ();
76
+
77
+ this .superMethods =
78
+ ctx .getSuperMethods (element .getEnclosingElement (), element .toString ().replace ("()" , "" ));
79
+
80
+ this .apiResponses = getApiResponses ();
81
+ this .javadoc =
82
+ Optional .of (Javadoc .parse (ctx .docComment (element )))
83
+ .filter (Predicate .not (Javadoc ::isEmpty ))
84
+ .orElseGet (
85
+ () ->
86
+ superMethods .stream ()
87
+ .map (e -> Javadoc .parse (ctx .docComment (e )))
88
+ .filter (Predicate .not (Javadoc ::isEmpty ))
89
+ .findFirst ()
90
+ .orElse (Javadoc .parse ("" )));
91
+
76
92
if (isWebMethod ()) {
77
- Annotation jakartaValidAnnotation = null ;
93
+
94
+ Class <Annotation > jakartaValidAnnotation ;
78
95
try {
79
- jakartaValidAnnotation = findAnnotation ( jakartaValidAnnotation () );
96
+ jakartaValidAnnotation = jakartaValidAnnotation ();
80
97
} catch (final ClassNotFoundException e ) {
81
- // ignore
98
+ jakartaValidAnnotation = null ;
82
99
}
83
- this .hasValid = findAnnotation (Valid .class ) != null || jakartaValidAnnotation != null ;
100
+
101
+ final var jakartaAnnotation = jakartaValidAnnotation ;
102
+
103
+ this .hasValid =
104
+ findAnnotation (Valid .class ) != null
105
+ || (jakartaValidAnnotation != null && findAnnotation (jakartaValidAnnotation ) != null )
106
+ || superMethods .stream ()
107
+ .map (
108
+ e ->
109
+ findAnnotation (Valid .class , e ) != null
110
+ || (jakartaAnnotation != null
111
+ && findAnnotation (jakartaAnnotation , e ) != null ))
112
+ .anyMatch (b -> b );
113
+
84
114
this .pathSegments = PathSegments .parse (Util .combinePath (bean .path (), webMethodPath ));
85
115
} else {
86
116
this .hasValid = false ;
@@ -99,31 +129,31 @@ public String toString() {
99
129
}
100
130
101
131
private void initWebMethodViaAnnotation () {
102
- Form form = findAnnotation (Form .class );
132
+ final var form = findAnnotation (Form .class );
103
133
if (form != null ) {
104
134
this .formMarker = true ;
105
135
}
106
- Get get = findAnnotation (Get .class );
136
+ final var get = findAnnotation (Get .class );
107
137
if (get != null ) {
108
138
initSetWebMethod (WebMethod .GET , get .value ());
109
139
return ;
110
140
}
111
- Put put = findAnnotation (Put .class );
141
+ final var put = findAnnotation (Put .class );
112
142
if (put != null ) {
113
143
initSetWebMethod (WebMethod .PUT , put .value ());
114
144
return ;
115
145
}
116
- Post post = findAnnotation (Post .class );
146
+ final var post = findAnnotation (Post .class );
117
147
if (post != null ) {
118
148
initSetWebMethod (WebMethod .POST , post .value ());
119
149
return ;
120
150
}
121
- Patch patch = findAnnotation (Patch .class );
151
+ final var patch = findAnnotation (Patch .class );
122
152
if (patch != null ) {
123
153
initSetWebMethod (WebMethod .PATCH , patch .value ());
124
154
return ;
125
155
}
126
- Delete delete = findAnnotation (Delete .class );
156
+ final var delete = findAnnotation (Delete .class );
127
157
if (delete != null ) {
128
158
initSetWebMethod (WebMethod .DELETE , delete .value ());
129
159
}
@@ -149,19 +179,34 @@ private List<OpenAPIResponse> getApiResponses() {
149
179
.map (OpenAPIResponses ::value )
150
180
.flatMap (Arrays ::stream );
151
181
152
- return Stream .concat (container , Arrays .stream (element .getAnnotationsByType (OpenAPIResponse .class )))
153
- .collect (Collectors .toList ());
182
+ final var methodResponses =
183
+ Stream .concat (
184
+ container , Arrays .stream (element .getAnnotationsByType (OpenAPIResponse .class )));
154
185
186
+ final var superMethodResponses =
187
+ superMethods .stream ()
188
+ .flatMap (
189
+ m -> Stream .concat (
190
+ Optional .ofNullable (findAnnotation (OpenAPIResponses .class ,m )).stream ()
191
+ .map (OpenAPIResponses ::value )
192
+ .flatMap (Arrays ::stream ),
193
+ Arrays .stream (m .getAnnotationsByType (OpenAPIResponse .class ))));
155
194
195
+ return Stream .concat (methodResponses , superMethodResponses ).collect (Collectors .toList ());
156
196
}
157
197
158
198
public <A extends Annotation > A findAnnotation (Class <A > type ) {
159
- A annotation = element .getAnnotation (type );
199
+
200
+ return findAnnotation (type , element );
201
+ }
202
+
203
+ public <A extends Annotation > A findAnnotation (Class <A > type , ExecutableElement elem ) {
204
+ final var annotation = elem .getAnnotation (type );
160
205
if (annotation != null ) {
161
206
return annotation ;
162
207
}
163
208
164
- return bean .findMethodAnnotation (type , element );
209
+ return bean .findMethodAnnotation (type , elem );
165
210
}
166
211
167
212
private List <String > addTagsToList (Element element , List <String > list ) {
@@ -172,15 +217,17 @@ private List<String> addTagsToList(Element element, List<String> list) {
172
217
list .add (element .getAnnotation (Tag .class ).name ());
173
218
}
174
219
if (element .getAnnotation (Tags .class ) != null ) {
175
- for (Tag tag : element .getAnnotation (Tags .class ).value ())
220
+ for (final Tag tag : element .getAnnotation (Tags .class ).value ())
176
221
list .add (tag .name ());
177
222
}
178
223
return list ;
179
224
}
180
225
181
226
public List <String > tags () {
182
- List <String > tags = new ArrayList <>();
183
- tags = addTagsToList (element , tags );
227
+ final List <String > tags = new ArrayList <>();
228
+ addTagsToList (element , tags );
229
+ superMethods .forEach (e -> addTagsToList (e , tags ));
230
+
184
231
return addTagsToList (element .getEnclosingElement (), tags );
185
232
}
186
233
@@ -191,20 +238,20 @@ void read() {
191
238
192
239
// non-path parameters default to form or query parameters based on the
193
240
// existence of @Form annotation on the method
194
- ParamType defaultParamType = (formMarker ) ? ParamType .FORMPARAM : ParamType .QUERYPARAM ;
241
+ final var defaultParamType = (formMarker ) ? ParamType .FORMPARAM : ParamType .QUERYPARAM ;
195
242
196
243
final List <? extends VariableElement > parameters = element .getParameters ();
197
- for (int i = 0 ; i < parameters .size (); i ++) {
198
- VariableElement p = parameters .get (i );
244
+ for (var i = 0 ; i < parameters .size (); i ++) {
245
+ final VariableElement p = parameters .get (i );
199
246
TypeMirror typeMirror ;
200
247
if (actualParams != null ) {
201
248
typeMirror = actualParams .get (i );
202
249
} else {
203
250
typeMirror = p .asType ();
204
251
}
205
- String rawType = Util .typeDef (typeMirror );
206
- UType type = Util .parse (typeMirror .toString ());
207
- MethodParam param = new MethodParam (p , type , rawType , ctx , defaultParamType , formMarker );
252
+ final var rawType = Util .typeDef (typeMirror );
253
+ final var type = Util .parse (typeMirror .toString ());
254
+ final var param = new MethodParam (p , type , rawType , ctx , defaultParamType , formMarker );
208
255
params .add (param );
209
256
param .addImports (bean );
210
257
}
@@ -286,7 +333,7 @@ public String simpleName() {
286
333
}
287
334
288
335
public boolean isFormBody () {
289
- for (MethodParam param : params ) {
336
+ for (final MethodParam param : params ) {
290
337
if (param .isForm ()) {
291
338
return true ;
292
339
}
@@ -295,7 +342,7 @@ public boolean isFormBody() {
295
342
}
296
343
297
344
public String bodyType () {
298
- for (MethodParam param : params ) {
345
+ for (final MethodParam param : params ) {
299
346
if (param .isBody ()) {
300
347
return param .shortType ();
301
348
}
@@ -304,7 +351,7 @@ public String bodyType() {
304
351
}
305
352
306
353
public String bodyName () {
307
- for (MethodParam param : params ) {
354
+ for (final MethodParam param : params ) {
308
355
if (param .isBody ()) {
309
356
return param .name ();
310
357
}
0 commit comments