开发者

Policy enforcement to add a new item - ASPECTJ

开发者 https://www.devze.com 2023-03-06 21:05 出处:网络
I have to enfo开发者_JAVA百科rce 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.....

I have to enfo开发者_JAVA百科rce 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....


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消