Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » AspectJ » around advice question
around advice question [message #61290] Tue, 24 January 2006 16:40 Go to next message
Asaf is currently offline AsafFriend
Messages: 36
Registered: July 2009
Member
Hi,



I'm testing the usage of Aspectj and encountered a problem.



I wrote the following code:



public aspect myAspect{



pointcut publicMethods() : execution(public * myMain.func(..));



pointcut logObjectCalls() : execution(* Logger.*(..));



pointcut loggableCalls() : publicMethods(); //l&& ! logObjectCalls();



before() : loggableCalls(){

Logger.entry(thisJoinPoint.getSignature().toString());

}



after() : loggableCalls(){

Logger.exit(thisJoinPoint.getSignature().toString());

}



void around(): call(public void myMain.func(String)){

if(Math.random() > .5){

System.out.println("> 0.5");

proceed(); //go ahead with the method call

}

else{

System.out.println("< 0.5");

}

}



}



The function FUNC of MYMAIN class has only one argument which is of String
type.

What I would like to achieve is that I will be able to check the input
parameters in the AROUND advice.



Thanks in advance,



Asaf Lahav
Re: around advice question [message #61345 is a reply to message #61290] Wed, 25 January 2006 15:35 Go to previous messageGo to next message
Tom Coupland is currently offline Tom CouplandFriend
Messages: 14
Registered: July 2009
Junior Member
Hi Asaf,

My first attempt at an AspectJ solution, so hopefully this is right!

Think what looking for is the args designator. Heres a snip for you


public class AnObject {

public String doSomethingStringy (String string){
return string+"added by method";
}

public static void main(String[] args){

AnObject obj = new AnObject();

System.out.println(obj.doSomethingStringy("Original"));

}
}



public aspect AnAspect {

pointcut effectDoSomething(String string) : call(public String
AnObject.doSomethingStringy (String)) && args(string);

String around(String string) : effectDoSomething(string){

string = string+" added before ";
string = proceed(string);
string = string+" added after";
return string;
}
}

Outputs:

Original added before added by method added after


As you can see this makes the parameters availible in your advice block
for manipulation/testing. Hopfully thats what your looking for!

Enjoy

Tom
Re: around advice question [message #61369 is a reply to message #61345] Thu, 26 January 2006 08:54 Go to previous message
Asaf is currently offline AsafFriend
Messages: 36
Registered: July 2009
Member
Thanks tom...
it helped allot.
Re: around advice question [message #591978 is a reply to message #61290] Wed, 25 January 2006 15:35 Go to previous message
Tom Coupland is currently offline Tom CouplandFriend
Messages: 14
Registered: July 2009
Junior Member
Hi Asaf,

My first attempt at an AspectJ solution, so hopefully this is right!

Think what looking for is the args designator. Heres a snip for you


public class AnObject {

public String doSomethingStringy (String string){
return string+"added by method";
}

public static void main(String[] args){

AnObject obj = new AnObject();

System.out.println(obj.doSomethingStringy("Original"));

}
}



public aspect AnAspect {

pointcut effectDoSomething(String string) : call(public String
AnObject.doSomethingStringy (String)) && args(string);

String around(String string) : effectDoSomething(string){

string = string+" added before ";
string = proceed(string);
string = string+" added after";
return string;
}
}

Outputs:

Original added before added by method added after


As you can see this makes the parameters availible in your advice block
for manipulation/testing. Hopfully thats what your looking for!

Enjoy

Tom
Re: around advice question [message #591982 is a reply to message #61345] Thu, 26 January 2006 08:54 Go to previous message
Asaf is currently offline AsafFriend
Messages: 36
Registered: July 2009
Member
Thanks tom...
it helped allot.
Previous Topic:pointcut all method calls on an object that implements an interface
Next Topic:Newbie on Book
Goto Forum:
  


Current Time: Thu Apr 18 23:18:59 GMT 2024

Powered by FUDForum. Page generated in 0.02203 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top