Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] overridden method not caught

I am guessing that you are using the AspectJ's byte code weaving (and not Spring's proxy-based). Right?

If so, I just tried your example and it works fine for me (I removed Spring related code as that is not significant here).

-Ramnivas

On Wed, Jan 6, 2010 at 12:40 AM, <adrian.p.smith@xxxxxx> wrote:
I am experimenting with the Spring integration for Aspectj as documented
at
http://static.springsource.org/spring/docs/2.5.x/reference/aop.html#aop-
using-aspectj
and have come across a problem.

If I alter the class invocation to override the method that I want to
instrument, the method is no longer detected.

So, my class is:

package foo;

public class EntitlementCalculationService {
       public void calculateEntitlement() {
               System.out.println("executing");
               try {
                       Thread.sleep(200);
               } catch (InterruptedException e) {
                       e.printStackTrace();
               }
       }
}

My aspect is:

package foo;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;

@Aspect
public class ProfilingAspect {
   @Around("methodsToBeProfiled()")
   public void profile(ProceedingJoinPoint pjp) throws Throwable {
       StopWatch sw = new StopWatch(getClass().getSimpleName());
       try {
           sw.start(pjp.getSignature().getName());
           pjp.proceed();
       } finally {
           sw.stop();
           System.out.println(sw.prettyPrint());
       }
   }

   @Pointcut("execution(public * foo..*.*(..))")
   public void methodsToBeProfiled(){}
}

And my code to test is:

package foo;

import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;

public final class Main {
       public Main() {
               ApplicationContext ctx = new
ClassPathXmlApplicationContext("/beans.xml", Main.class);

               EntitlementCalculationService
entitlementCalculationService = new EntitlementCalculationService();{

       entitlementCalculationService.calculateEntitlement();
       }

       public static void main(String[] args) {
               new Main();
       }
}

This gives the correct output:

    [java] StopWatch 'ProfilingAspect': running time (millis) = 201
    [java] -----------------------------------------
    [java] ms     %     Task name
    [java] -----------------------------------------
    [java] 00201  100%  calculateEntitlement

However, if I change the test code to override the calculateEntitlement
method like this:

package foo;

import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;


public final class Main {
       public Main() {
               ApplicationContext ctx = new
ClassPathXmlApplicationContext("/beans.xml", Main.class);

               EntitlementCalculationService
entitlementCalculationService = new EntitlementCalculationService(){
                       @Override
                       public void calculateEntitlement() {
                               System.out.println("test1");
                       }
               };

               entitlementCalculationService.calculateEntitlement();
       }

       public static void main(String[] args) {
               new Main();
       }
}

The aspect fails to trigger.

Any ideas please?

TIA,

Adrian Smith
BT Group
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top