blob: e324c6f43f7da326634ca2f4c33ec087b22b4330 (
plain)
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
|
package org.xyz.foo;
import java.util.*;
public aspect AJDKExamples pertypewithin(org.xyz..* && !AJDKExamples) {
public AJDKExamples() {
System.out.println("Aspect instance constructed");
}
// use WeakHashMap for auto-garbage collection of keys
private Map<Object,Boolean> instances = new WeakHashMap<Object,Boolean>();
after(Object o) returning() : execution(new(..)) && this(o) {
instances.put(o,true);
}
public Set<?> getInstances() {
return instances.keySet();
}
public static void main(String[] args) {
A a = new A();
A a2 = new A();
B b = new B();
B b2 = new B();
B b3 = new B();
System.out.println(AJDKExamples.hasAspect(A.class));
System.out.println(AJDKExamples.hasAspect(B.class));
Set<?> as = AJDKExamples.aspectOf(A.class).getInstances();
Set<?> bs = AJDKExamples.aspectOf(B.class).getInstances();
System.out.println("There are " + as.size() + " As");
System.out.println("There are " + bs.size() + " Bs");
}
}
class A {}
class B {}
|