From cbe1fbc633ddd98c84f9f62ed123a8b874723be7 Mon Sep 17 00:00:00 2001 From: acolyer Date: Fri, 11 Mar 2005 10:59:59 +0000 Subject: [PATCH] the "challenge Andy" mother of all dec @xxx tests. --- .../annotations/itds/AnnotationsAndITDs.aj | 183 ++++++++++++++++++ .../systemtest/ajc150/Annotations.java | 4 + .../org/aspectj/systemtest/ajc150/ajc150.xml | 38 ++++ 3 files changed, 225 insertions(+) create mode 100644 tests/java5/annotations/itds/AnnotationsAndITDs.aj diff --git a/tests/java5/annotations/itds/AnnotationsAndITDs.aj b/tests/java5/annotations/itds/AnnotationsAndITDs.aj new file mode 100644 index 000000000..2e51f3e12 --- /dev/null +++ b/tests/java5/annotations/itds/AnnotationsAndITDs.aj @@ -0,0 +1,183 @@ +import java.lang.annotation.*; +import org.aspectj.lang.reflect.MethodSignature; +import java.lang.reflect.*; + +/** + * test annotations on itds in various ways + * 1) annotated ITD + * 2) binding of an annotation on an ITD member + * 3) annotated ITD via declare @xxx + * 4) binding of annotated ITD via declare + */ +public aspect AnnotationsAndITDs { + + // annotated ITD constructors + + @SomeAnnotation(s="hello",clazz=AnnotationsAndITDs.class) + public ITDMe.new(String s) {} + + @SomeAnnotation(s="goodbye",clazz=String.class) + private ITDMe.new(int x) {} + + // annotated ITD methods + + @SomeAnnotation(s="x",clazz=Object.class) + private void ITDMe.foo() {} + + @SomeAnnotation(s="y",clazz=Integer.class) + public void ITDMe.bar(@ParamAnnotation int x) {} + + // annotated ITD fields + + @SomeAnnotation(s="d",clazz=Double.class) + public Double ITDMe.d; + + @SomeAnnotation(s="f",clazz=Double.class) + private Float ITDMe.f; + + // declare @xxx on ITD members + // ============================= + + // annotated ITD constructors + + declare @constructor : ITDMe2.new(..) : @SomeAnnotation(s="@cons",clazz=String.class); + + public ITDMe2.new(String s) {} + private ITDMe2.new(int x) {} + + // annotated ITD methods + + declare @method : * ITDMe2.*(..) : @SomeAnnotation(s="@method",clazz=ITDMe2.class); + + private void ITDMe2.foo() {} + public void ITDMe2.bar(@ParamAnnotation int x) {} + + // annotated ITD fields + + declare @field : * ITDMe2.* : @SomeAnnotation(s="@field",clazz=ITDMe2.class); + + public Double ITDMe2.d = 2d; + private Float ITDMe2.f = 1.0f; + + declare @type : ITDMe* : @SomeAnnotation(s="@type",clazz=System.class); + + + public static void main(String[] args) { + ITDMe itdme1 = new ITDMe("foo"); + ITDMe itdme2 = new ITDMe(5); + itdme1.foo(); + itdme1.bar(6); + itdme1.d = 1.0d; + itdme1.f = 1.0f; + ITDMe2 itdme21 = new ITDMe2("foo"); + ITDMe2 itdme22 = new ITDMe2(5); + itdme21.foo(); + itdme21.bar(6); + itdme21.d = 1.0d; + itdme21.f = 1.0f; + } +} + +aspect AnnotationTests { + + // static tests + + declare warning : execution(@SomeAnnotation * *(..)) : "execution(@SomeAnnotation ...)"; + + declare warning : execution(@SomeAnnotation new(..)) : "execution(@SomeAnnotation ...new(..)"; + + declare warning : set(@SomeAnnotation * *) : "set(@SomeAnnotation...)"; + + declare warning : staticinitialization(@SomeAnnotation *) : "si(@SomeAnnotation...)"; + + // binding tests + + before(SomeAnnotation sa) : execution(public ITDMe.new(String)) && @annotation(sa) { + print(sa); + } + + after(SomeAnnotation sa) : execution(private ITDMe.new(int)) && @annotation(sa) { + print(sa); + } + + before(SomeAnnotation sa) : execution(private ITDMe.new(int)) && @annotation(sa) { + print(sa); + } + + after(SomeAnnotation sa) : execution(public void ITDMe.bar(int)) && @annotation(sa) { + print(sa); + MethodSignature sig = (MethodSignature) thisJoinPoint.getSignature(); + Method meth = sig.getMethod(); + Annotation[][] anns = meth.getParameterAnnotations(); + System.out.println("method bar has " + anns.length + " params, first param annotation is " + + anns[0][0].toString()); + } + + before(SomeAnnotation sa) : set(public Double ITDMe.d) && @annotation(sa) { + print(sa); + } + + after(SomeAnnotation sa) : set(private Float ITDMe.f) && @annotation(sa) { + print(sa); + } + + after(SomeAnnotation sa) returning : staticinitialization(@SomeAnnotation *) && @annotation(sa){ + print(sa); + } + + // now repeat for the @declared versions + + before(SomeAnnotation sa) : execution(public ITDMe2.new(String)) && @annotation(sa) { + print(sa); + } + + after(SomeAnnotation sa) : execution(private ITDMe2.new(int)) && @annotation(sa) { + print(sa); + } + + before(SomeAnnotation sa) : execution(private ITDMe2.new(int)) && @annotation(sa) { + print(sa); + } + + after(SomeAnnotation sa) : execution(public void ITDMe2.bar(int)) && @annotation(sa) { + print(sa); + MethodSignature sig = (MethodSignature) thisJoinPoint.getSignature(); + Method meth = sig.getMethod(); + Annotation[][] anns = meth.getParameterAnnotations(); + System.out.println("method bar has " + anns.length + " params, first param annotation is " + + anns[0][0].toString()); + } + + before(SomeAnnotation sa) : set(public Double ITDMe2.d) && @annotation(sa) { + print(sa); + } + + after(SomeAnnotation sa) : set(private Float ITDMe2.f) && @annotation(sa) { + print(sa); + } + + private void print(SomeAnnotation sa) { + System.out.println(sa.s() + " " + sa.clazz().getName()); + } +} + +@Retention(RetentionPolicy.RUNTIME) +@interface SomeAnnotation { + String s(); + Class clazz(); +} + +@Retention(RetentionPolicy.RUNTIME) +@interface ParamAnnotation{ + String value() default ""; +} + +class ITDMe { + + +} + +class ITDMe2 { + + +} diff --git a/tests/src/org/aspectj/systemtest/ajc150/Annotations.java b/tests/src/org/aspectj/systemtest/ajc150/Annotations.java index d36426f38..3a099a2ab 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/Annotations.java +++ b/tests/src/org/aspectj/systemtest/ajc150/Annotations.java @@ -152,6 +152,10 @@ public class Annotations extends XMLBasedAjcTestCase { runTest("ajdk: dec annotation"); } + public void testAnnotationsAndITDs() { + runTest("nasty annotation and itds test"); + } + // helper methods..... public SyntheticRepository createRepos(File cpentry) { diff --git a/tests/src/org/aspectj/systemtest/ajc150/ajc150.xml b/tests/src/org/aspectj/systemtest/ajc150/ajc150.xml index 77db1aeba..666ad0d33 100644 --- a/tests/src/org/aspectj/systemtest/ajc150/ajc150.xml +++ b/tests/src/org/aspectj/systemtest/ajc150/ajc150.xml @@ -440,6 +440,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.39.5