Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Question about constructor pointcuts

I've been reading Java Concurrency in Practice and that got me
thinking about an aspect pattern i've been using. The book discusses
that exposing a Object A's this reference to Object B from Object A's
constructor will not be thread safe because Object B may act on Object
A's this reference before Object A's constructor has actually
finished. Since the constructor has not finished, Object A's state is
indeterminate and if Object B changes Object A's state, that too will
have indeterminate behavior.

That said, here is my pointcut/advice...
@After ("execution((@MyAnnotation *).new(..)) && target(listener)")
public void doSomething(Object listener)
{
    registrar.register(listener)
}

And my question...
Does the advice run before the constructor has finished, thus exposing
this in a non-threadsafe manner?
What is a better way to write this advice such that something happens
after the constructor has completed?

Be well,
andy


Back to the top