// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/DefaultArgumentExtractor.java
// public interface DefaultArgumentExtractor<T> extends ArgumentExtractor<T> {
// 
// }
// 
// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/models/Action.java
// public enum Action {
//     commit,
//     reconsume;
//     Action(){}
// }
// 
// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/models/Result.java
// public class Result<T> {
//     private T result;
//     private Action action;
// 
//     public Result(T result) {
//         this.result = result;
//     }
// 
//     public Result(Action action) {
//         this.action = action;
//     }
// 
//     public Object getResult() {
//         return result;
//     }
// 
//     public Action getAction() {
//         return action;
//     }
// }
// 
// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/models/Results.java
// public class Results {
// 
//     public static <T> Result<T> next(T result){
//         return new Result(result);
//     }
// 
//     public static Result end(Action action){
//         return new Result(action);
//     }
// }


import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONException;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.github.mq.consumer.DefaultArgumentExtractor;
import com.github.mq.consumer.models.Action;
import com.github.mq.consumer.models.Result;
import com.github.mq.consumer.models.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
    public static <T> T instantiateComponent(DefaultListableBeanFactory beanFactory, Class<T> cls, Annotation annotation) {
        T extractor;

        try {
            extractor = beanFactory.getBean(cls);
        } catch (BeansException e) {
            try {
                extractor = (T) beanFactory.getBean(cls.getName());
            } catch (BeansException e1) {
                BeanDefinitionBuilder beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(cls.getName());
                beanFactory.registerBeanDefinition(cls.getName(), beanDefinition.getBeanDefinition());
                extractor = (T) beanFactory.getBean(cls.getName());
            }
        }
        return extractor;
    }


    public static class FastJsonExtractor implements ArgumentExtractor {


        @Override
        public Result extract(Message message,Class parameterClass, Annotation annotation) {
            Object object;
            try {
                object = JSONObject.parseObject(message.getBody(), parameterClass);
            } catch (JSONException e) {
                e.printStackTrace();
                logger.error(String.format("%s 反序列化失败 by : %s",
                        parameterClass, JSONObject.parseObject(message.getBody(), Object.class)));
// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/DefaultArgumentExtractor.java
// public interface DefaultArgumentExtractor<T> extends ArgumentExtractor<T> {
// 
// }
// 
// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/models/Action.java
// public enum Action {
//     commit,
//     reconsume;
//     Action(){}
// }
// 
// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/models/Result.java
// public class Result<T> {
//     private T result;
//     private Action action;
// 
//     public Result(T result) {
//         this.result = result;
//     }
// 
//     public Result(Action action) {
//         this.action = action;
//     }
// 
//     public Object getResult() {
//         return result;
//     }
// 
//     public Action getAction() {
//         return action;
//     }
// }
// 
// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/models/Results.java
// public class Results {
// 
//     public static <T> Result<T> next(T result){
//         return new Result(result);
//     }
// 
//     public static Result end(Action action){
//         return new Result(action);
//     }
// }


// Path: mq-aliyun-consumer-spring-boot-starter/src/main/java/com/github/mq/consumer/parms/ArgumentExtractors.java
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONException;
import com.aliyun.openservices.shade.com.alibaba.rocketmq.shade.com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.github.mq.consumer.DefaultArgumentExtractor;
import com.github.mq.consumer.models.Action;
import com.github.mq.consumer.models.Result;
import com.github.mq.consumer.models.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;

    public static <T> T instantiateComponent(DefaultListableBeanFactory beanFactory, Class<T> cls, Annotation annotation) {
        T extractor;

        try {
            extractor = beanFactory.getBean(cls);
        } catch (BeansException e) {
            try {
                extractor = (T) beanFactory.getBean(cls.getName());
            } catch (BeansException e1) {
                BeanDefinitionBuilder beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(cls.getName());
                beanFactory.registerBeanDefinition(cls.getName(), beanDefinition.getBeanDefinition());
                extractor = (T) beanFactory.getBean(cls.getName());
            }
        }
        return extractor;
    }


    public static class FastJsonExtractor implements ArgumentExtractor {


        @Override
        public Result extract(Message message,Class parameterClass, Annotation annotation) {
            Object object;
            try {
                object = JSONObject.parseObject(message.getBody(), parameterClass);
            } catch (JSONException e) {
                e.printStackTrace();
                logger.error(String.format("%s 反序列化失败 by : %s",
                        parameterClass, JSONObject.parseObject(message.getBody(), Object.class)));
                return Results.end(Action.commit);