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;">"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."</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 'first-called constructor' is why
you do=
n'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;">> Is there a way to obtain the behaviour I want
with ot=
her pointcuts? <br>
</span></div><div><span class=3D"Apple-style-span" style=3D"border-
collapse=
: collapse;">In a quick few mins can'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"><<a href=3D"mailto:alex.villazon@xxxxxxxxxxx">alex.villazon@xxxxxx
=
si.ch</a>></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> I wo=
uld like to execute some code at the very beginning and the end of
every co=
nstructor... I'm using preinitialization and after
returning as f=
ollows.</div>
<div><br></div><div>public aspect Foo {</div><div>
=
<span style=3D"border-collapse:collapse;font-family:arial;font-size:
13px">&=
nbsp;pointcut allExecsNews() : execution(*.new(..))
&& =
!within(Foo) ; </span></div><span style=3D"border-
collapse:collapse;fo=
nt-family:arial;font-size:13px"> pointcut
preInit() : pre=
initialization(*.new(..)) && !within(Foo);<br>
<br> before() : preInit() {<br>
System.=
out.println("preinit " +
thisJoinPoint.getSignature());<br> =
}<br><br> after() returning() :
allExecsNews() {<b=
r> System.out.println("after returning "
+ this=
JoinPoint.getSignature());<br>
}<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> =
Object
a; <br>
Object b;<br><br> Reference(Object a)
{<br>&=
nbsp; this(a, null);<br>
System.o=
ut.println("Ref");<br> }<br><br>
Referenc=
e(Object a, Object b)
{<br> =
this.a =3D a;<br> this.b
=3D b;<=
br>
System.out.println("Ref =
2");<br>
}<br><br> public static void main(String[]
args) {=
<br> 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"> }</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"> 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"> &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"> 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