I modified the aspect and it does what I think you want, but requires that you handle the control flow yourself. ÂI think that is necessary because the method you are advising is 'equals()' which is likely to be used internally to the cflow implementation (although I haven't checked).
aspect A {
Âpointcut inTest1(Test1 i): within(Test1) && execution(boolean Test1.equals(Object)) && target(i);
ÂStack<Test1> t1Stack = new Stack<Test1>();
Â
Âboolean around(Test1 t): inTest1(t) {
Âfor (int i=0;i<t1Stack.size();i++) {
Âif (t1Stack.get(i)==t) {
Âreturn true;
Â}
Â}
Ât1Stack.push(t);
Âboolean b = proceed(t);
Ât1Stack.pop();
Âreturn b;
Â}
Â
Âpointcut inTest2(Test2 i): within(Test2) && execution(boolean Test2.equals(Object)) && target(i);
ÂStack<Test2> t2Stack = new Stack<Test2>();
Â
Âboolean around(Test2 t): inTest2(t) {
Âfor (int i=0;i<t2Stack.size();i++) {
Âif (t2Stack.get(i)==t) {
Âreturn true;
Â}
Â}
Ât2Stack.push(t);
Âboolean b = proceed(t);
Ât2Stack.pop();
Âreturn b;
Â}
}
run:
3
4
1
2
4
1
2
3
haha, no more cycles!
2009/8/6 Neville Grech
<nevillegrech@xxxxxxxxx>
Hi, I'm trying out the following simple example whereby I have two classes, which contain an object of the other class's type in each. I then proceed to construct four objects in such a way that a cycle is created. When I call the equals method, I should get an infinite cycle. I tried to create a pointcut descriptor that detects this. Unfortunately, this only works when the nesting level in the call stack is less than 4. I need to create a PCD which detects cycles when the same equals method in the same object is called in the same call stack. Here is the code (which doesn't work as it should):
import java.util.*;
class Launcher {
ÂÂÂ public static void main(String[] args) {
ÂÂÂÂÂÂÂ Test1 t1=new Test1(1);
ÂÂÂÂÂÂÂ Test2 t2=new Test2(2);
ÂÂÂÂÂÂÂ Test1 t3=new Test1(3);
ÂÂÂÂÂÂÂ Test2 t4=new Test2(4);
ÂÂÂÂÂÂÂ t1.myObject=t2;
ÂÂÂÂÂÂÂ t2.myObject=t3;
ÂÂÂÂÂÂÂ t3.myObject=t4;
ÂÂÂÂÂÂÂ t4.myObject=t1;
ÂÂÂÂÂÂÂ assert t3.equals(t1);
ÂÂÂÂÂÂÂ System.out.println("haha, no more cycles!");
ÂÂÂ }
}
class Test1 {
ÂÂÂ public Test1(int c) { i=c; }
ÂÂÂ int i;
ÂÂÂ Test2 myObject;
ÂÂÂ public boolean equals(Object o) {
ÂÂÂÂÂÂÂ System.out.println(i);
ÂÂÂÂÂÂÂ return myObject.equals(((Test1)o).myObject);
ÂÂÂ }
}
class Test2 {
ÂÂÂ public Test2(int c) { i=c; }
ÂÂÂ int i;
ÂÂÂ Test1 myObject;
ÂÂÂ
ÂÂÂ public boolean equals(Object o) {
ÂÂÂÂÂÂÂ System.out.println(i);
ÂÂÂÂÂÂÂ return myObject.equals(((Test2)o).myObject);
ÂÂÂ }
}
aspect A {
ÂÂÂ pointcut inTest1(Test1 i): call(boolean Test1.equals(Object)) && target(i);
ÂÂÂ pointcut inTest2(Test2 i): call(boolean Test2.equals(Object)) && target(i);
ÂÂÂ boolean around(Test1 up, Test1 now):
ÂÂÂÂÂÂÂ inTest1(now) && cflowbelow(inTest1(up)) && if(up==now){
ÂÂÂÂÂÂÂ return true;
ÂÂÂ }
ÂÂÂ boolean around(Test2 up, Test2 now):
ÂÂÂÂÂÂÂ if(up==now) && cflowbelow(inTest2(up)) && inTest2(now) {
ÂÂÂÂÂÂÂ return true;
ÂÂÂ }
}
--
Regards,
Neville Grech
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users