diff options
author | acolyer <acolyer> | 2005-03-09 14:10:39 +0000 |
---|---|---|
committer | acolyer <acolyer> | 2005-03-09 14:10:39 +0000 |
commit | 82fa47384f23d3ed0cf20b531fff947182b08a84 (patch) | |
tree | f984dac2428b08762c4a6d859e8e321f47fb19c8 /tests/java5 | |
parent | 69845b3545539b7961e8b4a3ef4a5ac416a305bb (diff) | |
download | aspectj-82fa47384f23d3ed0cf20b531fff947182b08a84.tar.gz aspectj-82fa47384f23d3ed0cf20b531fff947182b08a84.zip |
completing the set of AJDK examples coded up as test cases
Diffstat (limited to 'tests/java5')
-rw-r--r-- | tests/java5/annotations/ajdkExamples/DeclareAnnotation.aj | 62 | ||||
-rw-r--r-- | tests/java5/annotations/ajdkExamples/DeclaresWithAnnotations.aj | 48 | ||||
-rw-r--r-- | tests/java5/annotations/ajdkExamples/DecpAnnotations.aj | 48 | ||||
-rw-r--r-- | tests/java5/annotations/ajdkExamples/PrecedenceAnnotations.aj | 45 | ||||
-rw-r--r-- | tests/java5/annotations/ajdkExamples/org/xyz/model/Model.java | 7 | ||||
-rw-r--r-- | tests/java5/covariance/ajdk/AJDKExamples.aj | 47 | ||||
-rw-r--r-- | tests/java5/pertypewithin/ajdk/AJDKExamples.aj | 34 | ||||
-rw-r--r-- | tests/java5/varargs/ajdk/AJDKExamples.aj | 31 | ||||
-rw-r--r-- | tests/java5/varargs/ajdk/org/xyz/Foo.java | 5 | ||||
-rw-r--r-- | tests/java5/varargs/ajdk/org/xyz/Goo.java | 5 | ||||
-rw-r--r-- | tests/java5/varargs/ajdk/org/xyz/Hoo.java | 16 |
11 files changed, 348 insertions, 0 deletions
diff --git a/tests/java5/annotations/ajdkExamples/DeclareAnnotation.aj b/tests/java5/annotations/ajdkExamples/DeclareAnnotation.aj new file mode 100644 index 000000000..9ad605226 --- /dev/null +++ b/tests/java5/annotations/ajdkExamples/DeclareAnnotation.aj @@ -0,0 +1,62 @@ +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 warning : staticinitialization(@BusinessDomain *) + : "@BusinessDomain"; + + declare warning : execution(@Secured * *(..)) : "@Secured"; + + declare warning : set(@Persisted * *) : "@Persisted"; + + 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 void interest() {} + protected void commission() {} + +} + +class DAO { + + int x = 5; + +} + +class SubDAO extends DAO { + + int y = 6; + +}
\ No newline at end of file diff --git a/tests/java5/annotations/ajdkExamples/DeclaresWithAnnotations.aj b/tests/java5/annotations/ajdkExamples/DeclaresWithAnnotations.aj new file mode 100644 index 000000000..029ebafa1 --- /dev/null +++ b/tests/java5/annotations/ajdkExamples/DeclaresWithAnnotations.aj @@ -0,0 +1,48 @@ +import org.xyz.model.Model; + +public aspect DeclaresWithAnnotations { + + declare warning : withincode(@PerformanceCritical * *(..)) && + call(@ExpensiveOperation * *(..)) + : "Expensive operation called from within performance critical section"; + + declare error : call(* org.xyz.model.*.*(..)) && + !@within(Trusted) + : "Untrusted code should not call the model classes directly"; + +} + +@interface PerformanceCritical {} + +@interface ExpensiveOperation {} + +@interface Trusted {} + + +class Foo { + + @PerformanceCritical Foo getFoo() { + Model m = new Model(); + m.foo(); // DE + Foo foo = makeFoo(); // DW + return foo; + } + + Foo getFoo2() { + Foo foo = makeFoo(); + return foo; + } + + @ExpensiveOperation Foo makeFoo() { + return new Foo(); + } +} + +@Trusted class Goo { + + public void goo() { + Model m = new Model(); + m.foo(); + } + +}
\ No newline at end of file diff --git a/tests/java5/annotations/ajdkExamples/DecpAnnotations.aj b/tests/java5/annotations/ajdkExamples/DecpAnnotations.aj new file mode 100644 index 000000000..d3cd3fd42 --- /dev/null +++ b/tests/java5/annotations/ajdkExamples/DecpAnnotations.aj @@ -0,0 +1,48 @@ +public aspect DecpAnnotations { + + public String SecuredObject.getSecurityCredentials() { + return "none"; + } + + declare parents : (@Secured *) implements SecuredObject; + + declare parents : (@Secured BankAccount+) implements SecuredObject; + + public static void main(String[] args) { + Foo foo = new Foo(); + Goo goo = new Goo(); + BankAccount acc = new BankAccount(); + PrivateBankAccount pacc = new PrivateBankAccount(); + BusinessBankAccount bacc = new BusinessBankAccount(); + + System.out.println("Test Foo is not secured: " + + ((foo instanceof SecuredObject) ? "FAIL" : "PASS") + ); + System.out.println("Test Goo is secured: " + + ((goo instanceof SecuredObject) ? "PASS" : "FAIL") + ); + System.out.println("goo credentials: " + goo.getSecurityCredentials()); + System.out.println("Test BankAccount is not secured: " + + ((acc instanceof SecuredObject) ? "FAIL" : "PASS") + ); + System.out.println("Test PrivateBankAccount is not secured: " + + ((pacc instanceof SecuredObject) ? "FAIL" : "PASS") + ); + System.out.println("Test BusinessBankAccount is secured: " + + ((bacc instanceof SecuredObject) ? "PASS" : "FAIL") + ); + } +} + + +interface SecuredObject {} +@interface Secured {} + +class Foo {} +@Secured class Goo{} + +class BankAccount {} + +class PrivateBankAccount extends BankAccount {} + +@Secured class BusinessBankAccount extends BankAccount {}
\ No newline at end of file diff --git a/tests/java5/annotations/ajdkExamples/PrecedenceAnnotations.aj b/tests/java5/annotations/ajdkExamples/PrecedenceAnnotations.aj new file mode 100644 index 000000000..63218f7df --- /dev/null +++ b/tests/java5/annotations/ajdkExamples/PrecedenceAnnotations.aj @@ -0,0 +1,45 @@ +public aspect PrecedenceAnnotations { + + declare precedence : (@Security *), *; + + declare precedence : *, (@Performance *); + + public static void main(String[] args) { + A a = new A(); + a.foo(); + } +} + +@interface Security {} +@interface Performance{} + +class A { + pointcut foo() : execution(* foo()); + void foo() {} +} + +aspect S1 { + before() : A.foo() { + System.out.println("S1"); + } +} + +@Security aspect S2 { + + before() : A.foo() { + System.out.println("@Security S2"); + } + +} + +aspect P1 { + after() returning : A.foo() { + System.out.println("P1"); + } +} + +@Performance aspect P2 { + after() returning : A.foo() { + System.out.println("@Performance P2"); + } +}
\ No newline at end of file diff --git a/tests/java5/annotations/ajdkExamples/org/xyz/model/Model.java b/tests/java5/annotations/ajdkExamples/org/xyz/model/Model.java new file mode 100644 index 000000000..2829ccd61 --- /dev/null +++ b/tests/java5/annotations/ajdkExamples/org/xyz/model/Model.java @@ -0,0 +1,7 @@ +package org.xyz.model; + +public class Model { + + public void foo() {} + +}
\ No newline at end of file diff --git a/tests/java5/covariance/ajdk/AJDKExamples.aj b/tests/java5/covariance/ajdk/AJDKExamples.aj new file mode 100644 index 000000000..e7cb02383 --- /dev/null +++ b/tests/java5/covariance/ajdk/AJDKExamples.aj @@ -0,0 +1,47 @@ +public aspect AJDKExamples { + + declare warning : call(* whoAreYou()) + : "call(* whoAreYou())"; + + declare warning : call(* A.whoAreYou()) + : "call(* A.whoAreYou())"; + + declare warning : call(A whoAreYou()) + : "call(A whoAreYou())"; + + declare warning : call(A B.whoAreYou()) + : "call(A B.whoAreYou())"; + + declare warning : call(A+ B.whoAreYou()) + : "call(A+ B.whoAreYou())"; + + declare warning : call(B A.whoAreYou()) + : "call(B A.whoAreYou())"; + + declare warning : call(B whoAreYou()) + : "call(B whoAreYou())"; + + declare warning : call(B B.whoAreYou()) + : "call(B B.whoAreYou())"; + +} + +class A { + public A whoAreYou() { return this; } +} + +class B extends A { + // override A.whoAreYou *and* narrow the return type. + public B whoAreYou() { return this; } +} + +class C { + + public C() { + A a = new A(); + B b = new B(); + a.whoAreYou(); + b.whoAreYou(); + } + +}
\ No newline at end of file diff --git a/tests/java5/pertypewithin/ajdk/AJDKExamples.aj b/tests/java5/pertypewithin/ajdk/AJDKExamples.aj new file mode 100644 index 000000000..433270cf4 --- /dev/null +++ b/tests/java5/pertypewithin/ajdk/AJDKExamples.aj @@ -0,0 +1,34 @@ +import java.util.*; +public aspect AJDKExamples pertypewithin(org.xyz..*) { + + // 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 {}
\ No newline at end of file diff --git a/tests/java5/varargs/ajdk/AJDKExamples.aj b/tests/java5/varargs/ajdk/AJDKExamples.aj new file mode 100644 index 000000000..422b10ede --- /dev/null +++ b/tests/java5/varargs/ajdk/AJDKExamples.aj @@ -0,0 +1,31 @@ +public aspect AJDKExamples { + + declare warning : call(* org.xyz.*.*(int, String...)) : "call vararg match"; + + declare warning : execution(* org.xyz.*.*(Integer...)) : "execution vararg match"; + + declare warning : initialization(org.xyz.*.new((Foo || Goo)...)) : "init vararg match"; + + declare warning : execution(* *.*(String...)) : "single vararg"; + + declare warning : execution(* *.*(String[])) : "single String[]"; + + before(int i, String[] ss) : call(* foo(int,String...)) && args(i,ss) { + System.out.println("Matched at " + thisJoinPoint); + } + + public static void main(String[] args) { + X foo = new X(); + foo.foo(5,"hello"); + foo.bar(5,new String[]{"hello"}); + } + +} + +class X { + public void foo(String... ss) {} + public void bar(String[] ss) {} + public void foo(int i,String... ss) {} + public void bar(int i,String[] ss) {} +} + diff --git a/tests/java5/varargs/ajdk/org/xyz/Foo.java b/tests/java5/varargs/ajdk/org/xyz/Foo.java new file mode 100644 index 000000000..4b10d0af8 --- /dev/null +++ b/tests/java5/varargs/ajdk/org/xyz/Foo.java @@ -0,0 +1,5 @@ +package org.xyz; + +public class Foo { + +} diff --git a/tests/java5/varargs/ajdk/org/xyz/Goo.java b/tests/java5/varargs/ajdk/org/xyz/Goo.java new file mode 100644 index 000000000..0c615ebf8 --- /dev/null +++ b/tests/java5/varargs/ajdk/org/xyz/Goo.java @@ -0,0 +1,5 @@ +package org.xyz; + +public class Goo { + +} diff --git a/tests/java5/varargs/ajdk/org/xyz/Hoo.java b/tests/java5/varargs/ajdk/org/xyz/Hoo.java new file mode 100644 index 000000000..7ce1c1d73 --- /dev/null +++ b/tests/java5/varargs/ajdk/org/xyz/Hoo.java @@ -0,0 +1,16 @@ +package org.xyz; + +public class Hoo { + + public Hoo(Foo... foos) {} + public Hoo(Goo... goos) {} + public Hoo(String... ss) { + intStringVar(5,ss); + intString(5,ss[0]); + } + + void intStringVar(int i, String... ss) {} + void intString(int i, String s) {} + void integerVar(Integer... is) {} + +}
\ No newline at end of file |