Jun 23rd, 2005, 03:43 PM
introduce and intercept
Is it possible to introduce an interface to an existing class and also intercept calls to the methods of the original class? I've tried to add two interceptors to a proxy, but because I had to set exposeProxy, singleton and proxyTargetClass to true for the introduction, subsequent method interceptors aren't called.
I've tried (as a hack) to create the introduction proxy and then create another proxy on to that, but nothing seems to work. Am i missing something conceptually or is this a limit of the proxy-based approach? Would an alternative AOP framework like Aspectwerkz allow such functionality?
Jun 26th, 2005, 02:52 PM
I don't fully understand your problem but subsequent method calls from within the proxy context (i.e. from within an intercepted method to a method in the same class) are by nature (of a proxy based framework) never intercepted. This does not have anything to do with introductions or the proxyTargetClass features.
You can get to the current Proxy by using AopContext.currentProxy() and cast is to the interface/class you need. This is not a really elegant solution I must admit.
Jun 27th, 2005, 04:33 AM
Say I have a class called person with a method called "getName" and I want to introduce a new interface called "Coder" so I can ask a person that implements the interface for their favourite language. I can min-in the Coder interface and call the "getFavouriteLanguage" method and the code block in my mixin is called.
Now I'd like to intercept the call to "getName" so I can change the call to return their nick if they are a coder. If I have already used a mixin, my method intercepter doesn't work. It seems that I can only get a mixin *or* a method-intercepter, but I can't use the same proxy factory for both types.