Skip to content

Commit 8ccf945

Browse files
committed
插件Pointcut定义
1 parent 0261e21 commit 8ccf945

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.apache.ibatis.plugin;
2+
3+
import java.lang.reflect.Method;
4+
5+
/**
6+
* @author furious 2024/4/18
7+
*/
8+
public class Invocation {
9+
10+
private final Object target;
11+
private final Method method;
12+
private final Object[] args;
13+
14+
public Invocation(Object target, Method method, Object[] args) {
15+
this.target = target;
16+
this.method = method;
17+
this.args = args;
18+
}
19+
20+
public <T> T getTarget() {
21+
return (T)target;
22+
}
23+
24+
public Method getMethod() {
25+
return method;
26+
}
27+
28+
public Object[] getArgs() {
29+
return args;
30+
}
31+
32+
public <T> T proceed() throws Exception {
33+
return (T)method.invoke(target, args);
34+
}
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.apache.ibatis.plugin;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.Arrays;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* @author furious 2024/4/18
9+
*/
10+
public class PointcutDefinition {
11+
12+
private final Class<?> type;
13+
private final Method method;
14+
private final Class<?>[] args;
15+
private final String id;
16+
17+
public PointcutDefinition(Method method, PointcutRegistry registry) {
18+
this.type = method.getDeclaringClass();
19+
this.method = method;
20+
this.args = method.getParameterTypes();
21+
this.id = id(method);
22+
registry.getDefinitions().put(id, this);
23+
}
24+
25+
public static String id(Method method) {
26+
return String.join("_", method.getDeclaringClass().getSimpleName(), method.getName(), Arrays.stream(method.getParameterTypes()).map(Class::getSimpleName).collect(Collectors.joining("_")));
27+
}
28+
29+
public Class<?> getType() {
30+
return type;
31+
}
32+
33+
public Method getMethod() {
34+
return method;
35+
}
36+
37+
public Class<?>[] getArgs() {
38+
return args;
39+
}
40+
41+
public String getId() {
42+
return id;
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.apache.ibatis.plugin;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
import org.apache.ibatis.executor.Executor;
8+
import org.apache.ibatis.executor.parameter.ParameterHandler;
9+
import org.apache.ibatis.executor.resultset.ResultSetHandler;
10+
import org.apache.ibatis.executor.statement.StatementHandler;
11+
12+
/**
13+
* @author furious 2024/4/18
14+
*/
15+
public class PointcutRegistry {
16+
17+
public static final String Executor_update_MappedStatement_Object = "Executor_update_MappedStatement_Object";
18+
public static final String Executor_query_MappedStatement_Object = "Executor_query_MappedStatement_Object";
19+
public static final String ParameterHandler_getParameterObject_ = "ParameterHandler_getParameterObject_";
20+
public static final String ParameterHandler_setParameters_PreparedStatement = "ParameterHandler_setParameters_PreparedStatement";
21+
public static final String ResultSetHandler_handleResultSets_Statement = "ResultSetHandler_handleResultSets_Statement";
22+
public static final String StatementHandler_parameterize_Statement = "StatementHandler_parameterize_Statement";
23+
public static final String StatementHandler_update_Statement = "StatementHandler_update_Statement";
24+
public static final String StatementHandler_prepare_Connection_Integer = "StatementHandler_prepare_Connection_Integer";
25+
public static final String StatementHandler_query_Statement = "StatementHandler_query_Statement";
26+
public static final String StatementHandler_getBoundSql_ = "StatementHandler_getBoundSql_";
27+
28+
private final Map<String, PointcutDefinition> definitions = new LinkedHashMap<>();
29+
30+
public PointcutRegistry() {
31+
Arrays.stream(Executor.class.getMethods()).forEach(m -> new PointcutDefinition(m, this));
32+
Arrays.stream(ParameterHandler.class.getMethods()).forEach(m -> new PointcutDefinition(m, this));
33+
Arrays.stream(ResultSetHandler.class.getMethods()).forEach(m -> new PointcutDefinition(m, this));
34+
Arrays.stream(StatementHandler.class.getMethods()).forEach(m -> new PointcutDefinition(m, this));
35+
}
36+
37+
public Map<String, PointcutDefinition> getDefinitions() {
38+
return definitions;
39+
}
40+
41+
public PointcutDefinition getDefinition(String key) {
42+
return definitions.get(key);
43+
}
44+
}

0 commit comments

Comments
 (0)