Initialization pointcut problem [message #64862] |
Wed, 10 May 2006 09:59  |
Eclipse User |
|
|
|
Originally posted by: skirkaw.googlemail.com
Hi,
I could not figure out the following initialization pointcut problem. Any
help would be appreciated.
Within the method of my class I do initialization of an object and would
like to replace this object with another object. Hence, I write an around
advice. But the poincut i write is not matched with the object in my class
methosd. To illustrate, the code is simply as follows:
class Test {
public void doTest() {
String s=new String("old String");
System.out.println(s);
}
}
// I would like to rewrite String s
public aspect TestAspect {
pointcut p(): initialization(String.new(String));
void around (): p() {
String s=new String("new String");
proceed();
}
}
But my pointcut p is not matched with the line "String s=new String("old
String");". What am I mssing?
Thanks in advance
|
|
|
Re: Initialization pointcut problem [message #65016 is a reply to message #64862] |
Fri, 19 May 2006 08:07  |
Eclipse User |
|
|
|
To match calls to a constructor you need something like
"call(String.new(..))". Then to replace the object, you need to return
it from the around advice (and not call proceed). For example:
String around(): call(String.new(..)) && within(Test) {
return new String("new String");
}
Note also the "&& within(Test)" bit - you need to scope your pointcut
appropriately. This is good practice in general, but particularly
important here, otherwise your around advice would advise the call to
"new String" in its own body, leading to a stack overflow error.
Regards,
Matt.
Steve kirkaw wrote:
> Hi,
>
> I could not figure out the following initialization pointcut problem.
> Any help would be appreciated.
>
> Within the method of my class I do initialization of an object and would
> like to replace this object with another object. Hence, I write an
> around advice. But the poincut i write is not matched with the object in
> my class methosd. To illustrate, the code is simply as follows:
>
> class Test {
>
> public void doTest() {
>
> String s=new String("old String");
>
> System.out.println(s);
> }
> }
>
>
> // I would like to rewrite String s
>
> public aspect TestAspect {
>
> pointcut p(): initialization(String.new(String));
>
> void around (): p() {
>
> String s=new String("new String");
> proceed();
> }
> }
>
>
> But my pointcut p is not matched with the line "String s=new String("old
> String");". What am I mssing?
>
> Thanks in advance
>
|
|
|
Re: Initialization pointcut problem [message #593703 is a reply to message #64862] |
Fri, 19 May 2006 08:07  |
Eclipse User |
|
|
|
To match calls to a constructor you need something like
"call(String.new(..))". Then to replace the object, you need to return
it from the around advice (and not call proceed). For example:
String around(): call(String.new(..)) && within(Test) {
return new String("new String");
}
Note also the "&& within(Test)" bit - you need to scope your pointcut
appropriately. This is good practice in general, but particularly
important here, otherwise your around advice would advise the call to
"new String" in its own body, leading to a stack overflow error.
Regards,
Matt.
Steve kirkaw wrote:
> Hi,
>
> I could not figure out the following initialization pointcut problem.
> Any help would be appreciated.
>
> Within the method of my class I do initialization of an object and would
> like to replace this object with another object. Hence, I write an
> around advice. But the poincut i write is not matched with the object in
> my class methosd. To illustrate, the code is simply as follows:
>
> class Test {
>
> public void doTest() {
>
> String s=new String("old String");
>
> System.out.println(s);
> }
> }
>
>
> // I would like to rewrite String s
>
> public aspect TestAspect {
>
> pointcut p(): initialization(String.new(String));
>
> void around (): p() {
>
> String s=new String("new String");
> proceed();
> }
> }
>
>
> But my pointcut p is not matched with the line "String s=new String("old
> String");". What am I mssing?
>
> Thanks in advance
>
|
|
|
Powered by
FUDForum. Page generated in 0.04828 seconds