Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Plugin #3166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/java/org/apache/ibatis/plugin/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.ibatis.reflection.ExceptionUtil;
import org.apache.ibatis.util.MapUtil;
Expand All @@ -34,11 +36,13 @@ public class Plugin implements InvocationHandler {
private final Object target;
private final Interceptor interceptor;
private final Map<Class<?>, Set<Method>> signatureMap;
private final ConcurrentMap<Method, Boolean> methodMap;

private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
this.methodMap = new ConcurrentHashMap<>();
}

public static Object wrap(Object target, Interceptor interceptor) {
Expand All @@ -53,12 +57,16 @@ public static Object wrap(Object target, Interceptor interceptor) {

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
boolean intercepted = MapUtil.computeIfAbsent(methodMap, method, key -> {
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
if (methods != null && methods.contains(method)) {
return methods != null && methods.contains(method);
});
try {
if (intercepted) {
return interceptor.intercept(new Invocation(target, method, args));
} else {
return method.invoke(target, args);
}
return method.invoke(target, args);
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
Expand Down
Loading