-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from FuriousPws002/08-plugin
插件支持
- Loading branch information
Showing
12 changed files
with
287 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/19 | ||
*/ | ||
public interface Interceptor { | ||
|
||
String pointcut(); | ||
|
||
Object intercept(Invocation invocation) throws Throwable; | ||
|
||
default Object plugin(Object target, Configuration configuration) { | ||
return Plugin.wrap(target, configuration, this); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/apache/ibatis/plugin/InterceptorChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/19 | ||
*/ | ||
public class InterceptorChain { | ||
|
||
private final Configuration configuration; | ||
private final List<Interceptor> interceptors = new ArrayList<>(); | ||
|
||
public InterceptorChain(Configuration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
public <T> T pluginAll(Object target) { | ||
for (Interceptor interceptor : interceptors) { | ||
target = interceptor.plugin(target, configuration); | ||
} | ||
return (T) target; | ||
} | ||
|
||
public void addInterceptor(Interceptor interceptor) { | ||
interceptors.add(interceptor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* @author furious 2024/4/18 | ||
*/ | ||
public class Invocation { | ||
|
||
private final Object target; | ||
private final Method method; | ||
private final Object[] args; | ||
|
||
public Invocation(Object target, Method method, Object[] args) { | ||
this.target = target; | ||
this.method = method; | ||
this.args = args; | ||
} | ||
|
||
public <T> T getTarget() { | ||
return (T)target; | ||
} | ||
|
||
public Method getMethod() { | ||
return method; | ||
} | ||
|
||
public Object[] getArgs() { | ||
return args; | ||
} | ||
|
||
public <T> T proceed() throws Exception { | ||
return (T)method.invoke(target, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Objects; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/19 | ||
*/ | ||
public class Plugin implements InvocationHandler { | ||
|
||
private final Object target; | ||
private final Configuration configuration; | ||
private final Interceptor interceptor; | ||
|
||
public Plugin(Object target, Configuration configuration, Interceptor interceptor) { | ||
this.target = target; | ||
this.configuration = configuration; | ||
this.interceptor = interceptor; | ||
} | ||
|
||
public static Object wrap(Object target, Configuration configuration, Interceptor interceptor) { | ||
String pointcut = interceptor.pointcut(); | ||
PointcutDefinition definition = configuration.getPointcutRegistry().getDefinition(pointcut); | ||
//不是当前接口类型不代理 | ||
if (!definition.getType().isAssignableFrom(target.getClass())) { | ||
return target; | ||
} | ||
return Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class<?>[]{definition.getType()}, new Plugin(target, configuration, interceptor)); | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
PointcutDefinition definition = configuration.getPointcutRegistry().getDefinition(interceptor.pointcut()); | ||
if (!Objects.equals(definition.getId(), PointcutDefinition.id(method))) { | ||
//没有代理该方法,直接返回 | ||
return method.invoke(target, args); | ||
} | ||
return interceptor.intercept(new Invocation(target, method, args)); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/apache/ibatis/plugin/PointcutDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author furious 2024/4/18 | ||
*/ | ||
public class PointcutDefinition { | ||
|
||
private final Class<?> type; | ||
private final Method method; | ||
private final Class<?>[] args; | ||
private final String id; | ||
|
||
public PointcutDefinition(Method method, PointcutRegistry registry) { | ||
this.type = method.getDeclaringClass(); | ||
this.method = method; | ||
this.args = method.getParameterTypes(); | ||
this.id = id(method); | ||
registry.getDefinitions().put(id, this); | ||
} | ||
|
||
public static String id(Method method) { | ||
return String.join("_", method.getDeclaringClass().getSimpleName(), method.getName(), Arrays.stream(method.getParameterTypes()).map(Class::getSimpleName).collect(Collectors.joining("_"))); | ||
} | ||
|
||
public Class<?> getType() { | ||
return type; | ||
} | ||
|
||
public Method getMethod() { | ||
return method; | ||
} | ||
|
||
public Class<?>[] getArgs() { | ||
return args; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/apache/ibatis/plugin/PointcutRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.ibatis.executor.Executor; | ||
import org.apache.ibatis.executor.parameter.ParameterHandler; | ||
import org.apache.ibatis.executor.resultset.ResultSetHandler; | ||
import org.apache.ibatis.executor.statement.StatementHandler; | ||
|
||
/** | ||
* @author furious 2024/4/18 | ||
*/ | ||
public class PointcutRegistry { | ||
|
||
public static final String Executor_update_MappedStatement_Object = "Executor_update_MappedStatement_Object"; | ||
public static final String Executor_query_MappedStatement_Object = "Executor_query_MappedStatement_Object"; | ||
public static final String ParameterHandler_getParameterObject_ = "ParameterHandler_getParameterObject_"; | ||
public static final String ParameterHandler_setParameters_PreparedStatement = "ParameterHandler_setParameters_PreparedStatement"; | ||
public static final String ResultSetHandler_handleResultSets_Statement = "ResultSetHandler_handleResultSets_Statement"; | ||
public static final String StatementHandler_parameterize_Statement = "StatementHandler_parameterize_Statement"; | ||
public static final String StatementHandler_update_Statement = "StatementHandler_update_Statement"; | ||
public static final String StatementHandler_prepare_Connection_Integer = "StatementHandler_prepare_Connection_Integer"; | ||
public static final String StatementHandler_query_Statement = "StatementHandler_query_Statement"; | ||
public static final String StatementHandler_getBoundSql_ = "StatementHandler_getBoundSql_"; | ||
|
||
private final Map<String, PointcutDefinition> definitions = new LinkedHashMap<>(); | ||
|
||
public PointcutRegistry() { | ||
Arrays.stream(Executor.class.getMethods()).forEach(m -> new PointcutDefinition(m, this)); | ||
Arrays.stream(ParameterHandler.class.getMethods()).forEach(m -> new PointcutDefinition(m, this)); | ||
Arrays.stream(ResultSetHandler.class.getMethods()).forEach(m -> new PointcutDefinition(m, this)); | ||
Arrays.stream(StatementHandler.class.getMethods()).forEach(m -> new PointcutDefinition(m, this)); | ||
} | ||
|
||
public Map<String, PointcutDefinition> getDefinitions() { | ||
return definitions; | ||
} | ||
|
||
public PointcutDefinition getDefinition(String key) { | ||
return definitions.get(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/java/org/apache/ibatis/plugin/PageInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.sql.Connection; | ||
|
||
import org.apache.ibatis.executor.statement.StatementHandler; | ||
import org.apache.ibatis.mapping.BoundSql; | ||
|
||
/** | ||
* @author furious 2024/4/19 | ||
*/ | ||
public class PageInterceptor implements Interceptor { | ||
@Override | ||
public String pointcut() { | ||
return PointcutRegistry.StatementHandler_prepare_Connection_Integer; | ||
} | ||
|
||
@Override | ||
public Object intercept(Invocation invocation) throws Throwable { | ||
StatementHandler statementHandler = invocation.getTarget(); | ||
BoundSql original = statementHandler.getBoundSql(); | ||
Object[] args = invocation.getArgs(); | ||
Connection connection = (Connection) args[0]; | ||
//不判读是否为select语句,limit仅为示范参数 | ||
String sql = original.getSql(); | ||
sql += " LIMIT 1,1"; | ||
return connection.prepareStatement(sql); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters