// 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.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Around;

//work with Spring DI
@Component("error2")
// aspect
@Aspect
public class Error2 {
    // pointcut:
    //   public visibility, any return type, class dynamic.IntMath, any method name, any arguments
    //   public visibility, any return type, class dynamic.DoubleMath, any method name, any arguments
    @Pointcut("execution(public * dynamic.IntMath.*(..)) || execution(public * dynamic.DoubleMath.*(..))")
    void matherror() { };
    // advice:
    //    around
    @Around("matherror()")
    public Object oops(ProceedingJoinPoint pjp) {
        Object res = null;
        try {
            res = pjp.proceed();
        } catch(Throwable e) {
            System.out.println("AOP> Exception in math calculation: " + e.getMessage());
            System.exit(0);
        }
        return res;
    }
}
