3434import javax .lang .model .element .VariableElement ;
3535import javax .lang .model .type .DeclaredType ;
3636import javax .lang .model .type .TypeKind ;
37+ import javax .lang .model .type .TypeMirror ;
3738import javax .lang .model .util .ElementFilter ;
3839
3940import org .androidannotations .annotations .rest .Rest ;
40- import org .androidannotations .api .rest .EnhancedRestClient ;
4141import org .androidannotations .api .rest .RestErrorHandler ;
4242import org .androidannotations .helper .AnnotationHelper ;
4343import org .androidannotations .helper .ModelConstants ;
@@ -165,48 +165,59 @@ public void process(Element element, JCodeModel codeModel, EBeansHolder eBeansHo
165165 implementSetErrorHandler (holder , codeModel , eBeansHolder , methods );
166166 }
167167
168+ /**
169+ * Gets all of the methods of the class and includes the methods of any
170+ * implemented interfaces.
171+ *
172+ * @param typeElement
173+ * @return full list of methods.
174+ */
168175 private List <ExecutableElement > getMethods (TypeElement typeElement ) {
169176 List <? extends Element > enclosedElements = typeElement .getEnclosedElements ();
170177 List <ExecutableElement > methods = new ArrayList <ExecutableElement >(ElementFilter .methodsIn (enclosedElements ));
171178
172- if (!typeElement .getInterfaces ().isEmpty () && typeElement .getInterfaces ().get (0 ).toString ().equals (EnhancedRestClient .class .getName ())) {
173- DeclaredType dt = (DeclaredType ) typeElement .getInterfaces ().get (0 );
179+ // Add methods of the interfaces. These will be valid as they have gone
180+ // through the validator.
181+ for (TypeMirror iface : typeElement .getInterfaces ()) {
182+ DeclaredType dt = (DeclaredType ) iface ;
174183 methods .addAll (ElementFilter .methodsIn (dt .asElement ().getEnclosedElements ()));
175184 }
176185
177186 return methods ;
178187 }
179188
180- private void implementMapGetMethod (RestImplementationHolder holder , EBeansHolder eBeansHolder , List <ExecutableElement > methods , JFieldVar field , String methodName ) {
181- JMethod getCookieMethod = implementMethod (holder , null , eBeansHolder , methods , methodName , STRING , STRING );
182-
183- if (getCookieMethod != null ) {
184- JInvocation cookieValue = JExpr .invoke (field , "get" ).arg (getCookieMethod .params ().get (0 ));
185- getCookieMethod .body ()._return (cookieValue );
186- }
187- }
188-
189- private void implementMapPutMethod (RestImplementationHolder holder , EBeansHolder eBeansHolder , JCodeModel codeModel , List <ExecutableElement > methods , JFieldVar field , String methodName ) {
190- JMethod putMapMethod = implementMethod (holder , codeModel , eBeansHolder , methods , methodName , TypeKind .VOID .toString (), STRING , STRING );
191-
192- if (putMapMethod != null ) {
193- putMapMethod .body ().invoke (field , "put" ).arg (putMapMethod .params ().get (0 )).arg (putMapMethod .params ().get (1 ));
194- }
195- }
196-
189+ /**
190+ * Gets a method by name, return type and parameter types.
191+ *
192+ * Note: Maybe this method should be in a util class somewhere if it could
193+ * be reused by other processors.
194+ *
195+ * @param methods
196+ * @param methodName
197+ * @param returnType
198+ * @param parameterTypes
199+ * @return the ExecutableElement method object.
200+ */
197201 private ExecutableElement getMethod (List <ExecutableElement > methods , String methodName , String returnType , String ... parameterTypes ) {
198202 for (ExecutableElement method : methods ) {
199203 List <? extends VariableElement > parameters = method .getParameters ();
204+
205+ // Get the method return type or "VOID" if none.
200206 String methodReturnType = method .getReturnType ().getKind () == TypeKind .VOID ? TypeKind .VOID .toString () : method .getReturnType ().toString ();
201207
202208 if (parameters .size () == parameterTypes .length && methodReturnType .equals (returnType )) {
203209 if (methodName == null || method .getSimpleName ().toString ().equals (methodName )) {
210+ // At this point, method name, return type and number of
211+ // parameters are correct. Now we need to validate the
212+ // parameter types.
204213 boolean validMethod = true ;
205214
206215 for (int i = 0 ; i < parameters .size (); i ++) {
207216 VariableElement param = parameters .get (i );
208217
209218 if (!param .asType ().toString ().equals (parameterTypes [i ])) {
219+ // Parameter type does not match, this is not the
220+ // correct method.
210221 validMethod = false ;
211222 break ;
212223 }
@@ -222,15 +233,37 @@ private ExecutableElement getMethod(List<ExecutableElement> methods, String meth
222233 return null ;
223234 }
224235
236+ /**
237+ * Implements a method based on its name, return type and parameter types.
238+ * It will return the JMethod object to be given the full implementation.
239+ *
240+ * Note: Maybe this method should be in a util class somewhere if it could
241+ * be reused by other processors.
242+ *
243+ * @param holder
244+ * @param codeModel
245+ * @param eBeansHolder
246+ * @param methods
247+ * @param methodName
248+ * @param returnType
249+ * @param parameterTypes
250+ * @return The JMethod object created.
251+ */
225252 private JMethod implementMethod (RestImplementationHolder holder , JCodeModel codeModel , EBeansHolder eBeansHolder , List <ExecutableElement > methods , String methodName , String returnType , String ... parameterTypes ) {
253+ // First get the ExecutableElement method object from the util function.
226254 ExecutableElement method = getMethod (methods , methodName , returnType , parameterTypes );
227255 JMethod jmethod = null ;
228256
229257 if (method != null ) {
258+ // Get the return type or VOID if none.
230259 JType jcReturnType = returnType .equals (TypeKind .VOID .toString ()) ? codeModel .VOID : eBeansHolder .refClass (returnType );
260+
261+ // Create the implementation and annotate it with the Override
262+ // annotation.
231263 jmethod = holder .restImplementationClass .method (JMod .PUBLIC , jcReturnType , method .getSimpleName ().toString ());
232264 jmethod .annotate (Override .class );
233265
266+ // Create the parameters.
234267 for (int i = 0 ; i < method .getParameters ().size (); i ++) {
235268 VariableElement param = method .getParameters ().get (i );
236269 jmethod .param (eBeansHolder .refClass (parameterTypes [i ]), param .getSimpleName ().toString ());
@@ -240,6 +273,23 @@ private JMethod implementMethod(RestImplementationHolder holder, JCodeModel code
240273 return jmethod ;
241274 }
242275
276+ private void implementMapGetMethod (RestImplementationHolder holder , EBeansHolder eBeansHolder , List <ExecutableElement > methods , JFieldVar field , String methodName ) {
277+ JMethod getCookieMethod = implementMethod (holder , null , eBeansHolder , methods , methodName , STRING , STRING );
278+
279+ if (getCookieMethod != null ) {
280+ JInvocation cookieValue = JExpr .invoke (field , "get" ).arg (getCookieMethod .params ().get (0 ));
281+ getCookieMethod .body ()._return (cookieValue );
282+ }
283+ }
284+
285+ private void implementMapPutMethod (RestImplementationHolder holder , EBeansHolder eBeansHolder , JCodeModel codeModel , List <ExecutableElement > methods , JFieldVar field , String methodName ) {
286+ JMethod putMapMethod = implementMethod (holder , codeModel , eBeansHolder , methods , methodName , TypeKind .VOID .toString (), STRING , STRING );
287+
288+ if (putMapMethod != null ) {
289+ putMapMethod .body ().invoke (field , "put" ).arg (putMapMethod .params ().get (0 )).arg (putMapMethod .params ().get (1 ));
290+ }
291+ }
292+
243293 private void implementSetAuthentication (RestImplementationHolder holder , JCodeModel codeModel , EBeansHolder eBeansHolder , List <ExecutableElement > methods ) {
244294 JMethod setAuthMethod = implementMethod (holder , codeModel , eBeansHolder , methods , "setAuthentication" , TypeKind .VOID .toString (), "org.springframework.http.HttpAuthentication" );
245295
0 commit comments