反射优化
Showing
1 changed file
with
10 additions
and
8 deletions
| ... | @@ -18,6 +18,7 @@ import java.lang.reflect.Parameter; | ... | @@ -18,6 +18,7 @@ import java.lang.reflect.Parameter; |
| 18 | */ | 18 | */ |
| 19 | public class EncodeLifeCycle implements MethodAnnotationLifeCycle<Encode, Object> { | 19 | public class EncodeLifeCycle implements MethodAnnotationLifeCycle<Encode, Object> { |
| 20 | 20 | ||
| 21 | private Integer bodyIndex; | ||
| 21 | 22 | ||
| 22 | /** | 23 | /** |
| 23 | * 当方法调用时调用此方法,此时还没有执行请求发送 | 24 | * 当方法调用时调用此方法,此时还没有执行请求发送 |
| ... | @@ -27,11 +28,7 @@ public class EncodeLifeCycle implements MethodAnnotationLifeCycle<Encode, Object | ... | @@ -27,11 +28,7 @@ public class EncodeLifeCycle implements MethodAnnotationLifeCycle<Encode, Object |
| 27 | @Override | 28 | @Override |
| 28 | public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) { | 29 | public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) { |
| 29 | String value = (String) getAttribute(request, "value"); | 30 | String value = (String) getAttribute(request, "value"); |
| 30 | Parameter[] parameters = method.getMethod().getParameters(); | 31 | Object body = args[bodyIndex]; |
| 31 | for (int i = 0; i < parameters.length; i++) { | ||
| 32 | EncodeBody annotation = parameters[i].getAnnotation(EncodeBody.class); | ||
| 33 | if (annotation != null) { | ||
| 34 | Object body = args[i]; | ||
| 35 | String jsonString = JSON.toJSONString(body); | 32 | String jsonString = JSON.toJSONString(body); |
| 36 | String encode = DecryptUtils.encode(value, jsonString); | 33 | String encode = DecryptUtils.encode(value, jsonString); |
| 37 | EncryptEntity encodeBody = new EncryptEntity(encode); | 34 | EncryptEntity encodeBody = new EncryptEntity(encode); |
| ... | @@ -40,12 +37,17 @@ public class EncodeLifeCycle implements MethodAnnotationLifeCycle<Encode, Object | ... | @@ -40,12 +37,17 @@ public class EncodeLifeCycle implements MethodAnnotationLifeCycle<Encode, Object |
| 40 | forestBody.clear(); | 37 | forestBody.clear(); |
| 41 | request.addBody(encodeBody); | 38 | request.addBody(encodeBody); |
| 42 | } | 39 | } |
| 43 | } | ||
| 44 | } | ||
| 45 | 40 | ||
| 46 | 41 | ||
| 47 | @Override | 42 | @Override |
| 48 | public void onMethodInitialized(ForestMethod method, Encode annotation) { | 43 | public void onMethodInitialized(ForestMethod method, Encode annotation) { |
| 49 | 44 | Parameter[] parameters = method.getMethod().getParameters(); | |
| 45 | for (int i = 0; i < parameters.length; i++) { | ||
| 46 | Parameter parameter = parameters[i]; | ||
| 47 | EncodeBody body = parameter.getAnnotation(EncodeBody.class); | ||
| 48 | if (body != null) { | ||
| 49 | bodyIndex = i; | ||
| 50 | } | ||
| 51 | } | ||
| 50 | } | 52 | } |
| 51 | } | 53 | } | ... | ... |
-
Please register or sign in to post a comment