Pointcut for a method injected with AspectJ

209 Views Asked by At

How can I define a pointcut for a method injected with ApsectJ?

I have injected the method as follows and it works properly:

public void com.moeActivity.onBackPressed() {
    super.onBackPressed();
    Log.d("ATAG", "BACKK");
}

Now am trying to define a pointcut to detect the execution of the injected method but it is not successful, I tried the following:

pointcut eventActivity(): 
  execution(* com.moeActivity.onBackPressed(..));

Any help would be appreciated

Thanks!

1

There are 1 best solutions below

0
On

You need an advice body to run the enjected method (e.g. Log.d("ATAG", "BACKK") probably) inside of it like this:

public aspect LoggingAspect{
 after() : eventActivity() {
   Log.d("ATAG", "BACKK");
 } 
}

Doing this, the after advice connects with the named pointcut called eventActivity and runs the crosscutting action (i.e. Log.d("ATAG", "BACKK")) after super.onBackPressed(). As @kriegaex says, you need to read some AspectJ examples to comprehend it efficiently.