Community
Participate
Working Groups
When an aspect introduces a constructor with arguments to a class that doesn't define any constructor and an instance is created using the introduced constructor initialization code outside constructors fails to execute. Test case follows: I used eclipse 3.0/AJDT 1.1.4. The weave somehow messes with the initial assignment (= 17) to Server.field1, which is done outside any constructor. Server.java: ----------------------------------------------- public class Server { private int field1 = 17; public void showState() { System.out.println("field1 is " + field1); } } Concern.java: ----------------------------------------------- public aspect Concern { private int Server.field2 = 19; public Server.new(int newval) { System.out.print("Additional intertype constructor. "); field2 = newval; } } Client.java: ----------------------------------------------- public class Client { public static void main(String[] args) { Server obj1 = new Server(); obj1.showState(); Server obj2 = new Server(23); obj2.showState(); } } ----------------------------------------------- With the aspect weaved in, the output I got was: ----------------------------------------------- field1 is 17 Additional intertype constructor. field1 is 0 ----------------------------------------------- The implicit argumentless constructor was still in place, but when Client.java makes a Server instance through the introduced constructor the initialization of field1 did not execute.
Verified.
This is considered the correct behavior in AspectJ-1.1. I'm moving this to be a doc bug because we do need to document it better. This comment in the release notes explains the reasons for the change (but it doesn't explicitly mention the issue you've found). http://dev.eclipse.org/viewcvs/indextech.cgi/~checkout~/aspectj- home/doc/README-11.html#CONSTRUCTOR_EXECUTION_IS_BIGGER I believe there's no reliable way to recover in bytecode the original initializer code for a class, so there is no way to call it from an inter-type constructor. As we get more experience with AspectJ, we're finding that inter-type constructors are very rarely useful in real programs. If you have an example of a real system where you need inter-type constructors and the current behavior is causing you problems please send it to aspectj-users for discussion.
resolved by adding a subsection to the implementation section of the programming guide. It may be that this would better be in the semantics guide, but since this is a bytecode-related issue the implementation section seemed safer.