21
21
import org .aopalliance .intercept .MethodInvocation ;
22
22
import org .springframework .aop .ProxyMethodInvocation ;
23
23
import org .springframework .aop .framework .Advised ;
24
+ import org .springframework .aop .support .AopUtils ;
24
25
import org .springframework .modulith .core .ApplicationModule ;
26
+ import org .springframework .modulith .core .ApplicationModuleIdentifier ;
25
27
import org .springframework .modulith .core .ApplicationModules ;
28
+ import org .springframework .modulith .core .ArchitecturallyEvidentType .ReferenceMethod ;
26
29
import org .springframework .modulith .core .FormattableType ;
27
30
import org .springframework .modulith .core .SpringBean ;
28
31
import org .springframework .util .Assert ;
29
32
30
33
import com .tngtech .archunit .core .domain .JavaClass ;
34
+ import com .tngtech .archunit .core .domain .JavaMethod ;
31
35
32
36
class DefaultObservedModule implements ObservedModule {
33
37
@@ -51,7 +55,16 @@ class DefaultObservedModule implements ObservedModule {
51
55
*/
52
56
@ Override
53
57
public String getName () {
54
- return module .getName ();
58
+ return getIdentifier ().toString ();
59
+ }
60
+
61
+ /*
62
+ * (non-Javadoc)
63
+ * @see org.springframework.modulith.observability.ObservedModule#getIdentifier()
64
+ */
65
+ @ Override
66
+ public ApplicationModuleIdentifier getIdentifier () {
67
+ return module .getIdentifier ();
55
68
}
56
69
57
70
/*
@@ -69,37 +82,7 @@ public String getDisplayName() {
69
82
*/
70
83
@ Override
71
84
public String getInvokedMethod (MethodInvocation invocation ) {
72
-
73
- Method method = invocation .getMethod ();
74
-
75
- if (module .contains (method .getDeclaringClass ())) {
76
- return toString (invocation .getMethod (), module );
77
- }
78
-
79
- if (!ProxyMethodInvocation .class .isInstance (invocation )) {
80
- return toString (invocation .getMethod (), module );
81
- }
82
-
83
- // For class-based proxies, use the target class
84
-
85
- var advised = (Advised ) ((ProxyMethodInvocation ) invocation ).getProxy ();
86
- var targetClass = advised .getTargetClass ();
87
-
88
- if (module .contains (targetClass )) {
89
- return toString (targetClass , method , module );
90
- }
91
-
92
- // For JDK proxies, find original interface the method was logically declared on
93
-
94
- for (Class <?> type : advised .getProxiedInterfaces ()) {
95
- if (module .contains (type )) {
96
- if (Arrays .asList (type .getMethods ()).contains (method )) {
97
- return toString (type , method , module );
98
- }
99
- }
100
- }
101
-
102
- return toString (invocation .getMethod (), module );
85
+ return toString (findModuleLocalMethod (invocation ), module );
103
86
}
104
87
105
88
/*
@@ -144,11 +127,61 @@ public ObservedModuleType getObservedModuleType(Class<?> type, ApplicationModule
144
127
.orElse (null );
145
128
}
146
129
147
- private static String toString (Method method , ApplicationModule module ) {
148
- return toString (method .getDeclaringClass (), method , module );
130
+ /*
131
+ * (non-Javadoc)
132
+ * @see org.springframework.modulith.observability.ObservedModule#isEventListenerInvocation(org.aopalliance.intercept.MethodInvocation)
133
+ */
134
+ @ Override
135
+ public boolean isEventListenerInvocation (MethodInvocation invocation ) {
136
+
137
+ var method = findModuleLocalMethod (invocation );
138
+ var type = module .getArchitecturallyEvidentType (method .getDeclaringClass ());
139
+
140
+ return type .isEventListener ()
141
+ && type .getReferenceMethods ()
142
+ .map (ReferenceMethod ::getMethod )
143
+ .map (JavaMethod ::reflect )
144
+ .anyMatch (method ::equals );
149
145
}
150
146
151
- private static String toString (Class <?> type , Method method , ApplicationModule module ) {
147
+ private Method findModuleLocalMethod (MethodInvocation invocation ) {
148
+
149
+ Method method = invocation .getMethod ();
150
+
151
+ if (module .contains (method .getDeclaringClass ())) {
152
+ return invocation .getMethod ();
153
+ }
154
+
155
+ if (!ProxyMethodInvocation .class .isInstance (invocation )) {
156
+ return invocation .getMethod ();
157
+ }
158
+
159
+ // For class-based proxies, use the target class
160
+
161
+ var advised = (Advised ) ((ProxyMethodInvocation ) invocation ).getProxy ();
162
+ var targetClass = advised .getTargetClass ();
163
+
164
+ if (module .contains (targetClass )) {
165
+
166
+ return AopUtils .getMostSpecificMethod (method , targetClass );
167
+ }
168
+
169
+ // For JDK proxies, find original interface the method was logically declared on
170
+
171
+ for (Class <?> type : advised .getProxiedInterfaces ()) {
172
+ if (module .contains (type )) {
173
+ if (Arrays .asList (type .getMethods ()).contains (method )) {
174
+ return AopUtils .getMostSpecificMethod (method , targetClass );
175
+ }
176
+ }
177
+ }
178
+
179
+ return invocation .getMethod ();
180
+ }
181
+
182
+ private static String toString (Method method , ApplicationModule module ) {
183
+
184
+ var type = method .getDeclaringClass ();
152
185
153
186
var typeName = module .getType (type .getName ())
154
187
.map (FormattableType ::of )
0 commit comments