package de.gedoplan.seminar.sbt.di.exercise.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.LongAdder; import java.util.stream.Collectors; @Aspect @Component public class MethodCounter { private ConcurrentMap methodCounts = new ConcurrentHashMap<>(); protected void increase(String methodName) { methodCounts.computeIfAbsent(methodName,s -> new LongAdder()) .increment(); } public Map getMethodCounts() { return methodCounts.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey,e -> e.getValue().longValue())); } @AfterReturning("@annotation(Counted)") protected void count(JoinPoint joinPoint) { increase(joinPoint.getSignature().getName()); } }