Then one aspect instance will be created for each type within
<literal>org.xyz..*</literal>. For each aspect instance, the
after returning advice will match only the execution of constructors
- in the type that the aspect is an instance of. The net result is that
+ within the matched per-type-within type. The net result is that
the aspect tracks all known instances of each type within
<literal>org.xyz..*</literal>. To get access to the instances, a
programmer can simply write
<literal>InstanceTracking.aspectOf(org.xyz.SomeType.class).getInstances()</literal>.
</para>
- <para>
- A <literal>pertypewithin</literal> aspect may optionally be declared
- with a single generic type parameter. In this case, for each type
- <literal>T</literal> matched by the type pattern, the aspect instance
- created will be of type <literal>PerTypeWithinAspect<T></literal>.
- So the previous example could also be written as:
- </para>
-
- <programlisting><![CDATA[
- public aspect InstanceTracking<T> pertypewithin(org.xyz..*) {
-
- // use WeakHashMap for auto-garbage collection of keys
- private Map<T, Boolean> instances = new WeakHashMap<T, Boolean>();
-
- after(T t) returning : execution(new(..)) && this(t) {
- instances.put(t, true);
- }
-
- public Set<T> getInstances() {
- return instances.keySet();
- }
-
- }
- ]]></programlisting>
-
<para>
The <literal>pertypewithin</literal> aspect instantiation model should
be used when the implementation of a crosscutting concern requires that