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.

covariance.adoc 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. = Covariance
  2. [[covariance-inJava5]]
  3. == Covariance in Java 5
  4. Java 5 (and hence AspectJ 5) allows you to narrow the return type in an
  5. overriding method. For example:
  6. [source, java]
  7. ....
  8. class A {
  9. public A whoAreYou() {...}
  10. }
  11. class B extends A {
  12. // override A.whoAreYou *and* narrow the return type.
  13. public B whoAreYou() {...}
  14. }
  15. ....
  16. [[covariance-and-join-point-matching]]
  17. == Covariant methods and Join Point matching
  18. The join point matching rules for `call` and `execution` pointcut
  19. designators are extended to match against covariant methods.
  20. Given the classes `A` and `B` as defined in the previous section, and
  21. the program fragment
  22. [source, java]
  23. ....
  24. A a = new A();
  25. B b = new B();
  26. a.whoAreYou();
  27. b.whoAreYou();
  28. ....
  29. The signatures for the call join point `a.whoAreYou()` are simply:
  30. [source, java]
  31. ....
  32. A A.whoAreYou()
  33. ....
  34. The signatures for the call join point `b.whoAreYou()` are:
  35. [source, java]
  36. ....
  37. A A.whoAreYou()
  38. B B.whoAreYou()
  39. ....
  40. Following the join point matching rules given in xref:joinpointsignatures.adoc#jpsigs[Join Point Signatures].
  41. `call(* whoAreYou())`::
  42. Matches both calls, (since each call join point has at least one
  43. matching signature).
  44. `call(* A.whoAreYou())`::
  45. Matches both calls, (since each call join point has at least one
  46. matching signature).
  47. `call(A whoAreYou())`::
  48. Matches both calls, (since each call join point has at least one
  49. matching signature).
  50. `call(A B.whoAreYou())`::
  51. Does not match anything - neither of the call join points has a
  52. signature matched by this pattern. A lint warning is given for the
  53. call `a.whoAreYou()` ("does not match because declaring type is `A`, if
  54. match required use ``target(B)``").
  55. `call(A+ B.whoAreYou())`::
  56. Matches the call to `b.whoAreYou()` since the signature pattern
  57. matches the signature `B B.whoAreYou()`. A lint warning is given for
  58. the call `a.whoAreYou()` ("does not match because declaring type is `A`,
  59. if match required use ``target(B)``").
  60. `call(B A.whoAreYou())`::
  61. Does not match anything since neither join point has a signature
  62. matched by this pattern.
  63. `call(B whoAreYou())`::
  64. Matches the call to `b.whoAreYou()` only.
  65. `call(B B.whoAreYou())`::
  66. Matches the call to `b.whoAreYou()` only.
  67. The rule for signature matching at call and execution join points is
  68. unchanged from AspectJ 1.2: a call or execution pointcut matches if the
  69. signature pattern matches at least one of the signatures of the join
  70. point, and if the modifiers of the method or constructor are matched by
  71. any modifier pattern or annotation pattern that may be present.