[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Constructor pointcut

Constructors are tricky in Java, and that's exposed in AspectJ.  Here are some rules of thumb I use:

* If you want the join point on the "outside" of object creation, use:

    after() returning (Foo f): call(Foo.new(..)) { ... }

  you might be tempted to use "this" or "target" to expose the new object, but remember
  that if you're on the "outside" of object creation, the object itself might not be
  created yet... it only exists "on the way out", when you return the object.

* If you want the join point inside a particular constructor, use:

    after(Foo f) returning: this(f) && execution(Foo.new(..)) { ... }

  remember, though, that if you use "before" advice here, the body of the constructor
  will not have run, and so the object may be somewhat uninitialized.

* In the rare case that there are all sorts of constructors for the object
  that call each other with this(...) and you want exactly one join point 
  for each initialization of Foo, regardless of the path of constructors it takes, 
  then use:

    after(Foo f) returning: this(f) && initialization(Foo.new(..)) { ... }

Enjoy,

-erik
    
> -----Original Message-----
> From: Carlos Lizarralde [mailto:clizarralde@xxxxxxxxxx] 
> Sent: Monday, 30 December, 2002 6:05 am
> To: aspectj-users@xxxxxxxxxxx
> Subject: Re: [aspectj-users] Constructor pointcut
> 
> 
> It worked, EXCELLENT! 
> I was using the call primitive instead of execution. 
> Are you from Chile?? 
> En ese caso, saludos y feliz año nuevo
> Charly
> 
> 
> -----Original Message-----
> From: "Valentin Crettaz" <Valentin.Crettaz@xxxxxxx>
> To: <aspectj-users@xxxxxxxxxxx>
> Date: Mon, 30 Dec 2002 15:00:00 +0100
> Subject: Re: [aspectj-users] Constructor pointcut
> 
> > Here is how you can access a newly created instance:
> > 
> > pointcut newInstance(SomeObject obj) :
> >    this(obj) &&
> >    execution(SomeObject.new(..));
> > 
> > I hope this helps.
> > 
> > Val
> > 
> > ----- Original Message -----
> > From: "Carlos Lizarralde" <clizarralde@xxxxxxxxxx>
> > To: <aspectj-users@xxxxxxxxxxx>
> > Sent: Monday, December 30, 2002 2:48 PM
> > Subject: [aspectj-users] Constructor pointcut
> > 
> > 
> > > Hi, I am new to Aspect J and I have been trying to declare a point
> > cut in
> > > a constructor so that I can access the instance that I 
> just created.
> > > I have tried many things but none of them worked. What I 
> am tring to
> > do
> > > is to set in an object's attribute the time when It was created.
> > >
> > > Thanks ,
> > >
> > > Carlos Lizarralde