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
|
<!--<!DOCTYPE chapter SYSTEM "file:/C:/Documents%20and%20Settings/colyer/My%20Documents/projects/aspectjdev/lib/docbook/docbook-dtd/docbookx.dtd">
-->
<chapter id="generics" xreflabel="Generics">
<title>Generics</title>
<sect1 id="generics-inJava5">
<title>Generics in Java 5</title>
<para>
This section provides the essential information about generics in
Java 5 needed to understand how generics are treated in AspectJ 5.
For a full introduction to generics in Java, please see the
documentation for the Java 5 SDK.
</para>
<sect2>
<title>Declaring Generic Types</title>
<para>
A generic type is declared with one or more type parameters following the type name.
By convention formal type parameters are named using a single letter, though this is not required.
A simple generic list type
(that can contain elements of any type <literal>E</literal>) could be declared:
</para>
<programlisting><![CDATA[
interface List<E> {
Iterator<E> iterator();
void add(E anItem);
E remove(E anItem);
}
]]></programlisting>
<para>
It is important to understand that unlike template mechanisms there will only be one type, and one class file, corresponding to
the <literal>List</literal> interface, regardless of how many different instantiations of the <literal>List</literal> interface a program
has (each potentially providing a different value for the type parameter <literal>E</literal>). A consequence of this
is that you cannot refer to the type parameters of a type declaration in a static method or initializer, or in the declaration or
initializer of a static variable.
</para>
<para>
A <emphasis>parameterized type</emphasis>
is an invocation of a generic type with concrete values supplied for
all of its type parameters (for example, <literal>List<String></literal> or <literal>List<Food></literal>).
</para>
<para>A generic type may be declared with multiple type parameters. In addition to simple type parameter names, type
parameter declarations can also constrain the set of types allowed by using the <literal>extends</literal>
keyword. Some examples follow:</para>
<variablelist>
<varlistentry>
<term>class Foo<T> {...}</term>
<listitem>
<para>A class <literal>Foo</literal> with one type parameter, <literal>T</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>class Foo<T,S> {...}</term>
<listitem>
<para>A class <literal>Foo</literal> with two type parameters, <literal>T</literal> and <literal>S</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>class Foo<T extends Number> {...}</term>
<listitem>
<para>A class <literal>Foo</literal> with one type parameter <literal>T</literal>, where <literal>T</literal> must be
instantiated as the type <literal>Number</literal> or a subtype of <literal>Number</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>class Foo<T, S extends T> {...}</term>
<listitem>
<para>A class <literal>Foo</literal> with two type parameters, <literal>T</literal> and <literal>S</literal>. <literal>Foo</literal>
must be instantiated with a type <literal>S</literal> that is a subtype of the type specified for parameter <literal>T</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>class Foo<T extends Number & Comparable> {...}</term>
<listitem>
<para>A class <literal>Foo</literal> with one type parameter, <literal>T</literal>. <literal>Foo</literal>
must be instantiated with a type that is a subtype of <literal>Number</literal> and that implements <literal>Comparable</literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2>
<title>Using Generic and Parameterized Types</title>
<para>You declare a variable (or a method/constructor argument) of a parameterized type by specifying a concrete type specfication for each type parameter in
the generic type. The following example declares a list of strings and a list of numbers:</para>
<programlisting><![CDATA[
List<String> strings;
List<Number> numbers;
]]></programlisting>
<para>It is also possible to declare a variable of a generic type without specifying any values for the type
parameters (a <emphasis>raw</emphasis> type). For example, <literal>List strings</literal>.
In this case, unchecked warnings may be issued by the compiler
when the referenced object is passed as a parameter to a method expecting a parameterized type such as a
<literal>List<String></literal>. New code written in the Java 5 language would not be expected to use
raw types.</para>
<para>Parameterized types are instantiated by specifying type parameter values in the constructor call expression as in
the following examples:</para>
<programlisting><![CDATA[
List<String> strings = new MyListImpl<String>();
List<Number> numbers = new MyListImpl<Number>();
]]></programlisting>
<para>
When declaring parameterized types, the <literal>?</literal> wildcard may be used, which stands for "some type".
The <literal>extends</literal> and <literal>super</literal> keywords may be used in conjunction with the wildcard
to provide upper and lower bounds on the types that may satisfy the type constraints. For example:
</para>
<variablelist>
<varlistentry>
<term>List<?></term>
<listitem>
<para>A list containing elements of some type, the type of the elements in the list is unknown.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>List<? extends Number></term>
<listitem>
<para>A list containing elements of some type that extends Number, the exact type of the elements in the list is unknown.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>List<? super Double></term>
<listitem>
<para>A list containing elements of some type that is a super-type of Double, the exact type of the elements in the list is unknown.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
A generic type may be extended as any other type. Given a generic type <literal>Foo<T></literal> then
a subtype <literal>Goo</literal> may be declared in one of the following ways:
</para>
<variablelist>
<varlistentry>
<term>class Goo extends Foo</term>
<listitem>
<para>Here <literal>Foo</literal> is used as a raw type, and the appropriate warning messages will be
issued by the compiler on attempting to invoke methods in <literal>Foo</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>class Goo<E> extends Foo</term>
<listitem>
<para><literal>Goo</literal> is a generic type, but the super-type <literal>Foo</literal> is used as a raw
type and the appropriate warning messages will be
issued by the compiler on attempting to invoke methods defined by <literal>Foo</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>class Goo<E> extends Foo<E></term>
<listitem>
<para>This is the most usual form. <literal>Goo</literal> is a generic type with one parameter that extends
the generic type <literal>Foo</literal> with that same parameter. So <literal>Goo<String<</literal> is
a subclass of <literal>Foo<String></literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>class Goo<E,F> extends Foo<E></term>
<listitem>
<para><literal>Goo</literal> is a generic type with two parameters that extends
the generic type <literal>Foo</literal> with the first type parameter of <literal>Goo</literal> being used
to parameterize <literal>Foo</literal>. So <literal>Goo<String,Integer<</literal> is
a subclass of <literal>Foo<String></literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>class Goo extends Foo<String></term>
<listitem>
<para><literal>Goo</literal> is a type that extends
the parameterized type <literal>Foo<String></literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>A generic type may implement one or more generic interfaces, following the type binding
rules given above. A type may also implement one or more parameterized interfaces (for example,
<literal>class X implements List<String></literal>, however a type may not at the same time
be a subtype of two interface types which are different parameterizations of the same interface.</para>
</sect2>
<sect2>
<title>Subtypes, Supertypes, and Assignability</title>
<para>
The supertype of a generic type <literal>C</literal> is the type given in the extends clause of
<literal>C</literal>, or <literal>Object</literal> if no extends clause is present. Given the type declaration
</para>
<programlisting><![CDATA[
public interface List<E> extends Collection<E> {... }
]]></programlisting>
<para>
then the supertype of <literal>List<E></literal> is <literal>Collection<E></literal>.
</para>
<para>
The supertype of a parameterized type <literal>P</literal> is the type given in the extends clause of
<literal>P</literal>, or <literal>Object</literal> if no extends clause is present. Any type parameters in
the supertype are substituted in accordance with the parameterization of <literal>P</literal>. An example
will make this much clearer: Given the type <literal>List<Double></literal> and the definition of
the <literal>List</literal> given above, the direct supertype is
<literal>Collection<Double></literal>. <literal>List<Double></literal> is <emphasis>not</emphasis>
considered to be a subtype of <literal>List<Number></literal>.
</para>
<para>
An instance of a parameterized type <literal>P<T1,T2,...Tn></literal>may be assigned to a variable of
the same type or a supertype
without casting. In addition it may be assigned to a variable <literal>R<S1,S2,...Sm></literal> where
<literal>R</literal> is a supertype of <literal>P</literal> (the supertype relationship is reflexive),
<literal>m <= n</literal>, and for all type parameters <literal>S1..m</literal>, <literal>Tm</literal> equals
<literal>Sm</literal> <emphasis>or</emphasis> <literal>Sm</literal> is a wildcard type specification and
<literal>Tm</literal> falls within the bounds of the wildcard. For example, <literal>List<String></literal>
can be assigned to a variable of type <literal>Collection<?></literal>, and <literal>List<Double></literal>
can be assigned to a variable of type <literal>List<? extends Number></literal>.
</para>
</sect2>
<sect2>
<title>Generic Methods and Constructors</title>
<para>
A static method may be declared with one or more type parameters as in the following declaration:
</para>
<programlisting><![CDATA[
static <T> T first(List<T> ts) { ... }
]]></programlisting>
<para>
Such a definition can appear in any type, the type parameter <literal>T</literal> does not need to
be declared as a type parameter of the enclosing type.
</para>
<para>
Non-static methods may also be declared with one or more type parameters in a similar fashion:
</para>
<programlisting><![CDATA[
<T extends Number> T max(T t1, T t2) { ... }
]]></programlisting>
<para>The same technique can be used to declare a generic constructor.</para>
</sect2>
<sect2>
<title>Erasure</title>
<para>Generics in Java are implemented using a technique called <emphasis>erasure</emphasis>. All
type parameter information is erased from the run-time type system. Asking an object of a parameterized
type for its class will return the class object for the raw type (eg. <literal>List</literal> for an object
declared to be of type <literal>List<String></literal>. A consequence of this is that you cannot at
runtime ask if an object is an <literal>instanceof</literal> a parameterized type.</para>
</sect2>
</sect1>
<!-- ===================================================================== -->
<sect1 id="generics-inAspectJ5">
<title>Generics in AspectJ 5</title>
<para>
AspectJ 5 provides full support for all of the Java 5 language features, including generics. Any legal Java 5 program is a
legal AspectJ 5 progam. In addition, AspectJ 5 provides support for generic and parameterized types in pointcuts, inter-type
declarations, and declare statements. Parameterized types may freely be used within aspect members, and support is
also provided for generic <emphasis>abstract</emphasis> aspects.
</para>
<sect2>
<title>Matching generic and parameterized types in type patterns</title>
<para>
The foundation of AspectJ's support for generic and parameterized types in aspect declarations is the extension of type
pattern matching to allow matching against generic and parameterized types.
</para>
<para>
The type pattern <literal>"Foo"</literal> matches all types named <literal>Foo</literal>, whether they
be simple types, generic types, or parameterized types. So for example, <literal>Foo</literal>,
<literal>Foo<T></literal>, and <literal>Foo<String></literal>will all be matched.
</para>
<para>
AspectJ 5 also extends the specification of type patterns to allow explicit matching of generic and parameterized
types by including one or more type parameter patterns inside angle braces (<literal>< ></literal>) immediately
after the type pattern. For example, <literal>List<String></literal>
</para>
<programlisting><![CDATA[
TypePattern := SimpleTypePattern |
'!' TypePattern |
'(' AnnotationPattern? TypePattern ')'
TypePattern '&&' TypePattern |
TypePattern '||' TypePattern |
TypePattern '<' TypeParameterPatternList '>'
TypeParameterPatternList ::= TypeParameterPattern (',' TypeParameterPattern)*
TypeParameterPattern ::= TypePattern |
'?' TypeBoundPattern?
TypeBoundPattern ::= 'extends' TypePattern AdditionalBoundPatternList? |
'super' TypePattern AdditionalBoundPatternList?
AdditionalBoundPatternList ::= AdditionalBoundPattern AdditionalBoundPatternList |
AdditionalBoundPattern
AdditionalBoundPattern ::= '&' TypePattern
]]></programlisting>
<para>
A simple identifier (such as <literal>String</literal>) occuring in a type parameter list will be treated as a type name unless
a type variable of that name is in scope (declaring type variables is covered later). The type pattern <literal>List<E></literal>
will result in an "invalid absolute type name" warning if no type <literal>E</literal> is in scope (declared in the default package, or
imported in the compilation unit) and no declaration of <literal>E</literal> as a type variable is in scope either.
</para>
<para>Some simple examples of type patterns follow:</para>
<variablelist>
<varlistentry>
<term>List<String></term>
<listitem>
<para>Matches the parameterized type <literal>List<String></literal>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>List<? extends Number></term>
<listitem>
<para>Matches the parameterized type <literal>List<? extends Number></literal>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>List<E></term>
<listitem>
<para>Outside of a scope in which <literal>E</literal>is defined as a type variable, this pattern matches the
parameterized type <literal>List<E></literal>. If <literal>E</literal> is not
a type then an <literal>invalidAbsoluteTypeName</literal> xlint warning will be issued.
</para>
<para>In a scope in which
<literal>E</literal> is defined as a type variable, this pattern matches the generic type <literal>List<E></literal>.
The type parameter name does not have to match the name used in the declaration of <literal>List</literal>,
but the bounds must match. This pattern <emphasis>also</emphasis> matches any parameterization of <literal>List</literal>
that satisfies the bounds of the type variable (for example, <literal>List<String></literal>).
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
The <literal>*</literal>, <literal>+</literal>, and <literal>..</literal> wildcards may be used in type patterns
matching against generic and parameterized types (just as in any other type pattern). The <literal>+</literal>
wildcard matches all subtypes. Recalling the discussion on subtypes and supertypes in the previous section, note
that the pattern <literal>List<Number>+</literal> will match <literal>List<Number></literal> and
<literal>LinkedList<Number></literal>, but not <literal>List<Double></literal>. To match lists of
any number type use the pattern <literal>List<Number+></literal> which will match
<literal>List<Number></literal>, <literal>List<Double></literal>, <literal>List<Float></literal>
and so on.
</para>
<para>
The generics wildcard <literal>?</literal> is considered part of the signature of a parameterized type, and
is <emphasis>not</emphasis> used as an AspectJ wildcard in type matching. For example:
</para>
<variablelist>
<varlistentry>
<term>List<*></term>
<listitem>
<para>Matches any generic or parameterized <literal>List</literal>type (<literal>List<String></literal>,
<literal>List<Integer></literal> and so on) with a single type parameter.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>List<?></term>
<listitem>
<para>Matches the parameterized type <literal>List<?></literal> (and does
<emphasis>not</emphasis> match <literal>List<String></literal>,
<literal>List<Integer></literal> and so on)
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>List<? extends Number+></term>
<listitem>
<para>Matches <literal>List<? extends Number></literal>, <literal>List<? extends Double></literal>,
and so on, but does not match <literal>List<Double></literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2>
<title>Signature patterns</title>
<para>
Now that we understand how to write type patterns that match generic and parameterized types, it is time to look at
how these can be utilized to match member declarations by using signature patterns.
</para>
<para>To match members declared in generic types and making use of type variables defined in those types (for
example <literal>interface Foo<T> { public T doSomething(); }</literal> use a signature pattern of the form:</para>
<programlisting><![CDATA[
X Foo<X>.doSomething()
]]></programlisting>
<para>
This assumes a scope in which <literal>X</literal> is declared as a type variable. As with type patterns, the name
of the type variable does not have to match the name used in the member declaration, but the bounds must match.
For example, if the interface was declared as <literal>Foo<T extends Number></literal> then the signature
pattern would be: <literal>X Foo<X extends Number>.doSomething()</literal>.
</para>
<variablelist>
<varlistentry>
<term>T Util<T extends Number,S>.someFunction(List<S>)</term>
<listitem>
<para>Matches the method <literal>someFunction</literal> in a generic type <literal>Util</literal> with
two type parameters, the first type parameter having an upper bound of <literal>Number</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>LinkedList<E>.new()</term>
<listitem>
<para>Matches the no-argument constructor of the generic type <literal>LinkedList</literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
Matching a field with a generic type works in the same way. For example:
</para>
<programlisting><![CDATA[
T *<T>.*
]]></programlisting>
<para>Matches a field of the type of type parameter <literal>T</literal> in any generic type with a single
unbounded type parameter (the pattern<literal>*<T></literal>). The field may be of any name.
</para>
<para>Matching of members of parameterized types is straightforward. For example,
<literal>void List<String>.add(String)</literal> matches the add method in the
parameterized type <literal>List<String></literal>.
</para>
<para>
To match a generic <emphasis>method</emphasis> the generic method type variable
declarations become part of the signature pattern. For example:
</para>
<programlisting><![CDATA[
<T> List<T> *.favourites(List<T>)
]]></programlisting>
<para>matches a generic method <literal>favourites</literal> declared in any type. To match a
static generic method simply include the <literal>static</literal> modifier in the type pattern.</para>
</sect2>
<sect2>
<title>Pointcuts</title>
<para>
In this section we discuss how type patterns and signature patterns matching on generic and
parameterized types, methods, and constructors can be used in pointcut expressions.
We distinguish between pointcuts that match based on static type information, and pointcuts
that match based on runtime type information (<literal>this, target, args</literal>).
</para>
<para>
First however we need to address the notion of type variables and scopes. There is a
convention in Java, but no requirement, that type variables are named with a single letter.
Likewise it is rare, but perfectly legal, to declare a type with a single character name. Given the
type pattern <literal>List<Strng></literal>, is this a mis-spelling of the
parameterized type pattern <literal>List<String></literal> or is it a generic type pattern
with one unbounded type variable <literal>Strng</literal>?. Alternatively, given the
type pattern <literal>List<E></literal>, if the type <literal>E</literal> cannot be found,
is this a missing import statement or an implied type variable? There is no way for AspectJ
to disambiguate in these situations without an explicit declaration of type variable names. If
<literal>E</literal> is defined as a type variable, and <literal>Strng</literal> is not, then both
declarations can be correctly interpreted.
</para>
<sect3>
<title>Type Variables in Pointcut Expressions</title>
<para>The type variables in scope for a pointcut primitive are declared in a type variable
list immediately following the pointcut desginator keyword. For example:</para>
<programlisting><![CDATA[
call<T>(* Foo<T>.*(T))
]]></programlisting>
<para>matches a call to a method with any name (<literal>*</literal>) declared
by a generic type <literal>Foo</literal> with one unbounded type parameter. The method
takes one argument which is of the type of the type variable.</para>
<para>In contrast, the pointcut</para>
<programlisting><![CDATA[
call(* Foo<T>.*(T))
]]></programlisting>
<para>matches a call to a method with any name that takes an argument of
type <literal>T</literal>, where the target of the call is declared as the parameterized
type <literal>Foo<T></literal>. If there is no type <literal>T</literal> in scope, an
"invalid absolute type name (T)" warning will be issued.</para>
<para>
The type variables declaration following a pointcut designator permits only simple identifiers
(e.g. <literal><S,T></literal> and not <literal><S extends Number></literal>).
</para>
<para>A type variable declaration list can appear following any pointcut designator except
for <literal>handler</literal> (Java 5 does
not permit a generic class to be a direct or indirect subtype of <literal>Throwable</literal>
- see JLS 8.1.2), the dynamic pointcuts <literal>this, target, args, if, cflow, cflowbelow</literal>,
and the annotation pointcut designators
(<literal>@args, @this, @within</literal> and so on).</para>
</sect3>
<sect3>
<title>Initialization and execution pointcuts</title>
<para>
Recall that there is only ever one type for a generic type (e.g. <literal>List<E></literal>)
regardless of how many different parameterizations of that type (e.g.
<literal>List<String></literal>, <literal>List<Double></literal>) are used within a
program. For join points that occur within a type, such as execution join points, it therefore only
makes sense to talk about execution join points for the generic type. Given the generic type
</para>
<programlisting><![CDATA[
public class Foo<T> {
T doSomething(T toSomeT) {
return T;
}
}
]]></programlisting>
<para>
then
</para>
<variablelist>
<varlistentry>
<term>execution<T>(T Foo<T>.doSomething(..))</term>
<listitem>
<para>matches the execution of the <literal>doSomething</literal> method in
<literal>Foo</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>execution(* Foo.doSomething(..))</term>
<listitem>
<para>also matches the execution of the <literal>doSomething</literal> method in
<literal>Foo</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>execution(T Foo.doSomething(..))</term>
<listitem>
<para>results in an "invalid absolute type name (T)" warning since <literal>T</literal> is
interpreted as a type, not a type variable.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>execution(String Foo<String>.doSomething(..))</term>
<listitem>
<para>results in a compilation error "no execution join points for parameterized type
Foo<String>, use a generic signature instead".
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
Given the type declaration
</para>
<programlisting><![CDATA[
public class Bar<N extends Number> {
N doSomething(N toSomeN) {
return N;
}
}
]]></programlisting>
<para>
then
</para>
<variablelist>
<varlistentry>
<term>execution<T>(T Bar<T>.doSomething(..))</term>
<listitem>
<para>does not match the execution of <literal>Bar.doSomething</literal> since
the bounds of the type parameter <literal>T</literal> in the pointcut expression do
not match the bounds of the type parameter <literal>N</literal> in the type declaration.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>execution<T>(T Bar<T extends Number>.doSomething(..))</term>
<listitem>
<para>matches the execution of the <literal>doSomething</literal> method in
<literal>Bar</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>execution<T extends Number>(T Bar<T>.doSomething(..))</term>
<listitem>
<para>results in a compilation error, since type variable bounds must be specified as part
of the declaring type pattern, and not in the type variable list.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
If a type implements a <emphasis>parameterized</emphasis> interface, then
execution join points exist and can be matched for the parameterized interface operations within
the implementing type. For example, given the pair of types:
</para>
<programlisting><![CDATA[
public interface Greatest<T> {
T greatest(List<T> ts);
}
public class NumberOperations implements Greatest<Number> {
public Number greatest(List<Number> numbers) {
//...
}
}
]]></programlisting>
<para>
then
</para>
<programlisting><![CDATA[
execution(* Greatest<Number>.*(..))
]]></programlisting>
<para>
will match the execution of the <literal>greatest</literal> method declared in
<literal>NumberOperations</literal>. However, it <emphasis>does not</emphasis>
match the execution of <literal>greatest</literal> in the program below:
</para>
<programlisting><![CDATA[
public interface Greatest<T> {
T greatest(List<T> ts);
}
public class NumberOperations<N extends Number> implements Greatest<N> {
public N greatest(List<N> numbers) {
//...
}
}
// in some fragment of code...
NumberOperations<Number> numOps = new NumberOperations<Number>();
numOps.greatest(numList);
]]></programlisting>
<para>Since there is only one generic type, <literal>NumberOperations</literal>,
which implements a generic interface. Either of the pointcut expressions
<literal>execution<T>(* Greatest<T>>.*(..))</literal> or
<literal>execution<T>(* Greatest<T extends Number>>.*(..))</literal> will
match the execution of <literal>greatest</literal> in this example. Recall from
chapter <xref linkend="jpsigs"/> that a kinded pointcut primitive matches a join point if
it exactly matches one of the signatures of the join point. The signatures of the
execution join point for <literal>greatest</literal> in the example above are:</para>
<variablelist>
<varlistentry>
<term>public N Greatest<N>.greatest(List<N>)</term>
<listitem>
<para>from the declaration in the <literal>Greatest</literal> interface, and
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>public N Greatest<N extends Number>.greatest(List<N>)</term>
<listitem>
<para>from the additional bounds restriction of <literal>N</literal> in the
declaration of <literal>NumberOperations</literal>
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
Join points for <literal>staticinitialization</literal>,<literal>initialization</literal> and
<literal>preinitialization</literal>
only ever exist on a generic type (an interface cannot define a constructor). The expression
<literal>initialization<T>(Foo<T>.new(..))</literal> which match any initialization
join point for the generic type <literal>Foo<T></literal>, and
<literal>staticinitialization<T>(Foo<T>)</literal> matches the static initialization
of that same type.
</para>
<para>
The expression <literal>staticinitialization(List<String>)</literal> will result in a
compilation error: there is no static initialization join point for the parameterized type
<literal>List<String></literal>. However, the expression
<literal>staticinitialization(List<String>+)</literal> <emphasis>is</emphasis>
legal, and will match the static initialization of any type that
<literal>implements List<String></literal>. The expression
<literal>staticinitialization<T>(List<T>+)</literal> will match the static
initialization join point of any type that either extends or implements the generic
type <literal>List<T></literal> or implements any parameterization of that
interface.
</para>
</sect3>
<sect3>
<title>Static scoping: within and withincode</title>
<para>The <literal>within</literal> and <literal>withincode</literal>
pointcut designators both match the
execution of join points that occur within a type or a member of a type respectively. Therefore
the same considerations with respect to there only being <literal>one</literal> type for
a generic type regardless of how many parameterizations of that type are used in a program
apply.
</para>
<para>The <literal>within</literal> pointcut designator can never be used in conjunction
with a simple parameterized type. So
</para>
<variablelist>
<varlistentry>
<term>within<T>(Foo<T>)</term>
<listitem>
<para>matches all join points occurring within the generic type <literal>Foo<T></literal>,
and
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>within(Foo<String>)</term>
<listitem>
<para>results in a compilation error since there is no concept of a join point within a
parameterized type, but
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>within(Foo<String>+)</term>
<listitem>
<para>matches any join point occurring within a type that
<literal>implements Foo<String></literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>The <literal>withincode</literal> designator is likewise normally used with a
generic type, but can be used with a parameterized interface type to match join points
arising from code lexically within the implementation of the interface methods in a type
that implements the parameterized interface.
</para>
<variablelist>
<varlistentry>
<term>withincode<T>(* Foo<T>.*(..))</term>
<listitem>
<para>matches all join points arising from code lexically within a method of the
generic type <literal>Foo<T></literal>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>withincode(* Foo<String>.*(..))</term>
<listitem>
<para>results in a compilation error if <literal>Foo</literal> is not an interface. If
<literal>Foo</literal> is an interface then it matches all join points arising from
code lexically within the implementation of the interface methods in a type that
implements <literal>Foo<String></literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>withincode(* Foo<String>+.*(..))</term>
<listitem>
<para>matches any join point occurring within a method of a type that
<literal>implements Foo<String></literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3>
<title>Call, get and set pointcuts</title>
<para>
The <literal>call, get,</literal> and <literal>set</literal> join points can occur on the client
side (ie. outside of the type owning the member being called, accessed, or updated) or
within the type that owns the target member. The following short program demonstrates this:
</para>
<programlisting><![CDATA[
public class Foo<T> {
public T timeFor;
public Foo<T>(T aCuppa) {
timeFor = aCuppa; // set-site A
}
public void doThis(T t) {
doThat(t); // call-site A
}
public void doThat(T t) {
return;
}
}
public class Main {
public static void main(String[] args) {
Foo<String> foos = new Foo<String>();
foos.doThis("b"); //call-site B
foos.doThat("c"); // call-site C
foos.timeFor = "a cuppa"; // set-site B
}
}
]]></programlisting>
<para>
We have annotated the three method call sites as call-site A, call-site B, and call-site C.
Call-site A is situated within the generic type <literal>Foo<T></literal> and the call
join point has signature <literal>public void Foo<T>doThat(T)</literal>. The join point
arising from call-site B is a client-side call join point and has the signatures
<literal>public void Foo<String>doThis(String)</literal> (from the static type of
<literal>foos</literal>) <emphasis>and</emphasis>
<literal>public void Foo<T>doThis(T)</literal>. Likewise the call join point arising from
call-site C has the signatures
<literal>public void Foo<String>doThat(String)</literal> (from the static type of
<literal>foos</literal>) <emphasis>and</emphasis>
<literal>public void Foo<T>doThat(T)</literal>. A call pointcut expression matches if the
signature pattern exactly matches one of the signatures of the call join point.
</para>
<para>
The signatures for get and set join points works in a similar fashion. At set-site A in the above
example, the set join point has signature <literal>public T Foo<T>.timeFor</literal>. At
set-site B the set join point has signatures <literal>public T Foo<T>.timeFor</literal> and
<literal>public String Foo<String>.timeFor</literal>. A get or set pointcut expression
matches if the signature pattern exactly matches one of the signatures of the join point.
</para>
Some examples follow:
<variablelist>
<varlistentry>
<term>call(* List<?>.*(..))</term>
<listitem>
<para>matches a call to any method of a <literal>List<?></literal> (a call where the
target is declared to be a <literal>List<?></literal>). For example:
</para>
<programlisting><![CDATA[
int countItems(List<?> anyList) {
return anyList.size(); // matched by call(* List<?>.*(..))
}
]]></programlisting>
</listitem>
</varlistentry>
<varlistentry>
<term>call<T>(* List<T>.*(..))</term>
<listitem>
<para>matches any call to an operation defined in the generic type
<literal>List<E></literal>. This includes calls made to <literal>List<String></literal>,
<literal>List<Number></literal>, <literal>List<? super Foo></literal> and so on.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>get<T>(T *<T extends Account>.*)</term>
<listitem>
<para>matches the get of any field defined in a generic type with one type parameter that has
an upper bound of <literal>Account</literal>. The field has the type of the type parameter, and
can be of any name. This pointcut expression matches both gets of the field within the
declaring type, and also gets on parameterized instances of the type.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>set(Account Foo<Account>.*Account)</term>
<listitem>
<para>matches the set of a field of type <literal>Account</literal> where the target
is of type <literal>Foo<Account></literal> and the field name ends with "Account". Does not
match sets of any "*Account" field occurring within the <literal>Foo</literal> type itself.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>call(* List<? extends Number>.add(..))</term>
<listitem>
<para>matches any call to add an element to a list of type <literal>List<? extends Number></literal>.
Does not match calls to add elements to lists of type <literal>List<Number></literal> or
<literal>List<Double></literal> as these are distinct types.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>call(* List<Number+>.add(..))</term>
<listitem>
<para>matches any call to add an element to a list of type <literal> Number</literal> or
any subclass of <literal>Number</literal>. For example, <literal>List<Number>,
List<Double> List<Float></literal>.
Does not match calls to add elements to lists of type <literal>List<? extends Number></literal>
as this is a distinct type.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect3>
<sect3>
<title>Handler</title>
<para>
The Java Language Specification states that a generic class may not be a direct or indirect
subclass of <literal>Throwable</literal>. Therefore it is a compilation error to use a generic
or parameterized type pattern in a <literal>handler</literal> pointcut expression.
</para>
</sect3>
<sect3>
<title>Runtime type matching: this, target and args</title>
<para>
Java 5 generics are implemented using a technique known an <emphasis>erasure</emphasis>.
In particular, what gets "erased" is the ability to find out the parameterized runtime type
of an instance of a generic type. You can ask if something is an <literal>instanceof List</literal>,
but not if something is an <literal>instanceof List<String></literal>
</para>
<para>
The <literal>this, target</literal> and <literal>args</literal> pointcut designators all match
based on the runtime type of the appropriate object (this, target, or argument) at a join point.
To match any parameterization of a generic type, simply use the raw type (type variables are
not permitted with these designators). For example:
</para>
<variablelist>
<varlistentry>
<term>target(List)</term>
<listitem>
<para>matches any call to an instance of <literal>List</literal> (including
<literal>List<String>, List<Number></literal>, and so on.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>args (List)</term>
<listitem>
<para>matches any join point with a single argument that is an instance of
<literal>List</literal>.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
To match specific parameterizations of a generic type, simply use the type that you require
the relevant object to be an instance of inside the pointcut expression. For example:
<literal>target(List<String>)</literal>.
</para>
<para>
Recall that runtime tests to determine whether an object is an instance of a parameterized
type are not possible due to erasure. Therefore AspectJ matching behaviour with
parameterized types for <literal>this, target</literal> and <literal>args</literal> is as follows.
</para>
<simplelist>
<member>If it can be statically determined that a given object will always be an instance
of the required type, then the pointcut expressions matches. For example, given a variable
<literal>bankAccounts</literal>
of type <literal>Set<BankAccount></literal> and the pointcut expression
<literal>target(Set<BankAccount>)</literal> then any call made to
<literal>bankAccounts</literal> will be matched.</member>
<member>If it can be statically determined that a given object can never be an
instance of the required type, then the pointcut expression does not match. The
expression <literal>target(List<String>)</literal>will never match a call made
using a variable of type <literal>List<Number></literal> (it is not possible for
a type to implement two different parameterizations of the same interface).</member>
<member>If an object <emphasis>might</emphasis> be an instance of the required
type in some circumstances but not in others, then since it is not possible to perform
the runtime test, AspectJ deems the pointcut expression to match, but issues an
unchecked warning. This is analogous to the behaviour of the Java compiler when
converting between raw and parameterized types. Given a variable of type
<literal>List<? extends Number></literal> and a call join point with
target pointcut expression <literal>target(List<Double>)</literal> then
the expression matches but with an unchecked warning. The warning can be suppressed
by annotating the associated advice with either <literal>@SuppressAjWarnings</literal>
or <literal>@SuppressAjWarnings("unchecked")</literal>.</member>
</simplelist>
<para>
When using a parameterized type with the
<literal>this</literal> pointcut designator then a joinpoint is unambiguously
matched if and only if one or more of the following conditions hold:
</para>
<simplelist>
<member>the runtime type of the <literal>this</literal> object extends or
implements the parameterized type. For example,
<literal>class Foo implements List<String></literal> will match
<literal>this(List<String>)</literal>.</member>
<member>
The parameterized "this" type is given using a generics wildcard in the pointcut
expression, and the bounds of
the generic runtime type of <literal>this</literal> are such that all valid parameterizations
are matched by the wildcard. For example, the pointcut expression
<literal>this(List<? extends Number>)</literal> will match a <literal>this</literal>
object of type <literal>class Foo<N extends Number> implements List<N></literal>,
but not an object of type <literal>class Foo<N> implements List<N></literal>.
</member>
</simplelist>
<para>
You've already seen some examples of using the generic wildcard <literal>?</literal>
in parameterized type patterns. Since <literal>this, target</literal> and
<literal>args</literal> match using an instance of test, the generic wildcard can be useful in
specifying an acceptable range of parameterized types to match. When used in the binding
form, the same restrictions on operations permitted on the bound variable apply as when a
method declares a parameter with a wildcard type. For example, in the advice below, it is
a compilation error to attemp to add an element into the list <literal>aList</literal>.
</para>
<programlisting><![CDATA[
before(List<? extends Number> aList) :
execution(* org.xyz.Foo.*(..)) && args(aList) {
aList.add(new Double(5.0d)); // Compilation error on this line
}
]]></programlisting>
</sect3>
<sect3>
<title>Declaring pointcuts in generic classes</title>
<para>
AspectJ permits pointcuts to be declared in classes as well as aspects. A pointcut defined
inside a generic class may not use the type variables of the class in the pointcut expression
(just as static members of a generic class may not use type variables).
For example:
</para>
<programlisting><![CDATA[
public class Foo<T extends Number> {
...
// Not allowed - uses T in the pointcut expression
public pointcut fooOperationCall(T t) :
call(* Foo<T>.*(T)) && args(t);
// permitted, but recommended to use an alternate variable name in the local
// type variable declaration - e.g. execution<S>(...)
public pointcut fooExecution(Number n) :
execution<T>(* Foo<T>.*(T)) && args(n);
}
]]></programlisting>
</sect3>
</sect2>
<sect2>
<title>Inter-type Declarations</title>
<para>
AspectJ 5 allows type parameters to be used in inter-type declarations - either for declaring generic
methods and constructors, or for declaring members on generic types. The syntax for declaring generic
methods and constructors follows the regular AspectJ convention of simply qualifying the member name with
the target type.
</para>
<variablelist>
<varlistentry>
<term><T extends Number> T Utils.max(T first, T second) {...}</term>
<listitem>
<para>Declares a generic instance method <literal>max</literal> on the class <literal>Util</literal>.
The <literal>max</literal> method takes two arguments, <literal>first</literal> and <literal>second</literal> which must
both be of the same type (and that type must be Number or a subtype of Number) and returns an instance
of that type.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>static <E> E Utils.first(List<E> elements) {...}</term>
<listitem>
<para>Declares a static generic method <literal>first</literal> on the class <literal>Util</literal>.
The <literal>first</literal> method takes a list of elements of some type, and returns an instance
of that type.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><T> Sorter.new(List<T> elements,Comparator<? super T> comparator) {...}</term>
<listitem>
<para>Declares a constructor on the class <literal>Sorter</literal>.
The constructor takes a list of elements of some type, and a comparator that can compare instances
of the element type.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
A generic type may be the target of an inter-type declaration, used either in its raw form or with
type parameters specified. If type parameters are specified, then the number of type parameters given
must match the number of type parameters in
the generic type declaration. Type parameter <emphasis>names</emphasis> do not have to match.
For example, given the generic type <literal>Foo<T,S extends Number></literal> then:
</para>
<variablelist>
<varlistentry>
<term>String Foo.getName() {...}</term>
<listitem>
<para>Declares a <literal>getName</literal> method on behalf of the raw type <literal>Foo</literal>. It is
not possible to refer to the type parameters of Foo in such a declaration.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>R Foo<Q, R>.getMagnitude() {...}</term>
<listitem>
<para>Declares a method <literal>getMagnitude</literal> on the generic class <literal>Foo</literal>.
The method returns an instance of the type substituted for the second type parameter in an invocation
of <literal>Foo</literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>R Foo<Q, R extends Number>.getMagnitude() {...}</term>
<listitem>
<para>Results in a compilation error since a bounds specification is not allowed in this
form of an inter-type declaration (the bounds are determined from the declaration of the
target type).
</para>
</listitem>
</varlistentry>
</variablelist>
<para>A parameterized type may not be the target of an inter-type declaration. This is because
there is only one type (the generic type) regardless of how many different invocations (parameterizations) of
that generic type are made in a program. Therefore it does not make sense to try and declare a member
on behalf of (say) <literal>Foo<String></literal>, you can only declare members on the generic
type <literal>Foo<T></literal>.
</para>
<para>
If an inter-type member is declared inside a generic aspect, then the type parameter names from the
aspect declaration may be used in the signature specification of the inter-type declaration, but
<emphasis>not</emphasis> as type parameter names for a generic target type. In other words the example
that follows is legal:
</para>
<programlisting><![CDATA[
public abstract aspect A<T> {
private T Foo.data;
public T Foo.getData(T defaultValue) {
return (this.data != null ? data : defaultValue);
}
}
]]></programlisting>
<para>
Whereas the following example is not allowed and will report an error that a parameterized type may not be the
target of an inter-type declaration (since when the type parameter <literal>T</literal> in the aspect is subsituted with
say, <literal>String</literal>, then the target of the inter-type declaration becomes <literal>Goo<String></literal>).
</para>
<programlisting><![CDATA[
public abstract aspect A<T> {
private T Goo<T>.data;
public T Goo<T>.getData(T defaultValue) {
return (this.data != null ? data : defaultValue);
}
}
]]></programlisting>
</sect2>
<sect2>
<title>Declare Parents</title>
<para>Both generic and parameterized types can be used as the parent type in a <literal>declare parents</literal>
statement (as long as the resulting type hierarchy would be well-formed in accordance with Java's sub-typing
rules). Generic types may also be used as the target type of a <literal>declare parents</literal> statement:
a type variable list follows the <literal>parents</literal> keyword in these cases to declare the
type variables in scope.
Some examples follow:</para>
<variablelist>
<varlistentry>
<term>declare parents: Foo implements List<String></term>
<listitem>
<para>The <literal>Foo</literal> type implements the <literal>List<String></literal> interface. If
<literal>Foo</literal> already implements some other parameterization of the <literal>List</literal>
interface (for example, <literal>List<Integer></literal> then a compilation error will result since a
type cannot implement multiple parameterizations of the same generic interface type.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>declare parents <T>: org.xyz..*<T> extends Base<T></term>
<listitem>
<para>All generic types declared in a package beginning with <literal>org.xyz</literal> and with a
single unbounded type parameter, extend the generic type <literal>Base<T></literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>declare parents <T>: org.xyz..*<T> extends Base<S></term>
<listitem>
<para>Results in a compilation error (unless <literal>S</literal> is a type) since <literal>S</literal> is
not bound in the type pattern.
</para>
</listitem>
</varlistentry>
</variablelist>
</sect2>
<sect2>
<title>Declare Soft</title>
<para>It is an error to use a generic or parameterized type as the softened exception type in a declare soft statement. Java 5 does
not permit a generic class to be a direct or indirect subtype of <literal>Throwable</literal> (JLS 8.1.2).</para>
</sect2>
<sect2>
<title>Parameterized Aspects</title>
<para>
AspectJ 5 allows an <emphasis>abstract</emphasis> aspect to be declared as a generic type. Any concrete
aspect extending a generic abstract aspect must extend a parameterized version of the abstract aspect.
Wildcards are not permitted in this parameterization.
</para>
<para>Given the aspect declaration:</para>
<programlisting><![CDATA[
public abstract aspect ParentChildRelationship<P,C> {
...
}
]]></programlisting>
<para>then</para>
<variablelist>
<varlistentry>
<term>public aspect FilesInFolders extends ParentChildRelationship<Folder,File> {...</term>
<listitem>
<para>declares a concrete sub-aspect, <literal>FilesInFolders</literal> which extends the
parameterized abstract aspect <literal>ParentChildRelationship<Folder,File></literal>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>public aspect FilesInFolders extends ParentChildRelationship {...</term>
<listitem>
<para>results in a compilation error since the <literal>ParentChildRelationship</literal> aspect must
be fully parameterized.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>public aspect ThingsInFolders<T> extends ParentChildRelationship<Folder,T></term>
<listitem>
<para>results in a compilation error since concrete aspects may not have type parameters.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>public abstract aspect ThingsInFolders<T> extends ParentChildRelationship<Folder,T></term>
<listitem>
<para>declares a sub-aspect of <literal>ParentChildRelationship</literal> in which <literal>Folder</literal>
plays the role of parent (is bound to the type variable <literal>P</literal>).
</para>
</listitem>
</varlistentry>
</variablelist>
<para>An exception to the rule that concrete aspects may not be generic is a pertypewithin aspect, which
may be declared with a single unbounded type parameter. This is discussed in the chapter on <xref
linkend="pertypewithin" />.</para>
<para>The type parameter variables from a generic aspect declaration may be used in place of a type within any
member of the aspect. For example, we can declare a <literal>ParentChildRelationship</literal> aspect to
manage the bi-directional relationship between parent and child nodes as follows:
</para>
<programlisting><![CDATA[
public abstract aspect ParentChildRelationship<P,C> {
/**
* Parents contain a list of children
*/
private List<C> P.children;
/**
* Each child has a parent
*/
private P C.parent;
/**
* Parents provide access to their children
*/
public List<C> P.getChildren() {
return Collections.unmodifiableList(children);
}
/**
* A child provides access to its parent
*/
public P C.getParent() {
return parent;
}
/**
* ensure bi-directional navigation on adding a child
*/
public void P.addChild(C child) {
if (child.parent != null) {
child.parent.removeChild(child);
}
children.add(child);
child.parent = this;
}
/**
* ensure bi-directional navigation on removing a child
*/
public void P.removeChild(C child) {
if (children.remove(child)) {
child.parent = null;
}
}
/**
* ensure bi-directional navigation on setting parent
*/
public void C.setParent(P parent) {
parent.addChild(this);
}
public pointcut addingChild(P p, C c) :
execution(* P.addChild(C)) && this(p) && args(c);
public pointcut removingChild(P p, C c) :
execution(* P.removeChild(C)) && this(p) && args(c);
}
]]></programlisting>
<para>
Note in the above example how the type parameters <literal>P</literal> and <literal>C</literal> can be
used in inter-type declarations, pointcut expressions, and any other member of the aspect type.
</para>
<para>
The example aspect captures the protocol for managing a bi-directional parent-child relationship between
any two types playing the role of parent and child. In a compiler implementation managing an abstract syntax
tree (AST) in which AST nodes may contain other AST nodes we could declare the concrete aspect:
</para>
<programlisting><![CDATA[
public aspect ASTNodeContainment extends ParentChildRelationship<ASTNode,ASTNode> {
before(ASTNode parent, ASTNode child) : addingChild(parent, child) {
...
}
}
]]></programlisting>
<para>
As a result of this declaration, <literal>ASTNode</literal> gains members:
</para>
<simplelist>
<member><literal>List<ASTNode> children</literal></member>
<member><literal>ASTNode parent</literal></member>
<member><literal>List<ASTNode>getChildren()</literal></member>
<member><literal>ASTNode getParent()</literal></member>
<member><literal>void addChild(ASTNode child)</literal></member>
<member><literal>void removeChild(ASTNode child)</literal></member>
<member><literal>void setParent(ASTNode parent)</literal></member>
</simplelist>
<para>
In a system managing files and folders, we could declare the concrete aspect:
</para>
<programlisting><![CDATA[
public aspect FilesInFolders extends ParentChildRelationship<Folder,File> {
}
]]></programlisting>
<para>
As a result of this declaration, <literal>Folder</literal> gains members:
</para>
<simplelist>
<member><literal>List<File> children</literal></member>
<member><literal>List<File> getChildren()</literal></member>
<member><literal>void addChild(File child)</literal></member>
<member><literal>void removeChild(File child)</literal></member>
</simplelist>
<para>and <literal>File</literal> gains members:</para>
<simplelist>
<member><literal>Folder parent</literal></member>
<member><literal>Folder getParent()</literal></member>
<member><literal>void setParent(Folder parent)</literal></member>
</simplelist>
<para>When used in this way, the type parameters in a generic abstract aspect declare
<emphasis>roles</emphasis>, and the parameterization of the abstract aspect in the <literal>extends</literal>
clause binds types to those roles. This is a case where you may consider departing from the standard practice
of using a single letter to represent a type parameter, and instead use a role name. It makes no difference
to the compiler which option you choose of course.</para>
<programlisting><![CDATA[
public abstract aspect ParentChildRelationship<Parent,Child> {
/**
* Parents contain a list of children
*/
private List<Child> Parent.children;
/**
* Each child has a parent
*/
private Parent Child.parent;
/**
* Parents provide access to their children
*/
public List<Children> Parent.getChildren() {
return Collections.unmodifiableList(children);
}
/**
* A child provides access to its parent
*/
public Parent Children.getParent() {
return parent;
}
/**
* ensure bi-directional navigation on adding a child
*/
public void Parent.addChild(Child child) {
if (child.parent != null) {
child.parent.removeChild(child);
}
children.add(child);
child.parent = this;
}
...
]]></programlisting>
</sect2>
</sect1>
</chapter>
|