--- /dev/null
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@interface Colored { String color();}
+
+@Colored(color="red")
+public class WithinBinding1 {
+
+ void mRed() {System.out.println("red"); }
+
+ void mBlue() {System.out.println("blue");}
+
+ void mGreen() {System.out.println("green");}
+
+ WithinBinding1() {
+ System.out.println("yellow");
+ }
+
+ public static void main(String[]argv) {
+ WithinBinding1 instance = new WithinBinding1();
+ instance.mRed();
+ instance.mBlue();
+ instance.mGreen();
+ X.verifyRun();
+ }
+}
+
+aspect X {
+
+ static int maxruns = 21; // there are 21 join points in a run of WithinBinding1.main()
+
+ static int i = 0; // Count of advice executions
+
+ before(Colored c): @within(c) {
+ System.err.println(thisJoinPoint+" color="+c.color());
+ if (!c.color().equals("red")) throw new RuntimeException("not red? "+c.color());
+ i++;
+ }
+
+ public static void verifyRun() {
+ if (X.i != maxruns)
+ throw new RuntimeException("Expected "+maxruns+" advice runs but did "+X.i);
+ }
+}
--- /dev/null
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@interface Colored { String color();}
+
+@Colored(color="red") class RedClass {
+ public void m() { System.err.println("RedClass.m() running"); }
+}
+
+@Colored(color="green") class GreenClass {
+ public GreenClass() { System.err.println("GreenClass.ctor() running"); }
+ public void m() { System.err.println("GreenClass.m() running"); }
+}
+
+class NormalClass {
+ public void m() { System.err.println("NormalClass.m() running"); }
+}
+
+
+public class WithinBinding2 {
+ public static void main(String[]argv) {
+ new RedClass().m();
+ new NormalClass().m();
+ new GreenClass().m();
+ X.verifyRun();
+ }
+}
+
+aspect X {
+
+ static int red = 0;
+ static int green=0;
+
+ before(Colored c): @within(c) {
+ System.err.println(thisJoinPoint+" color="+c.color());
+ if (c.color().startsWith("r")) red++;
+ else if (c.color().startsWith("g")) green++;
+ else throw new RuntimeException("Didn't expect this color: "+c.color());
+ }
+
+ public static void verifyRun() {
+ System.err.println(red+" "+green);
+ if (red!=7) throw new RuntimeException("Expected 7 red join points but got "+red);
+ if (green!=9) throw new RuntimeException("Expected 9 green join points but got "+green);
+ }
+}
--- /dev/null
+import java.lang.annotation.*;
+
+@Retention(RetentionPolicy.RUNTIME)
+@interface Colored { String color();}
+
+public class WithinCodeBinding1 {
+
+ @Colored(color="red") void mRed() {System.out.println("red"); }
+
+ @Colored(color="blue") void mBlue() {System.out.println("blue");}
+
+ @Colored(color="green") void mGreen() {System.out.println("green");}
+
+ @Colored(color="yellow") WithinCodeBinding1() {
+ System.out.println("yellow");
+ }
+
+ public static void main(String[]argv) {
+ WithinCodeBinding1 instance = new WithinCodeBinding1();
+ instance.mRed();
+ instance.mBlue();
+ instance.mGreen();
+ X.verifyRun();
+ }
+}
+
+aspect X {
+
+ // Expected color order
+ static String exp[] = new String[]{"yellow","red","blue","green"};
+
+ static int i = 0; // Count of advice executions
+
+ before(Colored c): @withincode(c) && call(* println(..)) {
+ System.err.println(thisJoinPoint+" color="+c.color());
+ if (!c.color().equals(exp[i])) throw new RuntimeException("not "+exp[i]+"? "+c.color());
+ i++;
+ }
+
+ public static void verifyRun() {
+ if (X.i != exp.length)
+ throw new RuntimeException("Expected "+exp.length+" advice runs but did "+X.i);
+ }
+}
runTest("handler and @annotation");
}
+ ///////////////////////////////////// @WITHIN
+
+ // '@within()'
+ public void testWithinBinding1() {
+ runTest("@within");
+ }
+
+ //'@within()' but multiple types around (some annotated)
+ public void testWithinBinding2() {
+ runTest("@within - multiple types");
+ System.err.println(getLastRunResult().getStdErr());
+ }
+
+ ///////////////////////////////////// @WITHINCODE
+
+ // '@withincode() && call(* println(..))'
+ public void testWithinCodeBinding1() {
+ runTest("@withincode() and call(* println(..))");
+ }
+
///////////////////////////////////// @ANNOTATION complex tests
<compile files="HandlerBinding.aj" options="-1.5"/>
<run class="HandlerBinding"/>
</ajc-test>
+
+ <ajc-test dir="java5/annotations/binding" vm="1.5" title="@withincode() and call(* println(..))">
+ <compile files="WithinCodeBinding1.aj" options="-1.5"/>
+ <run class="WithinCodeBinding1"/>
+ </ajc-test>
+
+ <ajc-test dir="java5/annotations/binding" vm="1.5" title="@within">
+ <compile files="WithinBinding1.aj" options="-1.5"/>
+ <run class="WithinBinding1"/>
+ </ajc-test>
+
+ <ajc-test dir="java5/annotations/binding" vm="1.5" title="@within - multiple types">
+ <compile files="WithinBinding2.aj" options="-1.5"/>
+ <run class="WithinBinding2"/>
+ </ajc-test>
<ajc-test dir="java5/annotations/binding/complexExample" vm="1.5" title="packages and no binding">
<compile files="A.java,B.java,Color.java,X.java" options="-1.5"/>
<message kind="weave" text="Type 'SimpleVarargs' (SimpleVarargs.java:27) advised by before advice from 'VarargsAspect06' (VarargsAspect06.aj:3)"/>
</compile>
</ajc-test>
+
+
</suite>