Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Advice on Constructors With Subclasses

Hi James,
I think it still has a problem when multiple this constructors are
involved. In that case, the advice code would be executed twice (or more
times) depending on how many this() calls there are between constructor.

In this case the initialization pointcut comes in handy, cause it
matches the "first constructor called", so basically makes a distinction
between the originally called constructor and internal calls to this().

Simply changing it to :

public aspect CheckingWithSimplestAspectJEver {

    pointcut constructor(A inst) : initialization( && this(inst);
    after(A inst) : constructor(inst) {
System.out.println("Done with " + inst.getClass().getSimpleName());

Will avoid also the following situation :

public class C extends B {  
    public C() {
        System.out.println("I'm in C");
    public C(String value) {
        System.out.println("In C with value");

new C("Hello there");

I'm in A
I'm in B
I'm in C
In C with value
Done with C


James Elliott wrote:
> Simone,
> You are brilliant.
> This also works with construction via reflection, even within a
> delegate, and is orders of magnitude faster than your first solution.
> Thanks so much. =)
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx

Simone Gianni
CEO Semeru s.r.l.
Apache Committer

Back to the top