Question

I have to enforce a policy issuing a warning if items not belonging to a particular category are being added, apart from the three which are allowed and disallowing such additions.....

So far i am able to find the items and issue warning.... but not sure how to stop them from being added.... For Eg.

Allowed categories Shoes and socks

but if i try and add a vegetable item to the inventory it should give me a warning saying "category not allowed../nItem will not be added to inventory"..... and then proceed to the next item....

This is what i've written so far.....

import org.aspectj.lang.*;

public aspect a8 {

  boolean check;

    pointcut deliverMessage(): call(* ShoppingCart.addItem(..));

    pointcut interestingcalls(String categorie): call(Item.new(..)) && args(*, *, categorie);

      before(String categorie): interestingcalls(categorie)
       { 
       if(categorie.equals("Shoes"))
        {        
       System.out.println("categorie detect:" +categorie);
        }
        else if(categorie.equals("socks"))
        {        
       System.out.println("categorie detect:" +categorie);
        }
        else
        {
        check=true;
        around(categorie);
        System.out.println("please check categorie" +categorie);
        }
    }

    around(String categorie): interestingcalls(categorie) {
            System.out.println("Start! "+categorie);
            proceed(categorie);
            System.out.println("End!");
    }
}

I know i'm not using the around advice correctly....

Was it helpful?

Solution

What you probabily want is comething like this:

public aspect CartAspect {

    pointcut checkAdd(String categorie) : call (void ShoppingCart.addItem(*)) && args(categorie);

    void around(String categorie) : checkAdd(categorie) {
        System.out.println("Start! " + categorie);
        if (categorie.equals("shoes")) {
            System.out.println("categorie detect:" + categorie);
            proceed(categorie);
        } else if (categorie.equals("socks")) {
            System.out.println("categorie detect:" + categorie);
            proceed(categorie);
        } else {
            System.out.println("please check categorie " + categorie);
        }
        System.out.println("End!");
    }

}

Notice that in the case that the category is different from "shoes" or "socks", we DON'T call the proceed() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top