You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

varargs.adoc 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. = Varargs
  2. [[varargs-inJava5]]
  3. == Variable-length Argument Lists in Java 5
  4. Java 5 (and hence AspectJ 5) allows you to specify methods that take a
  5. variable number of arguments of a specified type. This is achieved using
  6. an ellipsis (`...`) in the method signature as shown:
  7. [source, java]
  8. ....
  9. public void foo(int i, String... strings) {}
  10. ....
  11. A method or constructor may take at most one variable length argument,
  12. and this must always be the last declared argument in the signature.
  13. === Calling Methods and Constructors with variable-length arguments
  14. A _varargs_ method may be called with zero or more arguments in the
  15. variable argument position. For example, given the definition of `foo`
  16. above, the following calls are all legal:
  17. [source, java]
  18. ....
  19. foo(5);
  20. foo(5,"One String");
  21. foo(7,"One String","Two Strings");
  22. foo(3,"One String","Two Strings","Three Strings");
  23. ....
  24. A _varargs_ parameter is treated as an array within the defining member.
  25. So in the body of `foo` we could write for example:
  26. [source, java]
  27. ....
  28. public void foo(int i, String... strings) {
  29. String[] someStrings = strings;
  30. // rest of method body
  31. }
  32. ....
  33. One consequence of this treatment of a varargs parameter as an array is
  34. that you can also call a varargs method with an array:
  35. [source, java]
  36. ....
  37. foo(7,new String[] {"One String","Two Strings"});
  38. ....
  39. [[varargs-in-pcds]]
  40. == Using Variable-length arguments in advice and pointcut expressions
  41. AspectJ 5 allows variable-length arguments to be used for methods
  42. declared within aspects, and for inter-type declared methods and
  43. constructors, in accordance with the rules outlined in the previous
  44. section.
  45. AspectJ 5 also allows variable length arguments to be matched by
  46. pointcut expressions and bound as formals in advice.
  47. === Matching signatures based on variable length argument types
  48. Recall from the definition of signature patterns given in the chapter on
  49. annotations (xref:annotations.adoc#signaturePatterns[Signature Patterns]), that `MethodPattern` and
  50. `ConstructorPattern` are extended to allow a `varargs` pattern in the
  51. last argument position of a method or constructor signature.
  52. [source, text]
  53. ....
  54. FormalsPattern :=
  55. '..' (',' FormalsPatternAfterDotDot)? |
  56. OptionalParensTypePattern (',' FormalsPattern)* |
  57. TypePattern '...'
  58. FormalsPatternAfterDotDot :=
  59. OptionalParensTypePattern (',' FormalsPatternAfterDotDot)* |
  60. TypePattern '...'
  61. ....
  62. Method and constructor patterns are used in the `call`, `execution`,
  63. `initialization`, `preinitialization`, and `withincode` pointcut
  64. designators. Some examples of usage follow:
  65. `call(* org.xyz.*.*(int, String...))`::
  66. Matches a call join point for a call to a method defined in the
  67. `org.xyz` package, taking an `int` and a `String` _vararg_.
  68. `execution(* org.xyz.*.*(Integer...))`::
  69. Matches an execution join point for the execution of a method defined
  70. in the `org.xyz` package, taking an `Integer` _vararg_.
  71. `initialization(org.xyz.*.new((Foo || Goo)...))`::
  72. Matches the initialization join point for the construction of an
  73. object in the `org.xyz` package via a constructor taking either a
  74. variable number of `Foo` parameters or a variable number of `Goo`
  75. parameters. (This example illustrating the use of a type pattern with
  76. `...`).
  77. A variable argument parameter and an array parameter are treated as
  78. distinct signature elements, so given the method definitions:
  79. [source, java]
  80. ....
  81. void foo(String...);
  82. void bar(String[]);
  83. ....
  84. The pointcut `execution(* *.*(String...))` matches the execution join
  85. point for `foo`, but not `bar`. The pointcut
  86. `execution(* *.*(String[]))` matches the execution join point for `bar`
  87. but not `foo`.
  88. === Exposing variable-length arguments as context in pointcuts and advice
  89. When a varargs parameter is used within the body of a method, it has an
  90. array type, as discussed in the introduction to this section. We follow
  91. the same convention when binding a varargs parameter via the `args`
  92. pointcut designator. Given a method
  93. [source, java]
  94. ....
  95. public void foo(int i, String... strings) {}
  96. ....
  97. The call or execution join points for `foo` will be matched by the
  98. pointcut `args(int,String[])`. It is not permitted to use the varargs
  99. syntax within an args pointcut designator - so you _cannot_ write
  100. `args(int,String...)`.
  101. Binding of a varargs parameter in an advice statement is
  102. straightforward:
  103. [source, java]
  104. ....
  105. before(int i, String[] ss) : call(* foo(int,String...)) && args(i,ss) {
  106. // varargs String... argument is accessible in advice body through ss
  107. // ...
  108. }
  109. ....
  110. Since you cannot use the varargs syntax in the `args` pointcut
  111. designator, you also cannot use the varargs syntax to declare advice
  112. parameters.
  113. Note: the proposal in this section does not allow you to distinguish
  114. between a join point with a signature `(int, String...)` and a join point
  115. with a signature `(int, String[])` based _solely_ on the use of the `args`
  116. pointcut designator. If this distinction is required, `args` can always
  117. be coupled with `call` or `execution`.