18
18
19
19
package org .apache .skywalking .apm .plugin .spring .patch ;
20
20
21
- import java .lang .reflect .Method ;
22
21
import org .apache .skywalking .apm .agent .core .plugin .interceptor .enhance .EnhancedInstance ;
23
22
import org .apache .skywalking .apm .agent .core .plugin .interceptor .enhance .InstanceMethodsAroundInterceptor ;
24
23
import org .apache .skywalking .apm .agent .core .plugin .interceptor .enhance .MethodInterceptResult ;
24
+ import org .springframework .aop .SpringProxy ;
25
25
import org .springframework .aop .framework .AdvisedSupport ;
26
26
27
+ import java .lang .reflect .Method ;
28
+
27
29
/**
28
30
* <code>CreateAopProxyInterceptor</code> check that the bean has been implement {@link EnhancedInstance}.
29
31
* if yes, true will be returned.
@@ -32,25 +34,45 @@ public class CreateAopProxyInterceptor implements InstanceMethodsAroundIntercept
32
34
33
35
@ Override
34
36
public void beforeMethod (EnhancedInstance objInst , Method method , Object [] allArguments , Class <?>[] argumentsTypes ,
35
- MethodInterceptResult result ) throws Throwable {
37
+ MethodInterceptResult result ) throws Throwable {
36
38
37
39
}
38
40
39
41
@ Override
40
42
public Object afterMethod (EnhancedInstance objInst , Method method , Object [] allArguments , Class <?>[] argumentsTypes ,
41
- Object ret ) throws Throwable {
43
+ Object ret ) throws Throwable {
42
44
AdvisedSupport advisedSupport = (AdvisedSupport ) allArguments [0 ];
43
45
44
- Class targetClass = advisedSupport .getTargetClass ();
45
- if (targetClass != null && EnhancedInstance .class .isAssignableFrom (targetClass )) {
46
- return true ;
46
+ if (maybeHasUserSuppliedProxyInterfaces (ret )) {
47
+ Class targetClass = advisedSupport .getTargetClass ();
48
+ if (targetClass != null ) {
49
+ if (onlyImplementsEnhancedInstance (advisedSupport ) || onlyImplementsEnhancedInstanceAndSpringProxy (advisedSupport )) {
50
+ return true ;
51
+ }
52
+ }
47
53
}
48
54
return ret ;
49
55
}
50
56
57
+ private boolean maybeHasUserSuppliedProxyInterfaces (Object ret ) {
58
+ return !(Boolean ) ret ;
59
+ }
60
+
61
+ private boolean onlyImplementsEnhancedInstanceAndSpringProxy (AdvisedSupport advisedSupport ) {
62
+ Class <?>[] ifcs = advisedSupport .getProxiedInterfaces ();
63
+ Class targetClass = advisedSupport .getTargetClass ();
64
+ return ifcs .length == 2 && EnhancedInstance .class .isAssignableFrom (targetClass ) && SpringProxy .class .isAssignableFrom (targetClass );
65
+ }
66
+
67
+ private boolean onlyImplementsEnhancedInstance (AdvisedSupport advisedSupport ) {
68
+ Class <?>[] ifcs = advisedSupport .getProxiedInterfaces ();
69
+ Class targetClass = advisedSupport .getTargetClass ();
70
+ return ifcs .length == 1 && EnhancedInstance .class .isAssignableFrom (targetClass );
71
+ }
72
+
51
73
@ Override
52
74
public void handleMethodException (EnhancedInstance objInst , Method method , Object [] allArguments ,
53
- Class <?>[] argumentsTypes , Throwable t ) {
75
+ Class <?>[] argumentsTypes , Throwable t ) {
54
76
55
77
}
56
78
}
0 commit comments