Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Constructor advice & problem with inlining?


On Sep 5, 2008, at 6:55 PM, Andy Clement wrote:

--_004_689d61aa0809050955l3574efb4ia5163335a6e02f64mailgmailco_
Content-Type: multipart/alternative;
boundary ="_000_689d61aa0809050955l3574efb4ia5163335a6e02f64mailgmailco_"

--_000_689d61aa0809050955l3574efb4ia5163335a6e02f64mailgmailco_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hello,

"pre-initialization is defined to encompass the period from the entry of th=
e first-called constructor to the call to the super constructor."

I believe 'first-called constructor' is why you don't see double preinits.

Is there a way to obtain the behaviour I want with other pointcuts?
In a quick few mins can't think of a way, maybe someone else on the list ca=
n.


Hi,
Is it possible to capture 'call' to constructors only inside the 'execution' of constructors? Probably with that, I could capture separately the invocations to this/super, so that I do not need to use pre-init?
  How could I express such a pointcut?

Thanks,

Alex




cheers,
Andy.

2008/9/4 Alex Villazon <alex.villazon@xxxxxxxxxxx<mailto:alex.villazon@lu.u=
nisi.ch>>
Hi,

I would like to execute some code at the very beginning and the end of ev= ery constructor... I'm using preinitialization and after returning as foll=
ows.

public aspect Foo {
pointcut allExecsNews() : execution(*.new(..)) && ! within(Foo) ;
   pointcut preInit() : preinitialization(*.new(..)) && !within(Foo);

  before() : preInit() {
    System.out.println("preinit " + thisJoinPoint.getSignature());
  }

  after() returning() : allExecsNews()  {
System.out.println("after returning " + thisJoinPoint.getSignature());
 }
}

applied to the following class

public class Reference {

   Object a;
   Object b;

  Reference(Object a) {
    this(a, null);
    System.out.println("Ref");
  }

  Reference(Object a, Object  b) {
       this.a =3D a;
       this.b =3D b;
       System.out.println("Ref 2");
  }

  public static void main(String[] args) {
     Reference r =3D new Reference(null);
 }
}

  I was expecting to have the following output:

preinit Reference(Object)
preinit Reference(Object, Object)
Ref 2
after returning Reference(Object, Object)
Ref
after returning Reference(Object)

 However I obtain:

preinit Reference(Object)
Ref 2
after returning Reference(Object, Object)
Ref
after returning Reference(Object)

i.e. the invocation to preinit Reference(Object, Object) is missing... ev=
en though the pointcut is correctly captured by the aspect... !!!

Join point 'constructor-execution(void Reference.<init>(java.lang.Object))'= in Type 'Reference' (Reference.java:8) advised by afterReturning advice fr=
om 'Foo' (Foo.java:10)

Join point 'constructor-execution(void Reference.<init>(java.lang.Object, j= ava.lang.Object))' in Type 'Reference' (Reference.java:14) advised by after=
Returning advice from 'Foo' (Foo.java:10)

Join point 'preinitialization(void Reference.<init>(java.lang.Object))' in = Type 'Reference' (Reference.java:7) advised by before advice from 'Foo' (Fo=
o.java:6)

Join point 'preinitialization(void Reference.<init>(java.lang.Object, java.= lang.Object))' in Type 'Reference' (Reference.java:13) advised by before ad=
vice from 'Foo' (Foo.java:6)

By checking the generated bytecode, I realized that the body of the Refer= ence(Object, Object) constructor was inlined directly in Reference(Object).= . i.e. there is no longer the invocation to Reference(Object, Object) as in=
the original constructor..

 The woven Reference(Object) has therefore:
   - invocation to before preinit advice
   - copy of the Reference(Object, Object) body  <=3D=3D=3D ???
