Commit 2c1550de 2c1550de2d745fd9394d6b2fae395477c1045b44 by 文鑫

反射优化

1 parent 4a8a31f9
...@@ -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,25 +28,26 @@ public class EncodeLifeCycle implements MethodAnnotationLifeCycle<Encode, Object ...@@ -27,25 +28,26 @@ 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 String jsonString = JSON.toJSONString(body);
32 EncodeBody annotation = parameters[i].getAnnotation(EncodeBody.class); 33 String encode = DecryptUtils.encode(value, jsonString);
33 if (annotation != null) { 34 EncryptEntity encodeBody = new EncryptEntity(encode);
34 Object body = args[i]; 35 ForestBody forestBody = request.getBody();
35 String jsonString = JSON.toJSONString(body); 36 //替换body
36 String encode = DecryptUtils.encode(value, jsonString); 37 forestBody.clear();
37 EncryptEntity encodeBody = new EncryptEntity(encode); 38 request.addBody(encodeBody);
38 ForestBody forestBody = request.getBody();
39 //替换body
40 forestBody.clear();
41 request.addBody(encodeBody);
42 }
43 }
44 } 39 }
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 }
......