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

//work with Spring DI
@Component("error1")
// aspect
@Aspect
public class Error1 {
    // 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:
    //   after throwing
    @AfterThrowing(pointcut="matherror()", throwing="e")
    public void oops(Throwable e) {
        System.out.println("AOP> Exception in math calculation: " + e.getMessage());
        System.exit(0);
    }
}
