Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aspectj and catching private or inner methods

I've configureg AspectJ with Spring and it works fine when "catching" public methods called from out of the class. Now I want do something like this:

public class SomeLogic(){

   public boolean someMethod(boolean test){

      if(test){
        return innerA();
      } else {
        return innerB();
      }
   }


   private boolean innerA() {// some logic}
   private boolean innerA() {// some other logic}

}

SomeLogic is a SpringBean. The methods innerA() and innerB() could be declared as private or public - the method someMethod() is called from a Struts action. Is it possible to catch with AspectJ the methods innerA() or innerB() called from someMethod() ?

My config (XML based):

    <aop:aspect id="innerAAspect" ref="INNER_A">
        <aop:pointcut id="innerAService" expression="execution(* some.package.SomeLogic.innerA(..))"/>
    </aop:aspect>

    <aop:aspect id="innerAAround" ref="INNER_A">
        <aop:around pointcut-ref="innerAService" method="proceed"/>
    </aop:aspect>


    <aop:aspect id="innerBAspect" ref="INNER_B">
        <aop:pointcut id="innerBService" expression="execution(* some.package.SomeLogic.innerB(..))"/>
    </aop:aspect>

    <aop:aspect id="innerBAround" ref="INNER_B">
        <aop:around pointcut-ref="innerBService" method="proceed"/>
    </aop:aspect>
like image 248
awonline Avatar asked Dec 09 '10 19:12

awonline


1 Answers

Yes it is easy to catch private methods with AspectJ.

An example that prints a sentence before all private methods:

 @Pointcut("execution(private * *(..))")
 public void anyPrivateMethod() {}

 @Before("anyPrivateMethod()")
 public void beforePrivateMethod(JoinPoint jp) {
     System.out.println("Before a private method...");
 }

If you are familiar with Eclipse, I recommend to develop AspectJ with STS or only install the AJDT plugin.

More information about Spring AOP capabilities can be found in the Spring reference documentation here.

like image 179
Espen Avatar answered Sep 28 '22 16:09

Espen