summaryrefslogtreecommitdiffstats
path: root/tests/java5/annotations/ajdkExamples/DeclareAnnotation.aj
blob: b018604c57bf923ccbaad0ed8764721d1177a447 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.lang.annotation.*;

public aspect DeclareAnnotation {
	
	declare @type : org.xyz.model..* : @BusinessDomain;
	
	declare @method : public * BankAccount+.*(..) : @Secured(role="supervisor");
	
	declare @field : * DAO+.* : @Persisted;
	
	declare @constructor : BankAccount+.new(..) : @Secured(role="supervisor");
	
	declare warning : staticinitialization(@BusinessDomain *)
	                : "@BusinessDomain";
	
	declare warning : execution(@Secured * *(..)) : "@Secured";
	
	declare warning : set(@Persisted * *) : "@Persisted";
	
	declare warning : initialization(@Secured *.new(..)) : "@Secured";
	
	public static void main(String[] args) throws Exception {
		Class bAcc = BankAccount.class;
		java.lang.reflect.Method credit = bAcc.getDeclaredMethod("credit");
		Secured secured = credit.getAnnotation(Secured.class);
		if (!secured.role().equals("supervisor")) {
			throw new RuntimeException("BankAccount.credit should have @Secured(role=supervisor) annotation");
		}
	}
}

@interface BusinessDomain {}

@Retention(RetentionPolicy.RUNTIME)
@interface Secured {
	String role() default "";
}

@interface Persisted {}

class BankAccount {
	
	public void credit() {}
	public void debit() {}
	protected void transfer() {}
	
}

class ExecutiveBankAccount extends BankAccount {
	
	public ExecutiveBankAccount() {
		super();
	}
	
	public void interest() {}
	protected void commission() {}
	
}

class DAO {
	
	int x = 5;
	
}

class SubDAO extends DAO {
	
	int y = 6;
	
}