Skip to content

Commit

Permalink
mybatis ognl支持
Browse files Browse the repository at this point in the history
  • Loading branch information
yinjihuan committed May 15, 2022
1 parent d4f93f6 commit 7c4de93
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
7 changes: 7 additions & 0 deletions fox-mock-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -109,6 +114,7 @@
<pattern>ognl</pattern>
<shadedPattern>com.cxytiandi.foxmock.agent.ognl</shadedPattern>
</relocation>

</relocations>
<artifactSet>
<includes>
Expand All @@ -119,6 +125,7 @@
<include>ch.qos.logback:logback-core</include>
<include>com.alibaba.arthas:arthas-repackage-logger</include>
<include>ognl:ognl</include>
<include>org.mybatis:mybatis</include>
</includes>
</artifactSet>
<filters>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cxytiandi.foxmock.agent.factory;

import com.cxytiandi.foxmock.agent.model.MockInfo;
import com.cxytiandi.foxmock.agent.utils.JsonUtils;

/**
* @作者 尹吉欢
* @个人微信 jihuan900
* @微信公众号 猿天地
* @GitHub https://github.com/yinjihuan
* @作者介绍 http://cxytiandi.com/about
* @时间 2022-05-15 22:59
*/
public class MockInfoFactory {

public static MockInfo create(String data) {
MockInfo mockInfo = null;
if (data.contains("f_mock_data") || data.contains("f_ognl_express")) {
mockInfo = JsonUtils.fromJson(data, MockInfo.class);
} else {
mockInfo = new MockInfo();
mockInfo.setMockData(data);
}
return mockInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
import com.cxytiandi.foxmock.agent.express.Express;
import com.cxytiandi.foxmock.agent.express.OgnlExpressException;
import com.cxytiandi.foxmock.agent.express.ExpressFactory;
import com.cxytiandi.foxmock.agent.factory.MockInfoFactory;
import com.cxytiandi.foxmock.agent.model.MockInfo;
import com.cxytiandi.foxmock.agent.storage.StorageHelper;
import com.cxytiandi.foxmock.agent.utils.JsonUtils;
import com.cxytiandi.foxmock.agent.utils.ReflectionUtils;
import com.cxytiandi.foxmock.agent.utils.StringUtils;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -52,6 +57,10 @@ public static boolean filter(Object[] args, String express) {

public static Object filterAndConvertDataByMybatisQuery(Object[] args) {
Object obj = filterAndConvertData(args);
if (obj == null) {
return null;
}

// 集合直接返回
if (obj instanceof List) {
return obj;
Expand All @@ -69,6 +78,7 @@ public static Object filterAndConvertDataByMybatisUpdate(Object[] args) {

private static Object filterAndConvertData(Object[] args) {
MappedStatement ms = (MappedStatement) args[0];
Object parameterObject = args[1];

String msId = ms.getId();
String className = msId.substring(0, msId.lastIndexOf("."));
Expand All @@ -80,8 +90,24 @@ private static Object filterAndConvertData(Object[] args) {
return null;
}

Type genericReturnType = ReflectionUtils.getGenericReturnType(className, methodName);
Object obj = JsonUtils.parseByType(data, genericReturnType);
return obj;
List<Object> argsList = new ArrayList<>();
BoundSql boundSql = ms.getBoundSql(parameterObject);
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();

for (ParameterMapping mapping : parameterMappings) {
HashMap parameterMap = (HashMap) parameterObject;
Object value = parameterMap.get(mapping.getProperty());
argsList.add(value);
}

MockInfo mockInfo = MockInfoFactory.create(data);
boolean filter = filter(argsList.toArray(new Object[argsList.size()]), mockInfo.getOgnlExpress());
if (filter) {
Type genericReturnType = ReflectionUtils.getGenericReturnType(className, methodName);
Object obj = JsonUtils.parseByType(mockInfo.getMockData(), genericReturnType);
return obj;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.alibaba.arthas.deps.org.slf4j.Logger;
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
import com.cxytiandi.foxmock.agent.constant.FoxMockConstant;
import com.cxytiandi.foxmock.agent.factory.MockInfoFactory;
import com.cxytiandi.foxmock.agent.model.ClassInfo;
import com.cxytiandi.foxmock.agent.model.MockInfo;
import com.cxytiandi.foxmock.agent.storage.StorageHelper;
Expand Down Expand Up @@ -76,14 +77,8 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
String data = StorageHelper.get(key);
if (Objects.nonNull(data)) {
match = true;
MockInfo mockInfo = null;
LOG.info(String.format("mock methods %s, mock data is %s", key, data));
if (data.contains("f_mock_data") || data.contains("f_ognl_express")) {
mockInfo = JsonUtils.fromJson(data, MockInfo.class);
} else {
mockInfo = new MockInfo();
mockInfo.setMockData(data);
}
MockInfo mockInfo = MockInfoFactory.create(data);
updateMethod(mockInfo, method, className, methodName);
}
}
Expand All @@ -92,9 +87,11 @@ public byte[] transform(ClassLoader loader, String className, Class<?> classBein
for (CtMethod method : declaredMethods) {
String methodName = method.getName();
if (FoxMockConstant.IBATIS_MOCK_QUERY_METHOD.equals(methodName)) {
LOG.info("mock mybatis query method");
method.insertBefore("Object data = com.cxytiandi.foxmock.agent.transformer.MethodInvokeFilter.filterAndConvertDataByMybatisQuery($args);if(java.util.Objects.nonNull(data)){return ($r)data;}");
}
if (FoxMockConstant.IBATIS_MOCK_UPDATE_METHOD.equals(methodName)) {
LOG.info("mock mybatis update method");
method.insertBefore("Object data = com.cxytiandi.foxmock.agent.transformer.MethodInvokeFilter.filterAndConvertDataByMybatisUpdate($args);if(java.util.Objects.nonNull(data)){return ($r)data;}");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cxytiandi.foxmock.agent.utils;

import com.google.gson.Gson;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

/**
Expand Down

This file was deleted.

0 comments on commit 7c4de93

Please sign in to comment.