content: package com.alibaba.jvm.sandbox.repeater.plugin.mybatis;
  
  import com.alibaba.jvm.sandbox.api.event.BeforeEvent;
  import com.alibaba.jvm.sandbox.repeater.plugin.core.impl.api.DefaultInvocationProcessor;
  import com.alibaba.jvm.sandbox.repeater.plugin.domain.Identity;
  import com.alibaba.jvm.sandbox.repeater.plugin.domain.Invocation;
  import com.alibaba.jvm.sandbox.repeater.plugin.domain.InvokeType;
  import org.apache.commons.lang3.reflect.FieldUtils;
  import org.apache.commons.lang3.reflect.MethodUtils;
  
  import java.lang.reflect.Field;
  import java.util.HashMap;
  
  /**
   * <p>
   *
   * @author zhaoyb1990
   */
  class MybatisProcessor extends DefaultInvocationProcessor {
  
      MybatisProcessor(InvokeType type) {
          super(type);
      }
  
      @Override
      public Identity assembleIdentity(BeforeEvent event) {
          Object mapperMethod = event.target;
          // SqlCommand = MapperMethod.command
          Field field = FieldUtils.getDeclaredField(mapperMethod.getClass(), "command", true);
          if (field == null) {
              return new Identity(InvokeType.MYBATIS.name(), "Unknown", "Unknown", new HashMap<String, String>(1));
          }
          try {
              Object command = field.get(mapperMethod);
              Object name = MethodUtils.invokeMethod(command, "getName");
              Object type = MethodUtils.invokeMethod(command, "getType");
              return new Identity(InvokeType.MYBATIS.name(), type.toString(), name.toString(), new HashMap<String, String>(1));
          } catch (Exception e) {
              return new Identity(InvokeType.MYBATIS.name(), "Unknown", "Unknown", new HashMap<String, String>(1));
          }
      }
  
      @Override
      public Object[] assembleRequest(BeforeEvent event) {
          // MapperMethod#execute(SqlSession sqlSession, Object[] args)
          // args可能存在不可序序列化异常（例如使用tk.mybatis)
          return new Object[]{event.argumentArray[1]};
      }
  
      @Override
      public boolean inTimeSerializeRequest(Invocation invocation, BeforeEvent event) {
          return false;
      }
  }
  