- invocation to after (corresponding to Reference(Object,Object)) !!!
   - invocation to after (correspoinding to Reference(Object)

 The woven Reference(Object, Object) has:
   - invocation to before preinit advice !!!
   - body...
   - invocation to after (corresponding to Reference(Object, Object)

Is this because I'm using after returning on the execution(*.new(..)) poi=
ntcut? or did I missed something?
If the constructor is inlined, why it doesn't inline also the invocation= to before preinitalization as well? Is there a way to obtain the behaviour= I want with other pointcuts? Is it because the object is not totally init=
ialized?

Many thanks in advance.

Alex








_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx<mailto:aspectj-users@xxxxxxxxxxx>
https://dev.eclipse.org/mailman/listinfo/aspectj-users



--_000_689d61aa0809050955l3574efb4ia5163335a6e02f64mailgmailco_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><span class=3D"Apple-style-span" style=3D"border-coll= apse: collapse;">Hello,</span></div><div><span class=3D"Apple-style- span" s= tyle=3D"border-collapse: collapse;"><br></span></div><div><span class=3D"Ap= ple-style-span" style=3D"border-collapse: collapse;">&quot;pre- initializati= on is defined to encompass the period from the entry of the first- called co=
nstructor to the call to the super constructor.&quot;</span></div>
<div><span class=3D"Apple-style-span" style=3D"border-collapse: collapse;">= <br></span></div><div><span class=3D"Apple-style-span" style=3D"border-coll= apse: collapse;">I believe &#39;first-called constructor&#39; is why you do=
n&#39;t see double preinits.</span></div>
<div><span class=3D"Apple-style-span" style=3D"border-collapse: collapse;">= <br></span></div><div><span class=3D"Apple-style-span" style=3D"border-coll= apse: collapse;">&gt; Is there a way to obtain the behaviour I want with ot=
her pointcuts?&nbsp;<br>
</span></div><div><span class=3D"Apple-style-span" style=3D"border- collapse= : collapse;">In a quick few mins can&#39;t think of a way, maybe someone el= se on the list can.</span></div><div><span class=3D"Apple-style- span" style=
=3D"border-collapse: collapse;"><br>
</span></div><div><span class=3D"Apple-style-span" style=3D"border- collapse= : collapse;">cheers,</span></div><div><span class=3D"Apple-style- span" styl= e=3D"border-collapse: collapse;">Andy.</span></div><div><div><span class=3D=
"Apple-style-span" style=3D"border-collapse: collapse;"><br>
</span></div><div class=3D"gmail_quote">2008/9/4 Alex Villazon <span dir=3D= "ltr">&lt;<a href=3D"mailto:alex.villazon@xxxxxxxxxxx";>alex.villazon@xxxxxx = si.ch</a>&gt;</span><br><blockquote class=3D"gmail_quote" style=3D"margin:0=
0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div style=3D"word-wrap:break-word">Hi,<div><br></ div><div>&nbsp;&nbsp;I wo= uld like to execute some code at the very beginning and the end of every co= nstructor... &nbsp;I&#39;m using preinitialization and after returning as f=
ollows.</div>
<div><br></div><div>public aspect Foo {</div><div>&nbsp;&nbsp; &nbsp;&nbsp;= <span style=3D"border-collapse:collapse;font-family:arial;font-size: 13px">&= nbsp;pointcut allExecsNews() :&nbsp; execution(*.new(..))&nbsp; &amp;&amp; = !within(Foo) ;&nbsp;</span></div><span style=3D"border- collapse:collapse;fo= nt-family:arial;font-size:13px">&nbsp;&nbsp;&nbsp; pointcut preInit() : pre=
initialization(*.new(..)) &amp;&amp; !within(Foo);<br>
<br>&nbsp;&nbsp; before() : preInit() {<br>&nbsp;&nbsp;&nbsp;&nbsp; System.= out.println(&quot;preinit &quot; + thisJoinPoint.getSignature());<br>&nbsp;= &nbsp; }<br><br>&nbsp;&nbsp; after() returning() : allExecsNews()&nbsp; {<b= r>&nbsp;&nbsp;&nbsp; System.out.println(&quot;after returning &quot; + this=
JoinPoint.getSignature());<br>
&nbsp; }<br></span><div>}</div><div><br></div><div>applied to the following= class</div><div><br></div><div><span style=3D"border- collapse:collapse;fon= t-family:arial;font-size:13px">public class Reference {<br><br>&nbsp;&nbsp;= &nbsp; Object a;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br> &nbsp;&nbsp;&nbsp; Object b;<br><br>&nbsp;&nbsp; Reference(Object a) {<br>&= nbsp;&nbsp;&nbsp;&nbsp; this(a, null);<br>&nbsp;&nbsp;&nbsp;&nbsp; System.o= ut.println(&quot;Ref&quot;);<br>&nbsp;&nbsp; }<br><br>&nbsp;&nbsp; Referenc= e(Object a, Object&nbsp; b) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= this.a =3D a;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.b =3D b;<= br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;Ref =
2&quot;);<br>
&nbsp;&nbsp; }<br><br>&nbsp;&nbsp; public static void main(String[] args) {= <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Reference r =3D new Reference(null);</sp= an></div><div><font face=3D"arial" size=3D"3"><span style=3D"border- collaps=
e:collapse;font-size:13px">&nbsp;&nbsp;}</span></font></div>
<div><font face=3D"arial" size=3D"3"><span style=3D"border- collapse:collaps= e;font-size:13px">}</span></font></div><div><font face=3D"arial" size=3D"3"=
<span style=3D"border-collapse:collapse;font-size:13px"><br></ span></font>=
</div><div>
<font face=3D"arial" size=3D"3"><span style=3D"border- collapse:collapse;fon= t-size:13px">&nbsp;&nbsp; I was expecting to have the following output:</sp= an></font></div><div><font face=3D"arial" size=3D"3"><span style=3D"border-=
collapse:collapse;font-size:13px"><br>
</span></font></div><div><font face=3D"arial" size=3D"3"><span style=3D"bor= der-collapse:collapse;font-size:13px">preinit Reference(Object)</ span></fon= t></div><div><font face=3D"arial" size=3D"3"><span style=3D"border- collapse=
:collapse;font-size:13px">preinit Reference(Object, Object)<br>
Ref 2<br>after returning Reference(Object, Object)<br>Ref<br>after returnin= g Reference(Object)<br><br></span></font></div><div><font face=3D"arial" si= ze=3D"3"><span style=3D"border-collapse:collapse;font-size: 13px">&nbsp;&nbs=
p;However I obtain:</span></font></div>
<div><font face=3D"arial" size=3D"3"><span style=3D"border- collapse:collaps= e;font-size:13px"><br></span></font></div><div><font face=3D"arial" size=3D= "3"><span style=3D"border-collapse:collapse;font-size:13px">preinit Referen=
ce(Object)<br>
Ref 2<br>after returning Reference(Object, Object)<br>Ref<br>after returnin= g Reference(Object)<br></span></font></div><div><br></div><div><font face= =3D"arial" size=3D"3"><span style=3D"border-collapse:collapse;font- size:13p= x">&nbsp;&nbsp;i.e. the invocation to preinit Reference(Object, Object) is = missing... even though the pointcut is correctly captured by the aspect... =
!!!</span></font></div>
<div><font face=3D"arial" size=3D"3"><span style=3D"border



Back to the top