Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] around constructor execution advice

Interesting...at first I thought that the following behavior is a bug,
but the more I think about it, I think this is the correct behavior.

The following code is producing a compile error:

public class AroundConstructor {
    AroundConstructor() { }
    static aspect Aspect {
        AroundConstructor around() : execution(AroundConstructor.new()) {
            return proceed();
        }
    }
}

"Incompatible return type for applying to constructor-execution(void
AroundConstructor.<init>())"

But when I change from execution to call, it compiles cleanly.  I
think the issue is that this constructor may be called by a sub-class.
 In this case the sub-class is expecting a type of "AroundConstructor"
or greater (ie- a super-class), but when the constructor execution is
wrapped in around advice, the proceed() call can return anythiing of
type AroundConstructor (ie- a sub-class) and hence the types would not
match if called from a sub-class.

When changed to a call PCD, the advice is triggered at the call site
(so, the compiler knows that it is not coming from a sub-class's
constructor) and there are no problems.

Just an interesting tidbit I found for a Friday afternoon...

--a


Back to the top