From 0df824b3760693ca91c56fefad3c75b5a6ec14ee Mon Sep 17 00:00:00 2001
From: kanghailin <1227734668@qq.com>
Date: Wed, 22 May 2024 17:40:42 +0800
Subject: [PATCH] Optimize Plugin

---
 src/main/java/org/apache/ibatis/plugin/Plugin.java | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

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<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) {
@@ -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);
     }