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.....

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

pointcut interestingCalls(String category) :
    call(Item.new(..)) && args(*, *, category);

before(String category): interestingCalls(category) { 
    if(category.equals("Socks")) {       
        System.out.println("category detect: " + category);
    else if(category.equals("Shoes"))
        System.out.println("category detect: " + category);
    else {
        check=true; 
        System.out.println("please check category " + category);
    }
}
Was it helpful?

Solution

Why not use the around aspect instead. Then, if they are not of the correct category you don't go into that method, so it gets skipped, if the skipped method is just doing the adding.

UPDATE:

Here is an example from AspectJ In Action, by Manning Publication.

public aspect ProfilingAspect {
  pointcut publicOperation() : execution(public * *.*(..));
  Object around() : publicOperation() {
    long start = System.nanoTime();
    Object ret = proceed();
    long end = System.nanoTime();
    System.out.println(thisJoinPointStaticPart.getSignature()
      + " took " + (end-start) + " nanoseconds");
    return ret;
  }
}

So, if you wanted to check if you should add the item, if it is an allowed category then just call proceed, otherwise you would just return a null perhaps.

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