1515 */
1616package org .androidannotations .handler .rest ;
1717
18- import java .util .List ;
19- import java .util .Locale ;
20- import java .util .TreeMap ;
21-
22- import javax .annotation .processing .ProcessingEnvironment ;
23- import javax .lang .model .element .Element ;
24- import javax .lang .model .element .ExecutableElement ;
25- import javax .lang .model .element .VariableElement ;
26- import javax .lang .model .type .TypeKind ;
27-
18+ import com .sun .codemodel .*;
2819import org .androidannotations .handler .BaseAnnotationHandler ;
2920import org .androidannotations .helper .APTCodeModelHelper ;
3021import org .androidannotations .helper .CanonicalNameConstants ;
3324import org .androidannotations .model .AnnotationElements ;
3425import org .androidannotations .process .IsValid ;
3526
36- import com .sun .codemodel .JArray ;
37- import com .sun .codemodel .JBlock ;
38- import com .sun .codemodel .JCatchBlock ;
39- import com .sun .codemodel .JClass ;
40- import com .sun .codemodel .JConditional ;
41- import com .sun .codemodel .JExpr ;
42- import com .sun .codemodel .JExpression ;
43- import com .sun .codemodel .JFieldRef ;
44- import com .sun .codemodel .JForEach ;
45- import com .sun .codemodel .JInvocation ;
46- import com .sun .codemodel .JMethod ;
47- import com .sun .codemodel .JMod ;
48- import com .sun .codemodel .JOp ;
49- import com .sun .codemodel .JTryBlock ;
50- import com .sun .codemodel .JType ;
51- import com .sun .codemodel .JVar ;
27+ import javax .annotation .processing .ProcessingEnvironment ;
28+ import javax .lang .model .element .Element ;
29+ import javax .lang .model .element .ExecutableElement ;
30+ import javax .lang .model .element .VariableElement ;
31+ import javax .lang .model .type .TypeKind ;
32+ import java .util .List ;
33+ import java .util .Locale ;
34+ import java .util .TreeMap ;
5235
5336public abstract class RestMethodHandler extends BaseAnnotationHandler <RestHolder > {
5437
@@ -81,7 +64,7 @@ public void process(Element element, RestHolder holder) {
8164 JMethod method = holder .getGeneratedClass ().method (JMod .PUBLIC , methodReturnClass , methodName );
8265 method .annotate (Override .class );
8366 TreeMap <String , JVar > params = addMethodParams (executableElement , holder , method );
84- JBlock methodBody = method . body ( );
67+ JBlock methodBody = new JBlock ( false , false );
8568
8669 // RestTemplate exchange() method call
8770 JInvocation exchangeCall = JExpr .invoke (holder .getRestTemplateField (), "exchange" );
@@ -94,18 +77,14 @@ public void process(Element element, RestHolder holder) {
9477 exchangeCall .arg (urlVariables );
9578 }
9679
97- JExpression returnCall = exchangeCall ;
98- JExpression result = setCookies (executableElement , holder , methodBody , exchangeCall );
99- if (result != null ) {
100- returnCall = result ;
101- }
102-
103- if (methodReturnVoid && result == null ) {
104- insertRestTryCatchBlock (holder , methodBody , exchangeCall , methodReturnVoid );
80+ JExpression response = setCookies (executableElement , holder , methodBody , exchangeCall );
81+ if (methodReturnVoid && response .equals (exchangeCall )) {
82+ methodBody .add (exchangeCall );
10583 } else if (!methodReturnVoid ) {
106- returnCall = addResultCallMethod (returnCall , methodReturnClass );
107- insertRestTryCatchBlock (holder , methodBody , returnCall , methodReturnVoid );
84+ methodBody ._return (addResultCallMethod (response , methodReturnClass ));
10885 }
86+ methodBody = surroundWithRestTryCatch (holder , methodBody , methodReturnVoid );
87+ method .body ().add (methodBody );
10988 }
11089
11190 protected JClass getMethodReturnClass (Element element , RestHolder holder ) {
@@ -171,13 +150,14 @@ protected JExpression addResultCallMethod(JExpression exchangeCall, JClass metho
171150 return exchangeCall ;
172151 }
173152
174- private JFieldRef setCookies (ExecutableElement executableElement , RestHolder restHolder , JBlock methodBody , JInvocation exchangeCall ) {
153+ private JExpression setCookies (ExecutableElement executableElement , RestHolder restHolder , JBlock methodBody , JInvocation exchangeCall ) {
175154 String [] settingCookies = restAnnotationHelper .settingCookies (executableElement );
176155 if (settingCookies != null ) {
177156 boolean methodReturnVoid = executableElement .getReturnType ().getKind () == TypeKind .VOID ;
178- JClass methodReturnClass = getMethodReturnClass (executableElement , restHolder );
179157
180- JClass responseEntityClass = classes ().RESPONSE_ENTITY .narrow (methodReturnVoid ? codeModel ().VOID : methodReturnClass );
158+ JClass exchangeResponseClass = restAnnotationHelper .retrieveResponseClass (executableElement .getReturnType (), restHolder );
159+ JType narrowType = exchangeResponseClass == null || methodReturnVoid ? codeModel ().VOID : exchangeResponseClass ;
160+ JClass responseEntityClass = classes ().RESPONSE_ENTITY .narrow (narrowType );
181161 JVar responseEntity = methodBody .decl (responseEntityClass , "response" , exchangeCall );
182162
183163 // set cookies
@@ -214,9 +194,9 @@ private JFieldRef setCookies(ExecutableElement executableElement, RestHolder res
214194 thenBlock .invoke (restHolder .getAvailableCookiesField (), "put" ).arg (innerForEach .var ()).arg (cookieValue );
215195 thenBlock ._break ();
216196
217- return JExpr . ref ( responseEntity . name ()) ;
197+ return responseEntity ;
218198 }
219- return null ;
199+ return exchangeCall ;
220200 }
221201
222202 /**
@@ -227,14 +207,11 @@ private JFieldRef setCookies(ExecutableElement executableElement, RestHolder res
227207 * if void). If the handler isn't set, it will re-throw the exception so
228208 * that it behaves as it did previous to this feature.
229209 */
230- private void insertRestTryCatchBlock (RestHolder holder , JBlock body , JExpression returnCall , boolean methodReturnVoid ) {
231- JTryBlock tryBlock = body . _try ( );
210+ private JBlock surroundWithRestTryCatch (RestHolder holder , JBlock block , boolean methodReturnVoid ) {
211+ JBlock newBlock = new JBlock ( false , false );
232212
233- if (methodReturnVoid ) {
234- tryBlock .body ().add ((JInvocation ) returnCall );
235- } else {
236- tryBlock .body ()._return (returnCall );
237- }
213+ JTryBlock tryBlock = newBlock ._try ();
214+ codeModelHelper .copy (block , tryBlock .body ());
238215
239216 JCatchBlock jCatch = tryBlock ._catch (classes ().REST_CLIENT_EXCEPTION );
240217
@@ -254,5 +231,7 @@ private void insertRestTryCatchBlock(RestHolder holder, JBlock body, JExpression
254231
255232 // re-throw the exception if handler wasn't set.
256233 conditional ._else ()._throw (exceptionParam );
234+
235+ return newBlock ;
257236 }
258237}
0 commit comments