Advice on Constructors With Subclasses [message #72172] |
Tue, 02 September 2008 23:30 |
James Elliott Messages: 2 Registered: July 2009 |
Junior Member |
|
|
I am attempting to write some advice that will run only after the
construction has completely finished, i.e. after the an object has been
completely initialized.
An object can be created in any of the types in an inheritance tree, and
thus, I would like to advise all of the constructors, but only run the
advice once, after the object has been constructed.
This is probably easier to describe via an example:
public class A {
public A() {
System.out.println("init A");
}
}
public class B extends A {
public B() {
super();
System.out.println("init B");
}
}
public class C extends B {
public C() {
super();
System.out.println("init C");
}
}
after(A item) : execution(A+.new()) && target(item) {
System.out.println(thisJoinPoint.getSourceLocation());
}
public static void main(String[] args) {
C c = new C();
B b = new B();
}
The above code has the following output:
init A
A.java:3
init B
B.java:3
init C
C.java:3
init A
A.java:3
init B
B.java:3
What I am attempting to create is advice that will produce the following:
init A
init B
init C
C.java:3
init A
init B
B.java:3
i.e. Only run the advice after the object is completely created
I have tried a few approaches, including !within(A+.new()) and
!cflow(execution(A+.new()), but due to the way the compiler inlines the
super() calls, these approaches produce the same results as above.
Does anybody have any experience with this problem, and/or suggestions on
how to solve it?
|
|
|
Powered by
FUDForum. Page generated in 0.03341 seconds