Skip to content

Commit 0df824b

Browse files
committed
Optimize Plugin
1 parent 87baeb2 commit 0df824b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/main/java/org/apache/ibatis/plugin/Plugin.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.HashSet;
2323
import java.util.Map;
2424
import java.util.Set;
25+
import java.util.concurrent.ConcurrentHashMap;
26+
import java.util.concurrent.ConcurrentMap;
2527

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

3841
private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
3942
this.target = target;
4043
this.interceptor = interceptor;
4144
this.signatureMap = signatureMap;
45+
this.methodMap = new ConcurrentHashMap<>();
4246
}
4347

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

5458
@Override
5559
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
56-
try {
60+
boolean intercepted = MapUtil.computeIfAbsent(methodMap, method, key -> {
5761
Set<Method> methods = signatureMap.get(method.getDeclaringClass());
58-
if (methods != null && methods.contains(method)) {
62+
return methods != null && methods.contains(method);
63+
});
64+
try {
65+
if (intercepted) {
5966
return interceptor.intercept(new Invocation(target, method, args));
67+
} else {
68+
return method.invoke(target, args);
6069
}
61-
return method.invoke(target, args);
6270
} catch (Exception e) {
6371
throw ExceptionUtil.unwrapThrowable(e);
6472
}

0 commit comments

Comments
 (0)