Note that there are some explanatory texts on larger screens.

plurals
  1. POBehaviours of new constructor added by AspectJ ITD
    primarykey
    data
    text
    <p>I am currently applying AspectJ to our project, and I found a behavior which is a bit strange to me.</p> <p><strong>Q1:</strong> I added a new constructor to my current class with inter-type declaration, and found that the class's member variable is not initialized if the new constructor is used to instantiate my class.</p> <p>For example:</p> <p>The class which I'll add a new constructor to:</p> <pre><code>public class Child { public String name = "John"; public Child(String desc) { // TODO Auto-generated constructor stub } } </code></pre> <p>The aspectJ code:</p> <pre><code>public aspect MyTest { public Child.new(String desc, int num) { System.out.println("Child Name:" + this.name); } } </code></pre> <p>If I instantiate the Child with the new constructor:</p> <pre><code>new Child("A child", 5) </code></pre> <p>the member variable <strong>this.name</strong> is not initialized as will be done with the original constructor.</p> <p>But, if I call the original constructor:</p> <pre><code>new Child("A child") </code></pre> <p>the member variable <strong>this.name</strong> will be initialized to "John" as usual</p> <p>The result:</p> <blockquote> <p>Child Name:null</p> </blockquote> <p><strong>Is this a limitation of AspectJ? Is there anyway to resolve this issue?</strong> </p> <p>I don't really want to add the code for member variable initialization to the new constructor.</p> <p><strong>Q2:</strong> It seems <strong>in the newly added constructor</strong>, <strong>super.method()</strong> can not be correctly resolved.</p> <p>The class which I'll add a new constructor to:</p> <pre><code>public class Child extends Parent{ public String name = "John"; public Child(String desc) { } } </code></pre> <p><strong>Child</strong> extends <strong>Parent</strong>. <strong>Parent</strong> has a method <strong>init()</strong></p> <pre><code>public class Parent { public void init() { //.... } } </code></pre> <p>I add a new constructor for the <strong>Child</strong> in my aspect.</p> <pre><code>public aspect MyTest { public Child.new(String desc, int num) { super.init(); } } </code></pre> <p>The above aspect code will trigger an exception.</p> <pre><code>Exception in thread "main" java.lang.NoSuchMethodError: com.test2.Child.ajc$superDispatch$com_test2_Child$init()V at MyTest.ajc$postInterConstructor$MyTest$com_test2_Child(MyTest.aj:19) at com.test2.Child.&lt;init&gt;(Child.java:1) at MainProgram.main(MainProgram.java:11) </code></pre> <p>My workaround is to define <strong>another method</strong> for my class <strong>Child</strong>, and indirectly call the super.method() within that method</p> <p>For example, add a new method that calls <strong>super.init()</strong> for <strong>Child</strong></p> <pre><code>public void Child.initState() { super.init(); } </code></pre> <p>Now, I can call initState() in the newly added constructor like below:</p> <pre><code>public aspect MyTest { public Child.new(String desc, int num) { this.initState(); } } </code></pre> <p><strong>Is this a limitation of AspectJ? Is this the only way to resolve this issue?</strong> </p> <p>Thank you all for your time :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload