2222import java .util .HashSet ;
2323import java .util .Map ;
2424import java .util .Set ;
25+ import java .util .concurrent .ConcurrentHashMap ;
26+ import java .util .concurrent .ConcurrentMap ;
2527
2628import org .apache .ibatis .reflection .ExceptionUtil ;
2729import 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