Bug 46282 - When creating obj thru intro constructor init code fails to execute
Summary: When creating obj thru intro constructor init code fails to execute
Status: RESOLVED FIXED
Alias: None
Product: AspectJ
Classification: Tools
Component: Docs (show other bugs)
Version: 1.1.1   Edit
Hardware: PC Windows XP
: P2 major (vote)
Target Milestone: 1.2   Edit
Assignee: Erik Hilsdale CLA
QA Contact:
URL:
Whiteboard:
Keywords: info
Depends on:
Blocks:
 
Reported: 2003-11-07 11:45 EST by Miguel Pessoa Monteiro CLA
Modified: 2004-01-29 10:58 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Miguel Pessoa Monteiro CLA 2003-11-07 11:45:55 EST
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.
Comment 1 George Harley CLA 2003-12-01 05:53:41 EST
Verified.
Comment 2 Jim Hugunin CLA 2004-01-12 05:13:39 EST
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.
Comment 3 Erik Hilsdale CLA 2004-01-29 10:58:41 EST
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.