// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package dynamic;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Before;

//work with Spring DI
@Component("check1")
// aspect
@Aspect
public class Check1 {
    // pointcut:
    //   public visibility, any return type, any class, divide method, two arguments any type
    @Pointcut("execution(public * *.divide(*, *))")
    public void mathcheck() { };
    // advice:
    //   before
    @Before("mathcheck()")
    public void check(JoinPoint thisJoinPoint) {
        Object o = thisJoinPoint.getArgs()[1];
        if(o instanceof Integer) {
            Integer oi = (Integer)o;
            if(oi.intValue() == 0) {
                System.out.println("AOP> About to divide by integer zero");
            }
        } else if(o instanceof Double) {
            Double od = (Double)o; 
            if(od.doubleValue() == 0.0) {
                System.out.println("AOP> About to divide by double zero");
            }
        } else {
            throw new RuntimeException("Unsupported data type in divide: " + o.getClass().getName());
        }
    }
}
