1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
|
<appendix id="semantics" xreflabel="Semantics">
<title>Language Semantics</title>
<para>
AspectJ extends Java by overlaying a concept of join points onto the
existing Java semantics and by adding adds four kinds of program elements
to Java:
</para>
<para>
Join points are well-defined points in the execution of a program. These
include method and constructor calls, field accesses and others described
below.
</para>
<para>
A pointcut picks out join points, and exposes some of the values in the
execution context of those join points. There are several primitive
pointcut designators, new named pointcuts can be defined by the
<literal>pointcut</literal> declaration.
</para>
<para>
Advice is code that executes at each join point in a pointcut. Advice has
access to the values exposed by the pointcut. Advice is defined by
<literal>before</literal>, <literal>after</literal>, and
<literal>around</literal> declarations.
</para>
<para>
Introduction and declaration form AspectJ's static crosscutting features,
that is, is code that may change the type structure of a program, by adding
to or extending interfaces and classes with new fields, constructors, or
methods. Introductions are defined through an extension of usual method,
field, and constructor declarations, and other declarations are made with a
new <literal>declare</literal> keyword.
</para>
<para>
An aspect is a crosscutting type, that encapsulates pointcuts, advice, and
static crosscutting features. By type, we mean Java's notion: a modular
unit of code, with a well-defined interface, about which it is possible to
do reasoning at compile time. Aspects are defined by the
<literal>aspect</literal> declaration.
</para>
<sect1>
<title>Join Points</title>
<para>
While aspects do define crosscutting types, the AspectJ system does not
allow completely arbitrary crosscutting. Rather, aspects define types that
cut across principled points in a program's execution. These principled
points are called join points.
</para>
<para>
A join point is a well-defined point in the execution of a program. The
join points defined by AspectJ are:
</para>
<glosslist>
<glossentry>
<glossterm>Method call</glossterm>
<glossdef>
<para>
When a method is called, not including super calls of non-static
methods.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Method execution</glossterm>
<glossdef>
<para>
When the body of code for an actual method executes.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Constructor call</glossterm>
<glossdef>
<para>
When an object is built and a constructor is called, not including
this or super constructor calls. The object being constructed is
returned at a constructor call join point, so it may be accessed
with <literal>after returning</literal> advice.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Initializer execution</glossterm>
<glossdef>
<para>
When the non-static initializers of a class run.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Constructor execution</glossterm>
<glossdef>
<para>
When the body of code for an actual constructor executes, after its
this or super constructor call. The object being constructed is
the currently executing object, and so may be accessed with the
<literal>this</literal> pointcut. No value is returned from
constructor execution join points.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Static initializer execution</glossterm>
<glossdef>
<para>
When the static initializer for a class executes.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Object pre-initialization</glossterm>
<glossdef>
<para>
Before the object initialization code for a particular class runs.
This encompasses the time between the start of its first called
constructor and the start of its parent's constructor. Thus, the
execution of these join points encompass the join points from the
code found in <literal>this()</literal> and
<literal>super()</literal> constructor calls.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Object initialization</glossterm>
<glossdef>
<para>
When the object initialization code for a particular class runs.
This encompasses the time between the return of its parent's
constructor and the return of its first called constructor. It
includes all the dynamic initializers and constructors used to
create the object. The object being constructed is
the currently executing object, and so may be accessed with the
<literal>this</literal> pointcut. No value is returned from
constructor execution join points.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Field reference</glossterm>
<glossdef>
<para>
When a non-final field is referenced.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Field assignment</glossterm>
<glossdef>
<para>
When a field is assigned to.
</para>
</glossdef>
</glossentry>
<glossentry>
<glossterm>Handler execution</glossterm>
<glossdef>
<para>
When an exception handler executes.
</para>
</glossdef>
</glossentry>
</glosslist>
</sect1>
<sect1>
<title>Pointcuts</title>
<para>
A pointcut is a program element that picks out join points, as well as
data from the execution context of the join points. Pointcuts are used
primarily by advice. They can be composed with boolean operators to
build up other pointcuts. So a pointcut is defined by one of
</para>
<variablelist>
<varlistentry>
<term><literal>call(<replaceable>Signature</replaceable>)</literal></term>
<listitem>
<para>Picks out a method or constructor call join point based on the
static signature at the caller side. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>execution(<replaceable>Signature</replaceable>)</literal></term>
<listitem>
<para>Picks out a method or constructor execution join point based on
the static signature at the callee side. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>get(<replaceable>Signature</replaceable>)</literal></term>
<listitem>
<para>Picks out a field get join point based on the static
signature. Note that references to constant fields (static final
fields bound to a constant string object or primitive value) are not
get join points, since Java requires them to be inlined. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>set(<replaceable>Signature</replaceable>)</literal></term>
<listitem>
<para>Picks out a field set join point based on the static
signature. Note that the initializations of constant fields (static
final fields where the initializer is a constant string object or
primitive value) are not set join points, since Java requires their
references to be inlined.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>handler(<replaceable>TypePattern</replaceable>)</literal></term>
<listitem>
<para>Picks out an exception handler of any of the Throwable types
of the type pattern. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>initialization(<replaceable>Signature</replaceable>)</literal></term>
<listitem>
<para>Picks out an object initialization join point based on the
static signature of the starting constructor. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>staticinitialization(<replaceable>TypePattern</replaceable>)</literal></term>
<listitem>
<para>Picks out a static initializer execution join point of any of the types
of the type pattern. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>within(<replaceable>TypePattern</replaceable>)</literal></term>
<listitem>
<para>Picks out all join points where the executing code is defined
in any of the classes of the type pattern. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>withincode(<replaceable>Signature</replaceable>)</literal></term>
<listitem>
<para>Picks out all join points where the executing code is defined
in the method or constructor of the appropriate signature. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cflow(<replaceable>Pointcut</replaceable>)</literal></term>
<listitem>
<para>Picks out all join points in the control flow of the join
points picked out by the pointcut, including pointcut's join points
themselves. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>cflowbelow(<replaceable>Pointcut</replaceable>)</literal></term>
<listitem>
<para>Picks out all join points in the control flow below the join
points picked out by the pointcut. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>this(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable>)</literal></term>
<listitem>
<para>Picks out all join points where the currently executing object
(the object bound to <literal>this</literal>) is an instance of a
type of the type pattern, or of the type of the identifier.
Will not match any join points from static methods.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>target(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable>)</literal></term>
<listitem>
<para>Picks out all join points where the target object (the object
on which a call or field operation is applied to) is an instance of a
type of the type pattern, or of the type of the
identifier. Will not match any calls, gets, or sets to static
members. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>args(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable>, ...)</literal></term>
<listitem>
<para>Picks out all join points where the arguments are instances of
a type of the appropriate type pattern or identifier. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal><replaceable>PointcutId</replaceable>(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable>, ...)</literal></term>
<listitem>
<para>Picks out all join points that are picked out by the
user-defined pointcut designator named by
<replaceable>PointcutId</replaceable>. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>if(<replaceable>BooleanExpression</replaceable>)</literal></term>
<listitem>
<para>Picks out all join points where the boolean expression
evaluates to <literal>true</literal>. The boolean expression used
can only access static members, variables exposed by teh enclosing
pointcut or advice, and <literal>thisJoinPoint</literal> forms. In
particular, it cannot call non-static methods on the aspect. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>! <replaceable>Pointcut</replaceable></literal></term>
<listitem>
<para>Picks out all join points that are not picked out by the
pointcut. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal><replaceable>Pointcut0</replaceable> <![CDATA[&&]]> <replaceable>Pointcut1</replaceable></literal></term>
<listitem>
<para>Picks out all join points that are picked out by both of the
pointcuts. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal><replaceable>Pointcut0</replaceable> || <replaceable>Pointcut1</replaceable></literal></term>
<listitem>
<para>Picks out all join points that are picked out by either of the
pointcuts. </para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>( <replaceable>Pointcut</replaceable> )</literal></term>
<listitem>
<para>Picks out all join points that are picked out by the
parenthesized pointcut. </para>
</listitem>
</varlistentry>
</variablelist>
<sect2>
<title>Pointcut naming
</title>
<para>
A named pointcut is defined with the <literal>pointcut</literal>
declaration.
</para>
<programlisting>
pointcut publicIntCall(int i):
call(public * *(int)) <![CDATA[&&]]> args(i);
</programlisting>
<para>
A named pointcut may be defined in either a class or aspect, and is
treated as a member of the class or aspect where it is found. As a
member, it may have an access modifier such as
<literal>public</literal> or <literal>private</literal>.
</para>
<programlisting>
class C {
pointcut publicCall(int i):
call(public * *(int)) <![CDATA[&&]]> args(i);
}
class D {
pointcut myPublicCall(int i):
C.publicCall(i) <![CDATA[&&]]> within(SomeType);
}
</programlisting>
<para>
Pointcuts that are not final may be declared abstract, and defined
without a body. Abstract pointcuts may only be declared within
abstract aspects.
</para>
<programlisting>
abstract aspect A {
abstract pointcut publicCall(int i);
}
</programlisting>
<para>
In such a case, an extending aspect may override the abstract
pointcut.
</para>
<programlisting>
aspect B extends A {
pointcut publicCall(int i): call(public Foo.m(int)) <![CDATA[&&]]> args(i);
}
</programlisting>
<para>For completeness, a pointcut with a declaration may be declared
<literal>final</literal>. </para>
<para>
Though named pointcut declarations appear somewhat like method
declarations, and can be overridden in subaspects, they cannot be
overloaded. It is an error for two pointcuts to be named with the same
name in the same class or aspect declaration.
</para>
<para>
The scope of a named pointcut is the enclosing class declaration. This
is different than the scope of other members; the scope of other
members is the enclosing class <emphasis>body</emphasis>. This means
that the following code is legal:
</para>
<programlisting>
aspect B percflow(publicCall()) {
pointcut publicCall(): call(public Foo.m(int));
}
</programlisting>
</sect2>
<sect2>
<title>Context exposure</title>
<para>
Pointcuts have an interface; they expose some parts of the execution
context of the join points they pick out. For example, the PublicIntCall
above exposes the first argument from the receptions of all public
unary integer methods. This context is exposed by providing typed
formal parameters to named pointcuts and advice, like the formal
parameters of a Java method. These formal parameters are bound by name
matching.
</para>
<para>
On the right-hand side of advice or pointcut declarations, a regular
Java identifier is allowed in certain pointcut designators in place of
a type or collection of types.
There are four primitive pointcut designators where this is allowed:
<literal>this</literal>, <literal>target</literal>, and
<literal>args</literal><!-- and hasaspect -->. In all such
cases, using an identifier rather than a type is as if the type
selected was the type of the formal parameter, so that the pointcut
</para>
<programlisting>
pointcut intArg(int i): args(i);
</programlisting>
<para>
picks out join points where an <literal>int</literal> is being passed
as an argument, but furthermore allows advice access to that argument.
</para>
<para>
Values can be exposed from named pointcuts as well, so
</para>
<programlisting>
pointcut publicCall(int x): call(public *.*(int)) <![CDATA[&&]]> intArg(x);
pointcut intArg(int i): args(i);
</programlisting>
<para>
is a legal way to pick out all calls to public methods accepting an int
argument, and exposing that argument.
</para>
<para>
There is one special case for this kind of exposure. Exposing an
argument of type Object will also match primitive typed arguments, and
expose a "boxed" version of the primitive. So,
</para>
<programlisting>
pointcut publicCall(): call(public *.*(..)) <![CDATA[&&]]> args(Object);
</programlisting>
<para>
will pick out all unary methods that take, as their only argument,
subtypes of Object (i.e., not primitive types like
<literal>int</literal>), but
</para>
<programlisting>
pointcut publicCall(Object o): call(public *.*(..)) <![CDATA[&&]]> args(o);
</programlisting>
<para>
will pick out all unary methods that take any argument: And if the
argument was an <literal>int</literal>, then the value passed to advice
will be of type <literal>java.lang.Integer</literal>.
</para>
</sect2>
<sect2>
<title>Primitive pointcuts</title>
<bridgehead>Method-related pointcuts</bridgehead>
<para>AspectJ provides two primitive pointcut designators designed to
capture method call and execution join points. </para>
<simplelist>
<member><literal>call(<replaceable>Signature</replaceable>)</literal></member>
<member><literal>execution(<replaceable>Signature</replaceable>)</literal></member>
</simplelist>
<para>These two pointcuts also pick out constructor call end execution
join points. </para>
<bridgehead>Field-related pointcuts</bridgehead>
<para>
AspectJ provides two primitive pointcut designators designed to
capture field reference and assignment join points:
</para>
<simplelist>
<member><literal>get(<replaceable>Signature</replaceable>)</literal></member>
<member><literal>set(<replaceable>Signature</replaceable>)</literal></member>
</simplelist>
<para>
All set join points are treated as having one argument, the value the
field is being set to, so at a set join point, that value can be
accessed with an <literal>args</literal> pointcut. So an aspect
guarding an integer variable x declared in type T might be written as
</para>
<programlisting><![CDATA[
aspect GuardedX {
static final int MAX_CHANGE = 100;
before(int newval): set(int T.x) && args(newval) {
if (Math.abs(newval - T.x) > MAX_CHANGE)
throw new RuntimeException();
}
}]]></programlisting>
<bridgehead>Object creation-related pointcuts</bridgehead>
<para>
AspectJ provides three primitive pointcut designators designed to
capture the initializer execution join points of objects.
</para>
<simplelist>
<member><literal>call(<replaceable>Signature</replaceable>)</literal></member>
<member><literal>initialization(<replaceable>Signature</replaceable>)</literal></member>
<member><literal>execution(<replaceable>Signature</replaceable>)</literal></member>
</simplelist>
<bridgehead>Class initialization-related pointcuts</bridgehead>
<para>
AspectJ provides one primitive pointcut designator to pick out
static initializer execution join points.
</para>
<simplelist>
<member><literal>staticinitialization(<replaceable>TypePattern</replaceable>)</literal></member>
</simplelist>
<bridgehead>Exception handler execution-related pointcuts</bridgehead>
<para>
AspectJ provides one primitive pointcut designator to capture
execution of exception handlers:
</para>
<simplelist>
<member><literal>handler(<replaceable>TypePattern</replaceable>)</literal></member>
</simplelist>
<para>
All handler join points are treated as having one argument, the value
of the exception being handled, so at a handler join point, that
value can be accessed with an <literal>args</literal> pointcut. So
an aspect used to put FooException objects into some normal form
before they are handled could be written as
</para>
<programlisting>
aspect NormalizeFooException {
before(FooException e): handler(FooException) <![CDATA[&&]]> args(e) {
e.normalize();
}
}
</programlisting>
<bridgehead>State-based pointcuts</bridgehead>
<para>
Many concerns cut across the dynamic times when an object of a
particular type is executing, being operated on, or being passed
around. AspectJ provides primitive pointcuts that capture join
points at these times. These pointcuts use the dynamic types of
their objects to discriminate, or pick out, join points. They may
also be used to expose to advice the objects used for
discrimination.
</para>
<simplelist>
<member><literal>this(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable>)</literal></member>
<member><literal>target(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable>)</literal></member>
<!-- <member><literal>hasaspect(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable>)</literal></member> -->
</simplelist>
<para>
The this pointcut picks out all join points where the currently
executing object (the object bound to <literal>this</literal>) is an
instance of a particular type. The target pointcut picks out all
join points where the target object (the object on which a method is
called or a field is accessed) is an instance of a particular type.
<!-- The hasaspect pointcut picks out all join points where there is an
associated aspect instance of a particular type. -->
</para>
<simplelist>
<member><literal>args(<replaceable>TypePattern</replaceable> or <replaceable>Id</replaceable> or "..", ...)</literal></member>
</simplelist>
<para>
The args pointcut picks out all join points where the arguments are
instances of some types. Each element in the comma-separated list is
one of three things. If it is a type pattern, then the argument
in that position must be an instance of a type of the type name. If
it is an identifier, then the argument in that position must be an
instance of the type of the identifier (or of any type if the
identifier is typed to Object). If it is the special wildcard "..",
then any number of arguments will match, just like in signatures. So
the pointcut
</para>
<programlisting>
args(int, .., String)
</programlisting>
<para>
will pick out all join points where the first argument is an
<literal>int</literal> and the last is a <literal>String</literal>.
</para>
<bridgehead>Control flow-based pointcuts</bridgehead>
<para>
Some concerns cut across the control flow of the program. The cflow
and cflowbelow primitive pointcut designators capture join points
based on control flow.
</para>
<simplelist>
<member><literal>cflow(<replaceable>Pointcut</replaceable>)</literal></member>
<member><literal>cflowbelow(<replaceable>Pointcut</replaceable>)</literal></member>
</simplelist>
<para>
The cflow pointcut picks out all join points that occur between the start and the
end of each of the pointcut's join points.
</para>
<para>
The cflowbelow pointcut picks out all join points that occur between
the start and the end of each of the pointcut's join points, but
not including the initial join point of the control flow itself.
</para>
<bridgehead>Program text-based pointcuts</bridgehead>
<para>
While many concerns cut across the runtime structure of the program,
some must deal with the actual lexical structure. AspectJ allows
aspects to pick out join points based on where their associated code
is defined.
</para>
<simplelist>
<member><literal>within(<replaceable>TypePattern</replaceable>)</literal></member>
<member><literal>withincode(<replaceable>Signature</replaceable>)</literal></member>
</simplelist>
<para>
The within pointcut picks out all join points where the code
executing is defined in the declaration of one of the types in
<replaceable>TypePattern</replaceable>. This includes the class
initialization, object initialization, and method and constructor
execution join points for the type, as well as any join points
associated with the statements and expressions of the type. It also
includes any join points that are associated with code within any of
the type's nested types.
</para>
<para>
The withincode pointcut picks out all join points where the code
executing is defined in the declaration of a particular method or
constructor. This includes the method or constructor execution join
point as well as any join points associated with the statements and
expressions of the method or constructor. It also includes any join
points that are associated with code within any of the method or
constructor's local or anonymous types.
</para>
<bridgehead>Dynamic property-based pointcuts</bridgehead>
<simplelist>
<member><literal>if(<replaceable>BooleanExpression</replaceable>)</literal></member>
</simplelist>
<para>
The if pointcut picks out join points based on a dynamic property.
It's syntax takes an expression, which must evaluate to a boolean
true or false. Within this expression, the
<literal>thisJoinPoint</literal> object is available. So one
(extremely inefficient) way of picking out all call join points would
be to use the pointcut
</para>
<programlisting>
if(thisJoinPoint.getKind().equals("call"))
</programlisting>
</sect2>
<sect2>
<title>Signatures</title>
<para>
One very important property of a join point is its signature, which is
used by many of AspectJ's pointcut designators to select particular
join points.
</para>
<para>
At a method call join point, the signature is composed of the type used
to access the method, the name of the method, and the the types of the called
method's formal parameters and return value (if any).
</para>
<para>
At a method execution join point, the signature is composed of the type
defining the method, the name of the method, and the the types of the executing
method's formal parameters and return value (if any).
</para>
<para>
At a constructor call join point, the signature is composed of the type
of the object to be constructed and the types of the
called constructor's formal parameters.
</para>
<para>
At a constructor execution join point, the signature is composed of the
type defining the constructor and the types of the executing
constructor's formal parameters.
</para>
<para>
At an object initialization join point, the signature is composed of
the type being initialized and the types of the formal parameters of
the first constructor entered during the initialization of this type.
</para>
<para>
At an object pre-initialization join point, the signature is composed
of the type being initialized and the types of the formal parameters of
the first constructor entered during the initialization of this type.
</para>
<para>
At a field reference or assignment join point, the signature is
composed of the type used to access or assign to the field, the name of
the field, and the type of the field.
</para>
<para>
At a handler execution join point, the signature is composed of the
exception type that the handler handles.
</para>
<para>
The <literal>withincode</literal>, <literal>call</literal>,
<literal>execution</literal>, <literal>get</literal>, and
<literal>set</literal> primitive pointcut designators all use signature
patterns to determine the join points they describe. A signature
pattern is an abstract description of one or more join-point
signatures. Signature patterns are intended to match very closely the
same kind of things one would write when defining individual methods
and constructors.
</para>
<para>
Method definitions in Java include method names, method parameters,
return types, modifiers like static or private, and throws clauses,
while constructor definitions omit the return type and replace the
method name with the class name. The start of a particular method
definition, in class <literal>Test</literal>, for example, might be
</para>
<programlisting>
class C {
public final void foo() throws ArrayOutOfBoundsException { ... }
}
</programlisting>
<para>
In AspectJ, method signature patterns have all these, but most elements
can be replaced by wildcards. So
</para>
<programlisting>
call(public final void C.foo() throws ArrayOutOfBoundsException)
</programlisting>
<para>
picks out call join points to that method, and the pointcut
</para>
<programlisting>
call(public final void *.*() throws ArrayOutOfBoundsException)
</programlisting>
<para>
picks out all call join points to methods, regardless of their name
name or which class they are defined on, so long as they take no
arguments, return no value, are both <literal>public</literal> and
<literal>final</literal>, and are declared to throw
<literal>ArrayOutOfBounds</literal> exceptions.
</para>
<para>
The defining type name, if not present, defaults to *, so another way
of writing that pointcut would be
</para>
<programlisting>
call(public final void *() throws ArrayOutOfBoundsException)
</programlisting>
<para>
Formal parameter lists can use the wildcard <literal>..</literal> to
indicate zero or more arguments, so
</para>
<programlisting>
execution(void m(..))
</programlisting>
<para>
picks out execution join points for void methods named
<literal>m</literal>, of any number of arguments, while
</para>
<programlisting>
execution(void m(.., int))
</programlisting>
<para>
picks out execution join points for void methods named
<literal>m</literal> whose last parameter is of type
<literal>int</literal>.
</para>
<para>
The modifiers also form part of the signature pattern. If an AspectJ
signature pattern should match methods without a particular modifier,
such as all non-public methods, the appropriate modifier should be
negated with the <literal>!</literal> operator. So,
</para>
<programlisting>
withincode(!public void foo())
</programlisting>
<para>
picks out all join points associated with code in null non-public
void methods named <literal>foo</literal>, while
</para>
<programlisting>
withincode(void foo())
</programlisting>
<para>
picks out all join points associated with code in null void methods
named <literal>foo</literal>, regardless of access modifier.
</para>
<para>
Method names may contain the * wildcard, indicating any number of
characters in the method name. So
</para>
<programlisting>
call(int *())
</programlisting>
<para>
picks out all call join points to <literal>int</literal> methods
regardless of name, but
</para>
<programlisting>
call(int get*())
</programlisting>
<para>
picks out all call join points to <literal>int</literal> methods
where the method name starts with the characters "get".
</para>
<para>
AspectJ uses the <literal>new</literal> keyword for constructor
signature patterns rather than using a particular class name. So the
execution join points of private null constructor of a class C
defined to throw an ArithmeticException can be picked out with
</para>
<programlisting>
execution(private C.new() throws ArithmeticException)
</programlisting>
</sect2>
<sect2>
<title>Type patterns</title>
<para>
Type patterns are a way to pick out collections of types and use them
in places where you would otherwise use only one type. The rules for
using type patterns are simple.
</para>
<bridgehead>Type name patterns</bridgehead>
<para>
First, all type names are also type patterns. So
<literal>Object</literal>, <literal>java.util.HashMap</literal>,
<literal>Map.Entry</literal>, <literal>int</literal> are all type
patterns.
</para>
<para>
There is a special type name, *, which is also a type pattern. * picks out all
types, including primitive types. So
</para>
<programlisting>
call(void foo(*))
</programlisting>
<para>
picks out all call join points to void methods named foo, taking one
argument of any type.
</para>
<para>
Type names that contain the two wildcards "*" and
"<literal>..</literal>" are also type patterns. The * wildcard matches
zero or more characters characters except for ".", so it can be used
when types have a certain naming convention. So
</para>
<programlisting>
handler(java.util.*Map)
</programlisting>
<para>
picks out the types java.util.Map and java.util.java.util.HashMap,
among others, and
</para>
<programlisting>
handler(java.util.*)
</programlisting>
<para>
picks out all types that start with "<literal>java.util.</literal>" and
don't have any more "."s, that is, the types in the
<literal>java.util</literal> package, but not inner types
(such as java.util.Map.Entry).
</para>
<para>
The "<literal>..</literal>" wildcard matches any sequence of
characters that start and end with a ".", so it can be used
to pick out all types in any subpackage, or all inner types. So
</para>
<programlisting>
target(com.xerox..*)
</programlisting>
<para>
picks out all join points where the target object is an instance of
defined in any type beginning with "<literal>com.xerox.</literal>".
</para>
<bridgehead>Subtype patterns</bridgehead>
<para>
It is possible to pick out all subtypes of a type (or a collection of
types) with the "+" wildcard. The "+" wildcard follows immediately a
type name pattern. So, while
</para>
<programlisting>
call(Foo.new())
</programlisting>
<para>
picks out all constructor call join points where an instance of exactly
type Foo is constructed,
</para>
<programlisting>
call(Foo+.new())
</programlisting>
<para>
picks out all constructor call join points where an instance of any
subtype of Foo (including Foo itself) is constructed, and the unlikely
</para>
<programlisting>
call(*Handler+.new())
</programlisting>
<para>
picks out all constructor call join points where an instance of any
subtype of any type whose name ends in "Handler" is constructed.
</para>
<bridgehead>Array type patterns</bridgehead>
<para>
A type name pattern or subtype pattern can be followed by one or more
sets of square brackets to make array type patterns. So
<literal>Object[]</literal> is an array type pattern, and so is
<literal>com.xerox..*[][]</literal>, and so is
<literal>Object+[]</literal>.
</para>
<bridgehead>Type patterns</bridgehead>
<para>
Type patterns are built up out of type name patterns, subtype patterns,
and array type patterns, and constructed with boolean operators
<literal><![CDATA[&&]]></literal>, <literal>||</literal>, and
<literal>!</literal>. So
</para>
<programlisting>
staticinitialization(Foo || Bar)
</programlisting>
<para>
picks out the static initializer execution join points of either Foo or Bar,
and
</para>
<programlisting>
call((Foo+ <![CDATA[&&]]> ! Foo).new(..))
</programlisting>
<para>
picks out the constructor call join points when a subtype of Foo, but
not Foo itself, is constructed.
</para>
</sect2>
<!-- ============================== -->
<sect2>
<title>Pointcuts and Join Points</title>
<para>It is possible to pick out every different kind of join point with
pointcuts, but some of the less common ones require pointcut
combination. </para>
<sect3>
<title>Method call </title> <!-- add chain up -->
<programlisting>
aspect A {
after() returning: call(void foo()) {
System.err.println(thisJoinPoint.getKind()); // should be "method-call"
}
}
</programlisting>
</sect3>
<sect3>
<title>Method execution</title>
<programlisting>
aspect A {
after() returning: execution(void foo()) {
System.err.println(thisJoinPoint.getKind()); // should be "method-execution"
}
}
</programlisting>
</sect3>
<sect3>
<title>Constructor call</title> <!-- add chain up -->
<programlisting>
aspect A {
after() returning: call(Foo.new()) {
System.err.println(thisJoinPoint.getKind()); // should be "constructor-call"
}
}
</programlisting>
</sect3>
<sect3>
<title>Constructor execution<!-- [add chain up] --></title>
<programlisting>
aspect A {
after() returning: execution(Foo.new()) {
System.err.println(thisJoinPoint.getKind()); // should be "constructor-execution"
}
}
</programlisting>
</sect3>
<sect3>
<title>Static initializer execution<!-- [add chain up] --></title>
<programlisting>
aspect A {
after() returning: staticinitializer(Foo) {
System.err.println(thisJoinPoint.getKind()); // should be "static-initializar"
}
}
</programlisting>
</sect3>
<sect3>
<title>Object pre-initialization<!-- [add chain up] --></title>
<para>This join point will most commonly be seen as the enclosing
execution join point of a particular call, since it cannot be simply
picked out by AspectJ's primitive pointcuts.</para>
<programlisting>
aspect A {
after() returning: call(Foo) {
System.err.println(thisEnclosingJoinPointStaticPart.getKind()); // should be "pre-initialization"
}
}
</programlisting>
</sect3>
<sect3>
<title>Object initialization<!-- [add chain up] --></title>
<programlisting>
aspect A {
after() returning: initialization(Foo.new()) {
System.err.println(thisEnclosingJoinPointStaticPart.getKind()); // should be "initialization"
}
}
</programlisting>
</sect3>
<sect3>
<title>Field Reference <!-- [add chain up] --></title>
<programlisting>
aspect A {
after() returning: get(Foo.x) {
System.err.println(thisEnclosingJoinPointStaticPart.getKind()); // should be "field-get"
}
}
</programlisting>
</sect3>
<sect3>
<title>Field Assignment <!-- [add chain up] --></title>
<programlisting>
aspect A {
after() returning: set(Foo.x) {
System.err.println(thisEnclosingJoinPointStaticPart.getKind()); // should be "field-set"
}
}
</programlisting>
</sect3>
<sect3>
<title>Handler Execution <!-- [add chain up] --></title>
<programlisting>
aspect A {
after() returning: handler(FooExn) {
System.err.println(thisEnclosingJoinPointStaticPart.getKind()); // should be "handler"
}
}
</programlisting>
</sect3>
</sect2>
</sect1>
<sect1>
<title>Advice</title>
<simplelist>
<member><literal>before(<replaceable>Formals</replaceable>): <replaceable>Pointcut</replaceable> { <replaceable>Body</replaceable> }</literal></member>
<member><literal>after(<replaceable>Formals</replaceable>) returning [ (<replaceable>Formal</replaceable>) ]: <replaceable>Pointcut</replaceable> { <replaceable>Body</replaceable> }</literal></member>
<member><literal>after(<replaceable>Formals</replaceable>) throwing [ (<replaceable>Formal</replaceable>) ]: <replaceable>Pointcut</replaceable> { <replaceable>Body</replaceable> }</literal></member>
<member><literal>after(<replaceable>Formals</replaceable>) : <replaceable>Pointcut</replaceable> { <replaceable>Body</replaceable> }</literal></member>
<member><literal><replaceable>Type</replaceable> around(<replaceable>Formals</replaceable>) [ throws <replaceable>TypeList</replaceable> ] : <replaceable>Pointcut</replaceable> { <replaceable>Body</replaceable> }</literal></member>
</simplelist>
<para>
Advice defines crosscutting behavior. It is defined in terms of
pointcuts. The code of a piece of advice runs at every join point picked
out by its pointcut. Exactly how the code runs depends on the kind of
advice.
</para>
<para>
AspectJ supports three kinds of advice. The kind of advice determines how
it interacts with the join points it is defined over. Thus AspectJ
divides advice into that which runs before its join points, that which
runs after its join points, and that which runs in place of (or "around")
its join points.
</para>
<para>
While before advice is relatively unproblematic, there can be three
interpretations of after advice: After the execution of a join point
completes normally, after it throws an exception, or after it does either
one. AspectJ allows after advice for any of these situations.
</para>
<programlisting>
aspect A {
pointcut publicCall(): call(public Object *(..));
after() returning (Object o): publicCall() {
System.out.println("Returned normally with " + o);
}
after() throwing (Exception e): publicCall() {
System.out.println("Threw an exception: " + e);
}
after(): publicCall(){
System.out.println("Returned or threw an Exception");
}
}
</programlisting>
<para>
After returning advice may not care about its returned object, in which
case it may be written
</para>
<programlisting>
after() returning: call(public Object *(..)) {
System.out.println("Returned normally");
}
</programlisting>
<para>
It is an error to try to put after returning advice on a join point that
does not return the correct type. For example,
</para>
<programlisting>
after() returning (byte b): call(int String.length()) {
// this is an error
}
</programlisting>
<para>
is not allowed. But if no return value is exposed, or the exposed return
value is typed to <literal>Object</literal>, then it may be applied to
any join point. If the exposed value is typed to
<literal>Object</literal>, then the actual return value is converted to
an object type for the body of the advice: <literal>int</literal> values
are represented as <literal>java.lang.Integer</literal> objects, etc, and
no value (from void methods, for example) is represented as
<literal>null</literal>.
</para>
<para>
Around advice runs in place of the join point it operates over, rather
than before or after it. Because around is allowed to return a value, it
must be declared with a return type, like a method. A piece of around
advice may be declared <literal>void</literal>, in which case it is not
allowed to return a value, and instead whatever value the join point
returned will be returned by the around advice (unless the around advice
throws an exception of its own).
</para>
<para>
Thus, a simple use of around advice is to make a particular method
constant:
</para>
<programlisting>
aspect A {
int around(): call(int C.foo()) {
return 3;
}
}
</programlisting>
<para>
Within the body of around advice, though, the computation of the original
join point can be executed with the special syntax
</para>
<programlisting>
proceed( ... )
</programlisting>
<para>
The proceed form takes as arguments the context exposed by the around's
pointcut, and returns whatever the around is declared to return. So the
following around advice will double the second argument to
<literal>foo</literal> whenever it is called, and then halve its result:
</para>
<programlisting>
aspect A {
int around(int i): call(int C.foo(Object, int)) <![CDATA[&&]]> args(i) {
int newi = proceed(i*2)
return newi/2;
}
}
</programlisting>
<para>
If the return value of around advice is typed to
<literal>Object</literal>, then the result of proceed is converted to an
object representation, even if it is originally a primitive value. And
when the advice returns an Object value, that value is converted back to
whatever representation it was originally. So another way to write the
doubling and halving advice is:
</para>
<programlisting>
aspect A {
Object around(int i): call(int C.foo(Object, int)) <![CDATA[&&]]> args(i) {
Integer newi = (Integer) proceed(i*2)
return new Integer(newi.intValue() / 2);
}
}
</programlisting>
<para>
In all kinds of advice, the parameters of the advice behave exactly like
method parameters. In particular, assigning to any parameter affects
only the value of the parameter, not the value that it came from. This
means that
</para>
<programlisting>
aspect A {
after() returning (int i): call(int C.foo()) {
i = i * 2;
}
}
</programlisting>
<para>
will <emphasis>not</emphasis> double the returned value of the advice.
Rather, it will double the local parameter. Changing the values of
parameters or return values of join points can be done by using around
advice.
</para>
<sect2>
<title>Advice modifiers</title>
<para>
The <literal>strictfp</literal> modifier is the only modifier allowed
on advice, and it has the effect of making all floating-point
expressions within the advice be FP-strict.
</para>
</sect2>
<sect2>
<title>Advice and checked exceptions</title>
<para>
An advice declaration must include a <literal>throws</literal> clause
listing the checked exceptions the body may throw. This list of
checked exceptions must be compatible with each target join point
of the advice, or an error is signalled by the compiler.
</para>
<para>
For example, in the following declarations:
</para>
<programlisting>
import java.io.FileNotFoundException;
class C {
int i;
int getI() { return i; }
}
aspect A {
before(): get(int C.i) {
throw new FileNotFoundException();
}
before() throws FileNotFoundException: get(int C.i) {
throw new FileNotFoundException();
}
}
</programlisting>
<para>
both pieces of advice are illegal. The first because the body throws
an undeclared checked exception, and the second because field get join
points cannot throw <literal>FileNotFoundException</literal>s.
</para>
<para> The exceptions that each kind of join point in AspectJ may throw are:
</para>
<variablelist>
<varlistentry>
<term>method call and execution</term>
<listitem>
<para>the checked exceptions declared by the target method's
<literal>throws</literal> clause.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>constructor call and execution</term>
<listitem>
<para>the checked exceptions declared by the target constructor's
<literal>throws</literal> clause.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>field get and set</term>
<listitem>
<para>no checked exceptions can be thrown from these join points. </para>
</listitem>
</varlistentry>
<varlistentry>
<term>exception handler execution</term>
<listitem>
<para>the exceptions that can be thrown by the target exception handler.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>static initializer execution</term>
<listitem>
<para>no checked exceptions can be thrown from these join points. </para>
</listitem>
</varlistentry>
<varlistentry>
<term>initializer execution, pre-initialization, and initialization</term>
<listitem>
<para>any exception that is in the throws clause of
<emphasis>all</emphasis> constructors of the initialized class. </para>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2>
<title>Advice precedence</title>
<para>
Multiple pieces of advice may apply to the same join point. In such
cases, the resolution order of the advice is based on advice
precedence.
</para>
<sect3>
<title>Determining precedence</title>
<para>There are a number of rules that determine whether a particular
piece of advice has precedence over another when they advise the same
join point. </para>
<para>If the two pieces of advice are defined in different aspects,
then there are three cases: </para>
<simplelist>
<member>If aspect A is declared such that it <literal>dominates</literal>
aspect B, then all advice defined in A has precedence over all advice
defined in
B. </member>
<member>
Otherwise, if aspect A is a subaspect of aspect B, then all advice
defined in A has precedence over all advice defined in
B. So, unless otherwise specified with a
<literal>dominates</literal> keyword, advice in a subaspect
dominates advice in a superaspect.
</member>
<member>
Otherwise, if two pieces of advice are defined in two different
aspects, it is undefined which one has precedence.
</member>
</simplelist>
<para>If the two pieces of advice are defined in the same aspect, then
there are two cases: </para>
<simplelist>
<member>If either are <literal>after</literal> advice, then the one that
appears later in the aspect has precedence over the one that appears
earlier. </member>
<member>Otherwise, then the one that appears earlier in the aspect
has precedence over the one that appears later.
</member>
</simplelist>
<para>These rules can lead to circularity, such as</para>
<programlisting>
aspect A {
before(): execution(void main(String[] args)) {}
after(): execution(void main(String[] args)) {}
before(): execution(void main(String[] args)) {}
}
</programlisting>
<para>such circularities will result in errors signalled by the compiler. </para>
</sect3>
<sect3>
<title>Effects of precedence</title>
<para>At a particular join point, advice is ordered by precedence.</para>
<para>A piece of <literal>around</literal> advice controls whether
advice of lower precedence will run by calling
<literal>proceed</literal>. The call to <literal>proceed</literal>
will run the advice with next precedence, or the computation under the
join point if there is no further advice. </para>
<para>A piece of <literal>before</literal> advice can prevent advice of
lower precedence from running by throwing an exception. If it returns
normally, however, then the advice of the next precedence, or the
computation under the join pint if there is no further advice, will run.
</para>
<para>Running <literal>after returning</literal> advice will run the
advice of next precedence, or the computation under the join point if
there is no further advice. Then, if that computation returned
normally, the body of the advice will run. </para>
<para>Running <literal>after throwing</literal> advice will run the
advice of next precedence, or the computation under the join
point if there is no further advice. Then, if that computation threw
an exception of an appropriate type, the body of the advice will
run. </para>
<para>Running <literal>after</literal> advice will run the advice of
next precedence, or the computation under the join point if
there is no further advice. Then the body of the advice will
run. </para>
</sect3>
</sect2>
<sect2>
<title>Reflective access to the join point</title>
<para>
Three special variables are visible within bodies of advice:
<literal>thisJoinPoint</literal>,
<literal>thisJoinPointStaticPart</literal>, and
<literal>thisEnclosingJoinPointStaticPart</literal>. Each is bound to
an object that encapsulates some of the context of the advice's current
or enclosing join point. These variables exist because some pointcuts
may pick out very large collections of join points. For example, the
pointcut
</para>
<programlisting>
pointcut publicCall(): call(public * *(..));
</programlisting>
<para>
picks out calls to many methods. Yet the body of advice over this
pointcut may wish to have access to the method name or parameters of a
particular join point.
</para>
<para>
<literal>thisJoinPoint</literal> is bound to a complete join point
object, while <literal>thisJoinPointStaticPart</literal> is bound to a
part of the join point object that includes less information,
but for which no memory allocation is required on each execution of the
advice.
</para>
<para>
<literal>thisEnclosingJoinPointStaticPart</literal> is bound to the
static part of the join point enclosing the current join point. Only
the static part of this enclosing join point is available through this
mechanism.
</para>
<para>
Like standard Java reflection, which uses objects from the
<literal>java.lang.reflect</literal> hierarchy, join point objects have
types in a type hierarchy. The type of objects bound to
<literal>thisJoinPoint</literal> is
<literal>org.aspectj.lang.JoinPoint</literal>, while
<literal>thisStaticJoinPoint</literal> is bound to objects of interface
type <literal>org.aspectj.lang.JoinPoint.StaticPart</literal>.
</para>
</sect2>
</sect1>
<sect1>
<title>Static crosscutting</title>
<para>
Advice declarations change the behavior of classes they crosscut, but do
not change their static type structure. For crosscutting concerns that do
operate over the static structure of type hierarchies, AspectJ provides
forms of introduction.
</para>
<para>
Each introduction form is a member of the aspect defining it, but defines
a new member of another type.
</para>
<sect2>
<title>Member introduction</title>
<para>
A method introduction looks like
</para>
<simplelist>
<member><literal><replaceable>Modifiers</replaceable>
<replaceable>Type</replaceable> <replaceable>TypePattern</replaceable>
.
<replaceable>Id</replaceable>(<replaceable>Formals</replaceable>)
{ <replaceable>Body</replaceable> }</literal></member>
<member><literal>abstract <replaceable>Modifiers</replaceable>
<replaceable>Type</replaceable> <replaceable>TypePattern</replaceable>
. <replaceable>Id</replaceable>(<replaceable>Formals</replaceable>);
</literal></member>
</simplelist>
<para>
The effect of such an introduction is to make all the types in TypePattern
support the new method. Interfaces in TypePattern will support the new method
as well, even if the method is neither public nor abstract, so the
following is legal AspectJ code:
</para>
<programlisting>
interface Iface {}
aspect A {
private void Iface.m() {
System.err.println("I'm a private method on an interface");
}
void worksOnI(Iface iface) {
// calling a private method on an interface
iface.m();
}
}
</programlisting>
<para>
A constructor introduction looks like
</para>
<simplelist>
<member><literal><replaceable>Modifiers</replaceable> <replaceable>TypePattern</replaceable>.new(<replaceable>Formals</replaceable>)
{ <replaceable>Body</replaceable> }</literal></member>
</simplelist>
<para>
The effect of such an introduction is to make all the types in
TypePattern support the new constructor. You cannot introduce a
constructor onto an interface, so if TypePattern includes an interface
type it is an error.
</para>
<para>
A field introduction looks like one of
</para>
<simplelist>
<member><literal><replaceable>Modifiers</replaceable>
<replaceable>Type</replaceable> <replaceable>TypePattern</replaceable>.<replaceable>Id</replaceable> = <replaceable>Expression</replaceable>;</literal></member>
<member><literal><replaceable>Modifiers</replaceable>
<replaceable>Type</replaceable> <replaceable>TypePattern</replaceable>.<replaceable>Id</replaceable>;</literal></member>
</simplelist>
<para>
The effect of such an introduction is to make all the types in
TypePattern support the new field. Interfaces in TypePattern will
support the new field as well, even if the field is neither public,
nor static, nor final.
</para>
</sect2>
<para>
Any occurrence of the identifier <literal>this</literal> in the body of
the constructor or method introduction, or in the initializer of a
field introduction, refers to the target type from the
<replaceable>TypePattern</replaceable> rather than to the aspect type.
</para>
<sect2>
<title>Access modifiers</title>
<para>
Members may be introduced with access modifiers public or private, or
the default package-protected (protected introduction is not
supported).
</para>
<para>
The access modifier applies in relation to the aspect, not in relation
to the target type. So a member that is privately introduced is visible
only from code that is defined within the aspect introducing it. One
that is package-protectedly introduced is visible only from code that
is defined within the introducing aspect's package.
</para>
<para>
Note that privately introducing a method (which AspectJ supports) is
very different from introducing a private method (which AspectJ
previously supported). AspectJ does not allow the introduction of the
private method "void writeObject(ObjectOutputStream)" required to
implement the interface java.io.Serializable.
</para>
</sect2>
<sect2>
<title>Conflicts</title>
<para>
Introduction may cause conflicts among introduced members and between
introduced members and defined members.
</para>
<para>
Assuming <literal>otherPackage</literal> is not the package defining
the aspect <classname>A</classname>, the code
</para>
<programlisting>
aspect A {
private Registry otherPackage.*.r;
public void otherPackage.*.register(Registry r) {
r.register(this);
this.r = r;
}
}
</programlisting>
<para>
adds a field "<literal>r</literal>" to every type in otherPackage. This
field is only accessible from the code inside of aspect
<literal>A</literal>. The aspect also adds a
"<literal>register</literal>" method to every type in
<literal>otherPackage</literal>. This method is accessible
everywhere.
</para>
<para>
If any type in <literal>otherPackage</literal> already defines a
private or package-protected field "<literal>r</literal>", there is no
conflict: The aspect cannot see such a field, and no code in
<literal>otherPackage</literal> can see the introduced
"<literal>r</literal>".
</para>
<para>
If any type in <literal>otherPackage</literal> defines a public field
"<literal>r</literal>", there is a conflict: The expression
</para>
<programlisting>
this.r = r
</programlisting>
<para>
is an error, since it is ambiguous whether the introduced
"<literal>r</literal>" or the public "<literal>r</literal>" should be
used.
</para>
<para>
If any type in <literal>otherPackage</literal> defines any method
"<literal>register(Registry)</literal>" there is a conflict, since it
would be ambiguous to any code that could see such a defined method
which "<literal>register(Registry)</literal>" method was applicable.
</para>
<para>
Conflicts are resolved as much as possible as per Java's conflict
resolution rules:
</para>
<simplelist>
<member>A subclass can inherit multiple <emphasis>fields</emphasis> from its superclasses,
all with the same name and type. However, it is an error to have an ambiguous
<emphasis>reference</emphasis> to a field.</member>
<member>A subclass can only inherit multiple
<emphasis>methods</emphasis> with the same name and argument types from
its superclasses if only zero or one of them is concrete (i.e., all but
one is abstract, or all are abstract).
</member>
</simplelist>
<para>
Given a potential conflict between inter-type member declarations in
different aspects, if one aspect dominates the other its declaration will
take effect without any conflict notice from compiler. This is true both
when the domination is declared explicitly in a "dominates" clause and
when sub-aspects implicitly dominate their corresponding super-aspect.
</para>
</sect2>
<sect2>
<title>Extension and Implementation</title>
<para>
An aspect may introduce a superclass or superinterface onto a type,
with the declarations
</para>
<simplelist>
<member><literal>declare parents: <replaceable>TypePattern</replaceable> extends <replaceable>TypeList</replaceable>;</literal></member>
<member><literal>declare parents: <replaceable>TypePattern</replaceable> implements <replaceable>TypeList</replaceable>;</literal></member>
</simplelist>
<para>
For example, if an aspect wished to make a particular class runnable,
it might add an appropriate <literal>void run()</literal> method, but
it should also change the type of the class to specify that it fulfills
the <literal>Runnable</literal> interface. In order to implement the
methods in the <literal>Runnable</literal> interface, the
<literal>run()</literal> method must be publically introduced:
</para>
<programlisting>
aspect A {
declare parents: SomeClass implements Runnable;
public void SomeClass.run() { ... }
}
</programlisting>
</sect2>
<sect2>
<title>Interfaces with members</title>
<para>
Through the use of introduction, interfaces may now carry
(non-public-static-final) fields and (non-public-abstract) methods that
classes can inherit. Conflicts may occur from ambiguously inheriting
members from a superclass and multiple superinterfaces.
</para>
<para>
Because interfaces may carry non-static initializers, the order of
super-interface instantiation is observable. We fix this order with the
following three properties: A supertype is initialized before a
subtype, that initialized code runs only once, and initializers for
supertypes run in left-to-right order. Consider the following hierarchy
where {<literal>Object</literal>, <literal>C</literal>,
<literal>D</literal>, <literal>E</literal>} are classes,
{<literal>M</literal>, <literal>N</literal>, <literal>O</literal>,
<literal>P</literal>, <literal>Q</literal>} are interfaces.
</para>
<programlisting>
Object M O
\ / \ /
C N Q
\ / /
D P
\ /
E
</programlisting>
<para>
when a new <literal>E</literal> is instantiated, the initializers run in this order:
</para>
<programlisting>
Object M C O N D Q P E
</programlisting>
</sect2>
<sect2>
<title>Warnings and Errors</title>
<para>An aspect may specify that a particular join point should never be
reached. </para>
<simplelist>
<member><literal>declare error: <replaceable>Pointcut</replaceable>: <replaceable>String</replaceable>;</literal></member>
<member><literal>declare warning: <replaceable>Pointcut</replaceable>: <replaceable>String</replaceable>;</literal></member>
</simplelist>
<para>If the compiler determines that a join point in
<replaceable>Pointcut</replaceable> could possibly be reached, then it
will signal either an error or warning, as declared, using the
<replaceable>String</replaceable> for its message. </para>
</sect2>
<sect2>
<title>Softened exceptions</title>
<para>An aspect may specify that a particular kind of exception, if
thrown at a join point, should bypass Java's usual static exception
checking system and instead be thrown as a
<literal>org.aspectj.lang.SoftException</literal>, which is subtype of
<literal>RuntimeException</literal> and thus does not need to be
declared. </para>
<simplelist>
<member><literal>declare soft: <replaceable>TypePattern</replaceable>: <replaceable>Pointcut</replaceable>;</literal></member>
</simplelist>
<para>For example, the aspect</para>
<programlisting>
aspect A {
declare soft: Exception: execution(void main(String[] args));
}
</programlisting>
<para>Would, at the execution join point, catch any
<literal>Exception</literal> and rethrow a
<literal>org.aspectj.lang.SoftException</literal> containing
original exception. </para>
<para>This is similar to what the following advice would do</para>
<programlisting>
aspect A {
void around() execution(void main(String[] args)) {
try { proceed(); }
catch (Exception e) {
throw new org.aspectj.lang.SoftException(e);
}
}
}
</programlisting>
<para>except, in addition to wrapping the exception, it also affects
Java's static exception checking mechanism. </para>
</sect2>
<sect2>
<title>Statically determinable pointcuts</title>
<para>Pointcuts that appear inside of <literal>declare</literal> forms
have certain restrictions. Like other pointcuts, these pick out join
points, but they do so in a way that is statically determinable. </para>
<para>Consequently, such pointcuts may not include, directly or
indirectly (through user-defined pointcut declarations) pointcuts that
discriminate based on dynamic (runtime) context. Therefore, such
pointcuts may not be defined in terms of</para>
<simplelist>
<member>cflow</member>
<member>cflowbelow</member>
<member>this</member>
<member>target</member>
<member>args</member>
<member>if</member>
</simplelist>
<para> all of which can discriminate on runtime information. </para>
</sect2>
</sect1>
<sect1>
<title>Aspects</title>
<para>
An aspect is a crosscutting type defined by the aspect declaration. The
aspect declaration is similar to the class declaration in that it defines
a type and an implementation for that type. It differs in that the type
and implementation can cut across other types (including those defined by
other aspect declarations), and that it may not be directly instantiated
with a new expression, with cloning, or with serialization. Aspects may
have one constructor definition, but if so it must be of a constructor
taking no arguments and throwing no checked exceptions.
</para>
<para>
Aspects may be defined either at the package level, or as a static nested
aspect, that is, a static member of a class, interface, or aspect. If it
is not at the package level, the aspect <emphasis>must</emphasis> be
defined with the static keyword. Local and anonymous aspects are not
allowed.
</para>
<sect2>
<title>Aspect Extension</title>
<para>
To support abstraction and composition of crosscutting concerns,
aspects can be extended in much the same way that classes can. Aspect
extension adds some new rules, though.
</para>
<sect3>
<title>Aspects may extend classes and implement interfaces</title>
<para>
An aspect, abstract or concrete, may extend a class and may implement
a set of interfaces. Extending a class does not provide the ability
to instantiate the aspect with a new expression: The aspect may still
only define a null constructor.
</para>
</sect3>
<sect3>
<title>Classes may not extend aspects</title>
<para>
It is an error for a class to extend or implement an aspect.
</para>
</sect3>
<sect3>
<title>Aspects extending aspects
</title>
<para>
Aspects may extend other aspects, in which case not only are fields
and methods inherited but so are pointcuts. However, aspects may only
extend abstract aspects. It is an error for a concrete aspect to
extend another concrete aspect.
</para>
</sect3>
</sect2>
<sect2>
<title>Aspect instantiation</title>
<para>
Unlike class expressions, aspects are not instantiated with
<literal>new</literal> expressions. Rather, aspect instances are
automatically created to cut across programs.
</para>
<para>
Because advice only runs in the context of an aspect instance, aspect
instantiation indirectly controls when advice runs.
</para>
<para>
The criteria used to determine how an aspect is instantiated
is inherited from its parent aspect. If the aspect has no parent
aspect, then by default the aspect is a singleton aspect.
</para>
<sect3>
<title>Singleton Aspects</title>
<simplelist>
<member><literal>aspect <replaceable>Id</replaceable> { ... }</literal></member>
<member><literal>aspect <replaceable>Id</replaceable> issingleton { ... }</literal></member>
</simplelist>
<para>
By default, or by using the modifier <literal>issingleton</literal>, an
aspect has exactly one instance that cuts across the entire program.
That instance is available at any time during program execution with
the static method <literal>aspectOf()</literal> defined on the aspect
-- so, in the above examples, <literal>A.aspectOf()</literal> will
return A's instance. This aspect instance is created as the aspect's
classfile is loaded.
</para>
<para>
Because the an instance of the aspect exists at all join points in
the running of a program (once its class is loaded), its advice will
have a chance to run at all such join points.
</para>
</sect3>
<sect3>
<title>Per-object aspects</title>
<simplelist>
<member><literal>aspect <replaceable>Id</replaceable> perthis(<replaceable>Pointcut</replaceable>) { ... }</literal></member>
<member><literal>aspect <replaceable>Id</replaceable> pertarget(<replaceable>Pointcut</replaceable>) { ... }</literal></member>
</simplelist>
<para>
If an aspect A is defined
<literal>perthis(<replaceable>Pointcut</replaceable>)</literal>, then
one object of type A is created for every object that is the
executing object (i.e., "this") at any of the join points picked out
by <replaceable>Pointcut</replaceable>.
The advice defined in A may then run at any join point where the
currently executing object has been associated with an instance of
A.
</para>
<para> Similarly, if an aspect A is defined
<literal>pertarget(<replaceable>Pointcut</replaceable>)</literal>,
then one object of type A is created for every object that is the
target object of the join points picked out by
<replaceable>Pointcut</replaceable>.
The advice defined in A may then run at any join point where the
target object has been associated with an instance of
A.
</para>
<para>
In either case, the static method call
<literal>A.aspectOf(Object)</literal> can be used to get the aspect
instance (of type A) registered with the object. Each aspect
instance is created as early as possible, but not before reaching a
join point picked out by <replaceable>Pointcut</replaceable> where
there is no associated aspect of type A.
</para>
<para> Both <literal>perthis</literal> and <literal>pertarget</literal>
aspects may be affected by code the AspectJ compiler controls, as
discussed in the <xref linkend="limitations"/> appendix. </para>
</sect3>
<sect3>
<title>Per-control-flow aspects</title>
<simplelist>
<member><literal>aspect <replaceable>Id</replaceable> percflow(<replaceable>Pointcut</replaceable>) { ... }</literal></member>
<member><literal>aspect <replaceable>Id</replaceable> percflowbelow(<replaceable>Pointcut</replaceable>) { ... }</literal></member>
</simplelist>
<para>
If an aspect A is defined
<literal>percflow(<replaceable>Pointcut</replaceable>)</literal> or
<literal>percflowbelow(<replaceable>Pointcut</replaceable>)</literal>,
then one object of type A is created for each flow of control of the
join points picked out by <replaceable>Pointcut</replaceable>, either
as the flow of control is entered, or below the flow of control,
respectively. The advice defined in A may run at any join point in
or under that control flow. During each such flow of control, the
static method <literal>A.aspectOf()</literal> will return an object
of type
A. An instance of the aspect is created upon entry into each such
control flow.
</para>
</sect3>
</sect2>
<sect2>
<title>Aspect privilege</title>
<simplelist>
<member><literal>privileged aspect <replaceable>Id</replaceable> { ... }</literal></member>
</simplelist>
<para>
Code written in aspects is subject to the same access control rules as
Java code when referring to members of classes or aspects. So, for
example, code written in an aspect may not refer to members with
default (package-protected) visibility unless the aspect is defined in
the same package.
</para>
<para>
While these restrictions are suitable for many aspects, there may be
some aspects in which advice or introductions needs to access private
or protected resources of other types. To allow this, aspects may be
declared <literal>privileged</literal>. Code in priviliged aspects has
access to all members, even private ones.
</para>
<programlisting>
class C {
private int i = 0;
void incI(int x) { i = i+x; }
}
privileged aspect A {
static final int MAX = 1000;
before(int x, C c): call(void C.incI(int)) <![CDATA[&&]]> target(c) <![CDATA[&&]]> args(x) {
if (c.i+x > MAX) throw new RuntimeException();
}
}
</programlisting>
<para>
In this case, if A had not been declared privileged, the field reference
c.i would have resulted in an error signalled by the compiler.
</para>
<para>
If a privileged aspect can access multiple versions of a particular
member, then those that it could see if it were not privileged take
precedence. For example, in the code
</para>
<programlisting>
class C {
private int i = 0;
void foo() { }
}
privileged aspect A {
private int C.i = 999;
before(C c): call(void C.foo()) target(c) {
System.out.println(c.i);
}
}
</programlisting>
<para>
A's introduced private field C.i, initially bound to 999, will be
referenced in the body of the advice in preference to C's privately
declared field, since the A would have access to fields it introduces
even if it were not privileged.
</para>
</sect2>
<sect2>
<title>Aspect domination</title>
<simplelist>
<member><literal>aspect <replaceable>Id</replaceable> dominates <replaceable>TypePattern</replaceable> { ... }</literal></member>
</simplelist>
<para>
An aspect may declare that the advice in it dominates the advice in
some other aspect. Such declarations are like the
<literal>strictfp</literal> keyword in Java; it applies to the advice
declarations inside of the respective aspects, and states that the
advice declared in the current aspect has more precedence than the
advice in the aspects from <replaceable>TypePattern</replaceable>.
</para>
<para>
For example, the CountEntry aspect might want to count the entry to
methods in the current package accepting a Type object as its first
argument. However, it should count all entries, even those that the
aspect DisallowNulls causes to throw exceptions. This can be
accomplished by stating that CountEntry dominates DisallowNulls.
</para>
<programlisting>
aspect DisallowNulls {
pointcut allTypeMethods(Type obj): call(* *(..)) <![CDATA[&&]]> args(obj, ..);
before(Type obj): allTypeMethods(obj) {
if (obj == null) throw new RuntimeException();
}
}
aspect CountEntry dominates DisallowNulls {
pointcut allTypeMethods(Type obj): call(* *(..)) <![CDATA[&&]]> args(obj, ..);
static int count = 0;
before(): allTypeMethods(Type) {
count++;
}
}
</programlisting>
</sect2>
</sect1>
</appendix>
<!--
Local variables:
compile-command: "java sax.SAXCount -v progguide.xml && java com.icl.saxon.StyleSheet -w0 progguide.xml progguide.html.xsl"
fill-column: 79
sgml-local-ecat-files: progguide.ced
sgml-parent-document:("progguide.sgml" "book" "appendix")
End:
-->
|