|
40 | 40 |
|
41 | 41 | import com.sun.codemodel.JArray; |
42 | 42 | import com.sun.codemodel.JBlock; |
| 43 | +import com.sun.codemodel.JCatchBlock; |
43 | 44 | import com.sun.codemodel.JClass; |
44 | 45 | import com.sun.codemodel.JCodeModel; |
| 46 | +import com.sun.codemodel.JConditional; |
45 | 47 | import com.sun.codemodel.JExpr; |
46 | 48 | import com.sun.codemodel.JExpression; |
47 | 49 | import com.sun.codemodel.JForEach; |
48 | 50 | import com.sun.codemodel.JInvocation; |
49 | 51 | import com.sun.codemodel.JMethod; |
50 | 52 | import com.sun.codemodel.JMod; |
| 53 | +import com.sun.codemodel.JOp; |
| 54 | +import com.sun.codemodel.JTryBlock; |
51 | 55 | import com.sun.codemodel.JType; |
52 | 56 | import com.sun.codemodel.JVar; |
53 | 57 |
|
@@ -204,13 +208,40 @@ protected JExpression addResultCallMethod(JExpression restCall, MethodProcessorH |
204 | 208 |
|
205 | 209 | private void insertRestCallInBody(JBlock body, JExpression restCall, MethodProcessorHolder methodHolder, boolean methodReturnVoid, boolean usesInstance) { |
206 | 210 | if (methodReturnVoid && !usesInstance && restCall instanceof JInvocation) { |
207 | | - body.add((JInvocation) restCall); |
| 211 | + insertRestTryCatchBlock(body, restCall, methodHolder, methodReturnVoid); |
208 | 212 | } else if (!methodReturnVoid) { |
209 | | - restCall = addResultCallMethod(restCall, methodHolder); |
210 | | - body._return(restCall); |
| 213 | + insertRestTryCatchBlock(body, addResultCallMethod(restCall, methodHolder), methodHolder, methodReturnVoid); |
211 | 214 | } |
212 | 215 | } |
213 | 216 |
|
| 217 | + private void insertRestTryCatchBlock(JBlock body, JExpression restCall, MethodProcessorHolder methodHolder, boolean methodReturnVoid) { |
| 218 | + RestImplementationHolder holder = restImplementationsHolder.getEnclosingHolder(methodHolder.getElement()); |
| 219 | + |
| 220 | + JTryBlock tryBlock = body._try(); |
| 221 | + |
| 222 | + if (methodReturnVoid) { |
| 223 | + tryBlock.body().add((JInvocation) restCall); |
| 224 | + } else { |
| 225 | + tryBlock.body()._return(restCall); |
| 226 | + } |
| 227 | + |
| 228 | + JCatchBlock jcatch = tryBlock._catch(methodHolder.getHolder().classes().REST_CLIENT_EXCEPTION); |
| 229 | + |
| 230 | + JBlock catchBlock = jcatch.body(); |
| 231 | + JConditional con = catchBlock._if(JOp.ne(holder.restErrorHandlerField, JExpr._null())); |
| 232 | + JVar excParam = jcatch.param("e"); |
| 233 | + |
| 234 | + JBlock thenBlock = con._then(); |
| 235 | + |
| 236 | + thenBlock.add(holder.restErrorHandlerField.invoke("onRestClientExceptionThrown").arg(excParam)); |
| 237 | + |
| 238 | + if (!methodReturnVoid) { |
| 239 | + thenBlock._return(JExpr._null()); |
| 240 | + } |
| 241 | + |
| 242 | + con._else()._throw(excParam); |
| 243 | + } |
| 244 | + |
214 | 245 | private JVar generateHashMapVar(RestImplementationHolder holder, MethodProcessorHolder methodHolder) { |
215 | 246 | ExecutableElement element = (ExecutableElement) methodHolder.getElement(); |
216 | 247 | JCodeModel codeModel = methodHolder.getCodeModel(); |
|
0 commit comments