diff --git a/src/main/java/org/apache/ibatis/plugin/Plugin.java b/src/main/java/org/apache/ibatis/plugin/Plugin.java index e4f829221ed..fde4c1d6c13 100644 --- a/src/main/java/org/apache/ibatis/plugin/Plugin.java +++ b/src/main/java/org/apache/ibatis/plugin/Plugin.java @@ -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; @@ -34,11 +36,13 @@ public class Plugin implements InvocationHandler { private final Object target; private final Interceptor interceptor; private final Map, Set> signatureMap; + private final ConcurrentMap methodMap; private Plugin(Object target, Interceptor interceptor, Map, Set> signatureMap) { this.target = target; this.interceptor = interceptor; this.signatureMap = signatureMap; + this.methodMap = new ConcurrentHashMap<>(); } public static Object wrap(Object target, Interceptor interceptor) { @@ -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 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); }