Note that there are some explanatory texts on larger screens.

plurals
  1. POaspectj pointcut and advice for single call following construction of an instance
    primarykey
    data
    text
    <p>Based on my own experimentation and the documentation here: <a href="http://www.eclipse.org/aspectj/doc/released/faq.html#q:initializationjoinpoints" rel="nofollow">http://www.eclipse.org/aspectj/doc/released/faq.html#q:initializationjoinpoints</a></p> <p>This:</p> <pre><code>public class Init { public static void main (String[] args) { new Test(); } } @Inherited @interface MyAnnotation {} @MyAnnotation class Super {} class Test extends Super { Test() {} } </code></pre> <p>Will result in this:</p> <pre><code> &lt;constructor-call sig="Test()" &gt; &lt;staticinitialization sig="Super._init_" /&gt; &lt;staticinitialization sig="Test._init_" /&gt; &lt;initialization sig="Super()" &gt; &lt;instanceinitializer-execution sig="Super._init_" /&gt; &lt;constructor-execution sig="Super()" /&gt; &lt;/initialization&gt; &lt;initialization sig="Test()" &gt; &lt;instanceinitializer-execution sig="Test._init_" /&gt; &lt;constructor-execution sig="Test()" /&gt; &lt;/initialization&gt; &lt;/constructor-call&gt; </code></pre> <p>I am trying to create an advice based on a pointcut so that I can perform action on an instance after it has been completely constructed, regardless whether it is of type Super or of type Test.</p> <p>I have tried this:</p> <pre><code>pointcut initializedCall(): initialization((@MyAnnotation *).new(..)); after(Super s) returning: this(s) &amp;&amp; initializedCall() { System.out.println("after initialization via aspect"); } </code></pre> <p>but it gets called twice when creating an instance of Test. Once after Super is initialized, and then again after Test is initialized.</p> <p>I have tried this:</p> <pre><code>pointcut constructorExecution(): execution((@MyAnnotation *).new(..)); after(Super s) returning : target(s) &amp;&amp; constructorExecution() { System.out.println("after constructor execution via aspect"); } </code></pre> <p>And it too gets called twice which creating an instance of Test, same as above.</p> <p>Finally, I have tried this:</p> <pre><code>pointcut constructorCall(): call((@AutowiredClass *).new(..)); after(): constructorCall() { System.out.println("after constructor call via aspect"); } </code></pre> <p>Which works great, as it only gets called once, but I can't use target() on it to get a reference to the newly created instance, just as described <a href="http://www.eclipse.org/aspectj/doc/released/faq.html#q:adviseconstructors" rel="nofollow">here</a>.</p> <p>Is there any way to execute at the same time as the last, but also get a reference to the instance? Or is there a way to tell if the aspect is going to fire again so I can wait until the last time?</p> <p>Thanks.</p> <p><strong>UPDATE:</strong> I also need this to work with reflection, as I am serializing and deserializing the classes to/from json and want to ensure code gets executed appropriately following construction during deserialization.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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