@@ -54,8 +54,11 @@ point.
54
54
55
55
The following example shows a simple `MethodInterceptor` implementation:
56
56
57
+ [tabs]
58
+ ======
59
+ Java::
60
+ +
57
61
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
58
- .Java
59
62
----
60
63
public class DebugInterceptor implements MethodInterceptor {
61
64
@@ -67,8 +70,10 @@ The following example shows a simple `MethodInterceptor` implementation:
67
70
}
68
71
}
69
72
----
73
+
74
+ Kotlin::
75
+ +
70
76
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
71
- .Kotlin
72
77
----
73
78
class DebugInterceptor : MethodInterceptor {
74
79
@@ -80,6 +85,7 @@ The following example shows a simple `MethodInterceptor` implementation:
80
85
}
81
86
}
82
87
----
88
+ ======
83
89
84
90
Note the call to the `proceed()` method of `MethodInvocation`. This proceeds down the
85
91
interceptor chain towards the join point. Most interceptors invoke this method and
@@ -129,8 +135,11 @@ wrapped in an unchecked exception by the AOP proxy.
129
135
130
136
The following example shows a before advice in Spring, which counts all method invocations:
131
137
138
+ [tabs]
139
+ ======
140
+ Java::
141
+ +
132
142
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
133
- .Java
134
143
----
135
144
public class CountingBeforeAdvice implements MethodBeforeAdvice {
136
145
@@ -145,8 +154,10 @@ The following example shows a before advice in Spring, which counts all method i
145
154
}
146
155
}
147
156
----
157
+
158
+ Kotlin::
159
+ +
148
160
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
149
- .Kotlin
150
161
----
151
162
class CountingBeforeAdvice : MethodBeforeAdvice {
152
163
@@ -157,6 +168,7 @@ The following example shows a before advice in Spring, which counts all method i
157
168
}
158
169
}
159
170
----
171
+ ======
160
172
161
173
TIP: Before advice can be used with any pointcut.
162
174
@@ -181,8 +193,11 @@ arguments. The next two listing show classes that are examples of throws advice.
181
193
182
194
The following advice is invoked if a `RemoteException` is thrown (including from subclasses):
183
195
196
+ [tabs]
197
+ ======
198
+ Java::
199
+ +
184
200
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
185
- .Java
186
201
----
187
202
public class RemoteThrowsAdvice implements ThrowsAdvice {
188
203
@@ -191,8 +206,10 @@ The following advice is invoked if a `RemoteException` is thrown (including from
191
206
}
192
207
}
193
208
----
209
+
210
+ Kotlin::
211
+ +
194
212
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
195
- .Kotlin
196
213
----
197
214
class RemoteThrowsAdvice : ThrowsAdvice {
198
215
@@ -201,13 +218,17 @@ The following advice is invoked if a `RemoteException` is thrown (including from
201
218
}
202
219
}
203
220
----
221
+ ======
204
222
205
223
Unlike the preceding
206
224
advice, the next example declares four arguments, so that it has access to the invoked method, method
207
225
arguments, and target object. The following advice is invoked if a `ServletException` is thrown:
208
226
227
+ [tabs]
228
+ ======
229
+ Java::
230
+ +
209
231
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
210
- .Java
211
232
----
212
233
public class ServletThrowsAdviceWithArguments implements ThrowsAdvice {
213
234
@@ -216,8 +237,10 @@ arguments, and target object. The following advice is invoked if a `ServletExcep
216
237
}
217
238
}
218
239
----
240
+
241
+ Kotlin::
242
+ +
219
243
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
220
- .Kotlin
221
244
----
222
245
class ServletThrowsAdviceWithArguments : ThrowsAdvice {
223
246
@@ -226,13 +249,17 @@ arguments, and target object. The following advice is invoked if a `ServletExcep
226
249
}
227
250
}
228
251
----
252
+ ======
229
253
230
254
The final example illustrates how these two methods could be used in a single class
231
255
that handles both `RemoteException` and `ServletException`. Any number of throws advice
232
256
methods can be combined in a single class. The following listing shows the final example:
233
257
258
+ [tabs]
259
+ ======
260
+ Java::
261
+ +
234
262
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
235
- .Java
236
263
----
237
264
public static class CombinedThrowsAdvice implements ThrowsAdvice {
238
265
@@ -245,8 +272,10 @@ methods can be combined in a single class. The following listing shows the final
245
272
}
246
273
}
247
274
----
275
+
276
+ Kotlin::
277
+ +
248
278
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
249
- .Kotlin
250
279
----
251
280
class CombinedThrowsAdvice : ThrowsAdvice {
252
281
@@ -259,6 +288,7 @@ methods can be combined in a single class. The following listing shows the final
259
288
}
260
289
}
261
290
----
291
+ ======
262
292
263
293
NOTE: If a throws-advice method throws an exception itself, it overrides the
264
294
original exception (that is, it changes the exception thrown to the user). The overriding
@@ -292,8 +322,11 @@ the invoked method, the method's arguments, and the target.
292
322
The following after returning advice counts all successful method invocations that have
293
323
not thrown exceptions:
294
324
325
+ [tabs]
326
+ ======
327
+ Java::
328
+ +
295
329
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
296
- .Java
297
330
----
298
331
public class CountingAfterReturningAdvice implements AfterReturningAdvice {
299
332
@@ -309,8 +342,10 @@ not thrown exceptions:
309
342
}
310
343
}
311
344
----
345
+
346
+ Kotlin::
347
+ +
312
348
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
313
- .Kotlin
314
349
----
315
350
class CountingAfterReturningAdvice : AfterReturningAdvice {
316
351
@@ -322,6 +357,7 @@ not thrown exceptions:
322
357
}
323
358
}
324
359
----
360
+ ======
325
361
326
362
This advice does not change the execution path. If it throws an exception, it is
327
363
thrown up the interceptor chain instead of the return value.
@@ -380,24 +416,30 @@ introduced interfaces can be implemented by the configured `IntroductionIntercep
380
416
Consider an example from the Spring test suite and suppose we want to
381
417
introduce the following interface to one or more objects:
382
418
419
+ [tabs]
420
+ ======
421
+ Java::
422
+ +
383
423
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
384
- .Java
385
424
----
386
425
public interface Lockable {
387
426
void lock();
388
427
void unlock();
389
428
boolean locked();
390
429
}
391
430
----
431
+
432
+ Kotlin::
433
+ +
392
434
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
393
- .Kotlin
394
435
----
395
436
interface Lockable {
396
437
fun lock()
397
438
fun unlock()
398
439
fun locked(): Boolean
399
440
}
400
441
----
442
+ ======
401
443
402
444
This illustrates a mixin. We want to be able to cast advised objects to `Lockable`,
403
445
whatever their type and call lock and unlock methods. If we call the `lock()` method, we
@@ -434,8 +476,11 @@ to that held in the target object.
434
476
435
477
The following example shows the example `LockMixin` class:
436
478
479
+ [tabs]
480
+ ======
481
+ Java::
482
+ +
437
483
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
438
- .Java
439
484
----
440
485
public class LockMixin extends DelegatingIntroductionInterceptor implements Lockable {
441
486
@@ -462,8 +507,10 @@ The following example shows the example `LockMixin` class:
462
507
463
508
}
464
509
----
510
+
511
+ Kotlin::
512
+ +
465
513
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
466
- .Kotlin
467
514
----
468
515
class LockMixin : DelegatingIntroductionInterceptor(), Lockable {
469
516
@@ -490,6 +537,7 @@ The following example shows the example `LockMixin` class:
490
537
491
538
}
492
539
----
540
+ ======
493
541
494
542
Often, you need not override the `invoke()` method. The
495
543
`DelegatingIntroductionInterceptor` implementation (which calls the `delegate` method if
@@ -504,8 +552,11 @@ interceptor (which would be defined as a prototype). In this case, there is no
504
552
configuration relevant for a `LockMixin`, so we create it by using `new`.
505
553
The following example shows our `LockMixinAdvisor` class:
506
554
555
+ [tabs]
556
+ ======
557
+ Java::
558
+ +
507
559
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
508
- .Java
509
560
----
510
561
public class LockMixinAdvisor extends DefaultIntroductionAdvisor {
511
562
@@ -514,11 +565,14 @@ The following example shows our `LockMixinAdvisor` class:
514
565
}
515
566
}
516
567
----
568
+
569
+ Kotlin::
570
+ +
517
571
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
518
- .Kotlin
519
572
----
520
573
class LockMixinAdvisor : DefaultIntroductionAdvisor(LockMixin(), Lockable::class.java)
521
574
----
575
+ ======
522
576
523
577
We can apply this advisor very simply, because it requires no configuration. (However, it
524
578
is impossible to use an `IntroductionInterceptor` without an
0 commit comments