[aspectwerkz-user] RE: Initializing references from a "before" aspect
"Hindin, Avi" <[email protected]>
| Newsgroups | gmane.comp.java.aspectwerkz.user |
|---|---|
| Message-ID | <F8B9B12641FB6B4FAC761BDEF9D63B04050B35E2@tlv-ex-01.adprod.bmc.com> |
Hi Jonas,
Firstly thank you for your quick response; I'm trying to do this example:
<aspectwerkz>
<system id="AspectWerkzExample">
<package name="testAOP">
<aspect class="MyAspect2">
<!--pointcut name="greetMethodPCut" expression="execution(*
testAOP.HelloWorld.greet(..)) && args(arg0, arg1)"/-->
<pointcut name="greetMethodPCut" expression="execution(public *
testAOP.HelloWorld.greet(..))"/>
<advice name="aroundGreeting(MyCustomJoinPoint jp)"
type="around" bind-to="greetMethodPCut"/>
</aspect>
</package>
</system>
</aspectwerkz>
package testAOP;
import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
public class MyAspect2 {
// we could inherit StaticJoinPoint instead
public static interface MyCustomJoinPoint extends JoinPoint {
Object proceed(String arg0, String arg1);
}
public Object aroundGreeting(MyCustomJoinPoint jp) throws Throwable
{
// lets change the args :
return jp.proceed("new arg0", "new arg1");
}
}
public class HelloWorld {
public static void main(String args[]) {
HelloWorld world = new HelloWorld();
world.greet(null, null);
}
public void greet(String s1,String s2)
{
System.out.println(">>>>>>>>>>> <" + s1 + ">, <"+ s2 + ">");
}
}
At AW compile time I got this error:
org.codehaus.aspectwerkz.exception.DefinitionException: could not find
advice method [aroundGreeting(MyCustomJoinPoint jp)] in [testAOP.MyAspect2]
(are you using a compiler extension that you have not registered?) (are you
using XML defined advice, with StaticJoinPoint bindings without specifying
the fullsource like signature?)
Can you help me with this problem?
Thanks,
Avi Hindin.
-----Original Message-----
From: Jonas Bonér [mailto:[email protected]]
Sent: Sunday, April 17, 2005 7:36 PM
To: [email protected]
Cc: Hindin, Avi; Haimov, Gilad; Sivan, Aryeh
Subject: Re: Initializing references from a "before" aspect
Hi there.
If I understand you correctly, you want to modify the value of a
parameter that is passed to the method you are advising.
If so I would use an around advice and make use of the custom join
point (see
http://aspectwerkz.codehaus.org/new_features_in_2_0.html#Strongly_typed_proc
eed
for details) to change the parameter value and pass the modified
parameter value on.
Does this help?
/Jonas
On hi there4/17/05, Hindin, Avi <[email protected]> wrote:
> Hi guys,
>
>
>
> My aim is to write a "before" aspect that initializes a reference passed
> to
> the original method as null.
>
>
>
> That is, what I logically want to do in the aspect is:
>
> myMethod(String str)
>
> {
>
> // "before" aspect should do the following:
>
> If(str==null)
>
> str = "abc";
>
>
>
>
>
> // applicative logic here ....
>
> }
>
>
>
> All this is well, but, of course, does not work. Why? Because AspectWerkz
> implements "before" aspects by calling
>
> an automatically generated method to do the work (example below) and you
> cannot, of course, initialize a reference
>
> within a method.
>
>
>
> But is there a workaround? Please notice that such a workaround cannot
> involve attaching the aspect
>
> to code _callng_ the method, since that code is not available to me.
>
>
>
> Your help will be highly appreciated!
>
>
>
> Avi Hindin
>
> BMC Software
>
>
>
>
>
>
>
>
>
--
/Jonas