Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How to use "call pointcut", when using reflection

Tarun Sharma wrote:
The problem : If I have to use "call pointcut" as in
call(ConstructorPattern) and if there the constructor is invoked via
reflection then it doesn't seem to execute the advice. I believe the
reason is at the compile time weaving the "ajc compiler" does not weave
for clients executing via reflection.

The only way I believe you can do this in AspectJ-1.1 is to put your advice on the reflective call itself and then do a dynamic test, i.e.

after() returning(Object newObject):
    call(Object java.lang.reflect.Constructor.newInstance(*))
{
    if (newObject instanceof MyClass) {
        <do something here with newObject>
    }
}

The issue is I CANNOT use "execution pointcut" as I have to get return
type from the "call(ConstructorPattern)"

Hmm, are you sure you need this? Are you using around advice to change the return value, or do you just need to access it? You can use the this pcd to bind the value of the new Object, like this:

after(Object newObject) returning:
    execution(MyClass.new(..)) && this(newObject)
{
    <do something here with newObject>
}

I hope this helps - Jim




Back to the top