22
22
import java .util .HashSet ;
23
23
import java .util .Map ;
24
24
import java .util .Set ;
25
+ import java .util .concurrent .ConcurrentHashMap ;
26
+ import java .util .concurrent .ConcurrentMap ;
25
27
26
28
import org .apache .ibatis .reflection .ExceptionUtil ;
27
29
import org .apache .ibatis .util .MapUtil ;
@@ -34,11 +36,13 @@ public class Plugin implements InvocationHandler {
34
36
private final Object target ;
35
37
private final Interceptor interceptor ;
36
38
private final Map <Class <?>, Set <Method >> signatureMap ;
39
+ private final ConcurrentMap <Method , Boolean > methodMap ;
37
40
38
41
private Plugin (Object target , Interceptor interceptor , Map <Class <?>, Set <Method >> signatureMap ) {
39
42
this .target = target ;
40
43
this .interceptor = interceptor ;
41
44
this .signatureMap = signatureMap ;
45
+ this .methodMap = new ConcurrentHashMap <>();
42
46
}
43
47
44
48
public static Object wrap (Object target , Interceptor interceptor ) {
@@ -53,12 +57,16 @@ public static Object wrap(Object target, Interceptor interceptor) {
53
57
54
58
@ Override
55
59
public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
56
- try {
60
+ boolean intercepted = MapUtil . computeIfAbsent ( methodMap , method , key -> {
57
61
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 ) {
59
66
return interceptor .intercept (new Invocation (target , method , args ));
67
+ } else {
68
+ return method .invoke (target , args );
60
69
}
61
- return method .invoke (target , args );
62
70
} catch (Exception e ) {
63
71
throw ExceptionUtil .unwrapThrowable (e );
64
72
}
0 commit comments