blob: ab58d8c8a1d9df42cd8127da32d5eabe9c2aaf03 (
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
39
40
41
42
43
44
|
package moody;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
enum Mood { HAPPY, SAD, JOLLY, GRUMPY }
class AnnotationMoodImplementor { }
@Aspect
class AnnotationMoodIndicator {
public interface Moody {
Mood getMood();
void setMood(Mood mood);
}
public static class MoodyImpl implements Moody {
private Mood mood = Mood.HAPPY;
public Mood getMood() { return mood; }
public void setMood(Mood mood) { this.mood = mood; }
}
@DeclareParents(value="moody.AnnotationMoodImplementor",defaultImpl=MoodyImpl.class)
private Moody implementedInterface;
}
public class MainClass {
public static void main(String[] args) {
AnnotationMoodImplementor ami0 = new AnnotationMoodImplementor();
AnnotationMoodImplementor ami1 = new AnnotationMoodImplementor();
System.err.println("ami0's mood is " + ((AnnotationMoodIndicator.Moody) ami0).getMood());
((AnnotationMoodIndicator.Moody) ami1).setMood(Mood.JOLLY);
System.err.println("ami1's mood is now " + ((AnnotationMoodIndicator.Moody) ami1).getMood());
System.err.println("ami0's mood is still " + ((AnnotationMoodIndicator.Moody) ami0).getMood());
}
}
|