/*
 *  Copyright 1999-2019 Seata.io Group.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package io.seata.rm.tcc.interceptor;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.seata.common.exception.FrameworkException;
import io.seata.common.util.StringUtils;
import io.seata.rm.tcc.api.BusinessActionContextParameter;

/**
 * Extracting TCC Context from Method
 *
 * @author zhangsen
 */
public class ActionContextUtil {

    private ActionContextUtil() {

    }

    /**
     * Extracting context data from parameters
     *
     * @param targetParam the target param
     * @return map map
     */
    public static Map<String, Object> fetchContextFromObject(Object targetParam) {
        try {
            Map<String, Object> context = new HashMap<>(8);
            List<Field> fields = new ArrayList<>();
            getAllField(targetParam.getClass(), fields);
            for (Field f : fields) {
                String fieldName = f.getName();
                BusinessActionContextParameter annotation = f.getAnnotation(BusinessActionContextParameter.class);

                if (annotation != null) {
                    f.setAccessible(true);
                    Object paramObject = f.get(targetParam);
                    int index = annotation.index();
                    if (index >= 0) {
                        @SuppressWarnings("unchecked")
                        Object targetObject = ((List<Object>)paramObject).get(index);
                        if (annotation.isParamInProperty()) {
                            context.putAll(fetchContextFromObject(targetObject));
                        } else {
                            if (StringUtils.isBlank(annotation.paramName())) {
                                context.put(fieldName, paramObject);
                            } else {
                                context.put(annotation.paramName(), paramObject);
                            }
                        }
                    } else {
                        if (annotation.isParamInProperty()) {
                            context.putAll(fetchContextFromObject(paramObject));
                        } else {
                            if (StringUtils.isBlank(annotation.paramName())) {
                                context.put(fieldName, paramObject);
                            } else {
                                context.put(annotation.paramName(), paramObject);
                            }
                        }
                    }
                }
            }
            return context;
        } catch (Throwable t) {
            throw new FrameworkException(t, "fetchContextFromObject failover");
        }
    }

    /**
     * Gets all field.
     *
     * @param interFace the inter face
     * @param fields    the fields
     */
    public static void getAllField(Class<?> interFace, List<Field> fields) {
        if (interFace == Object.class || interFace.isInterface()) {
            return;
        }
        Field[] field = interFace.getDeclaredFields();
        fields.addAll(Arrays.asList(field));
        getAllField(interFace.getSuperclass(), fields);
    }

}

@@ -1500,24 +1500,25 @@
 ), fields);%0A
+%0A
             
@@ -1900,24 +1900,86 @@
 on.index();%0A
+                    if (annotation.isParamInProperty()) %7B%0A    
             
@@ -2000,24 +2000,28 @@
 dex %3E= 0) %7B%0A
+    
             
@@ -2083,24 +2083,28 @@
             
+    
 Object targe
@@ -2128,16 +2128,17 @@
 Object%3E)
+ 
 paramObj
@@ -2158,70 +2158,8 @@
 x);%0A
-                        if (annotation.isParamInProperty()) %7B%0A
     
@@ -2301,233 +2301,46 @@
     
-if (StringUtils.isBlank(annotation.paramName())) %7B%0A                                context.put(fieldName, paramObject);%0A                            %7D else %7B%0A                                context.put(annotation.paramName(), 
+context.putAll(fetchContextFromObject(
 para
@@ -2339,38 +2339,35 @@
 ect(paramObject)
+)
 ;%0A
-    
                 
@@ -2380,34 +2380,8 @@
   %7D%0A
-                        %7D%0A
     
@@ -2409,188 +2409,8 @@
 e %7B%0A
-                        if (annotation.isParamInProperty()) %7B%0A                            context.putAll(fetchContextFromObject(paramObject));%0A                        %7D else %7B%0A    
     
@@ -2500,36 +2500,32 @@
                 
-    
 context.put(fiel
@@ -2561,36 +2561,32 @@
                 
- 
-   
 %7D else %7B%0A       
@@ -2570,36 +2570,32 @@
        %7D else %7B%0A
-    
                 
@@ -2660,38 +2660,8 @@
 t);%0A
-                            %7D%0A
     

add methods to reduce redundant code (#2494)