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.

pertypewithin.adoc 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [[pertypewithin]]
  2. = The `pertypewithin` Aspect Instantiation Model
  3. AspectJ 5 defines a new per-clause type for aspect instantiation:
  4. `pertypewithin`. Unlike the other per-clauses, `pertypewithin` takes a
  5. type pattern:
  6. [source, text]
  7. ....
  8. PerTypeWithin := 'pertypewithin' '(' OptionalParensTypePattern ')'
  9. ....
  10. When an aspect is declared using the `pertypewithin` instantiation
  11. model, one new aspect instance will be created for each type matched by
  12. the associated type pattern.
  13. Pertypewithin aspects have `aspectOf` and `hasAspect` methods with the
  14. following signatures:
  15. [source, java]
  16. ....
  17. /**
  18. * return true if this aspect has an instance associated with
  19. * the given type.
  20. */
  21. public static boolean hasAspect(Class clazz)
  22. /**
  23. * return the instance associated with the given type.
  24. * Throws NoAspectBoundException if there is no such
  25. * aspect.
  26. */
  27. public static P aspectOf(Class clazz)
  28. ....
  29. Where `P` is the type of the `pertypewithin` aspect.
  30. In addition, `pertypewithin` aspects have a `getWithinTypeName` method
  31. that can be called to return the package qualified name of the type for
  32. which the aspect instance has been created.
  33. [source, java]
  34. ....
  35. /**
  36. * return the package qualified name (eg. com.foo.MyClass) of the type
  37. * for which the aspect instance has been instantiated.
  38. */
  39. public String getWithinTypeName()
  40. ....
  41. In common with the other per-clause instantiation models, the execution
  42. of any advice declared within a `pertypewithin` aspect is conditional
  43. upon an implicit pointcut condition. In this case, that any join point
  44. be `within` the type that the executing aspect is an `aspectOf`. For
  45. example, given the aspect definition
  46. [source, java]
  47. ....
  48. import java.util.*;
  49. public aspect InstanceTracking pertypewithin(org.xyz..*) {
  50. // use WeakHashMap for auto-garbage collection of keys
  51. private Map<Object,Boolean> instances = new WeakHashMap<Object,Boolean>();
  52. after(Object o) returning() : execution(new(..)) && this(o) {
  53. instances.put(o,true);
  54. }
  55. public Set<?> getInstances() {
  56. return instances.keySet();
  57. }
  58. }
  59. ....
  60. Then one aspect instance will be created for each type within
  61. `org.xyz..*`. For each aspect instance, the after returning advice will
  62. match only the execution of constructors within the matched
  63. per-type-within type. The net result is that the aspect tracks all known
  64. instances of each type within `org.xyz..*`. To get access to the
  65. instances, a programmer can simply write
  66. `InstanceTracking.aspectOf(org.xyz.SomeType.class).getInstances()`.
  67. The `pertypewithin` aspect instantiation model should be used when the
  68. implementation of a crosscutting concern requires that some state be
  69. maintained for each type in a set of types. To maintain state for a
  70. single type, it is easier to use a static inter-type declared field.
  71. Examples of usage include instance tracking, profiling, and the
  72. implementation of a common tracing idiom that uses one Logger per traced
  73. class.