aspectjaspects

Applying Aspects on package and supackages


I am using AspectJ to apply aspects on methods of classes under packages org.apache.http, org.apache.http.entity, org.apache.http.impl, org.apache.http.io and similarly others.

I used the aspects as below but it is not applied.

public pointcut capturehttp():within(org.apache.http..*) && (call(public * *(..)) || call(private * *(..)));
after():capturehttp()
{

    System.out.println("In test test test testy test http method set");

}

I also tried as suggested in Aspectj aspect for specifying multiple packages but it didn't work. Please suggest to me where I am wrong?


Solution

  • Use the following aspect:

    public aspect HttpCoreAspect {
    
        pointcut captureHttp(): within(com.my.pckg..*) && !within(HttpCoreAspect) 
            && (call(* org.apache.http..*.*(..)) || call(org.apache.http..*.new(..)));
    
        after(): captureHttp() {
            System.out.println("Apache HttpCore was invoked");
        }
    
    }
    

    The captureHttp() pointcut above will capture method or constructor calls to any type in package org.apache.http or any subpackage, called from any code within package com.my.pckg or any of its subpackages, excluding calls made from the aspect HttpCodeAspect itself (should you happen to invoke HttpCore from your after() advice, we don't want an infinite recursion to happen).