Hendrik Jungnitsch
2022-09-29 e7d09831122841390b36555c28a762d66d41a562
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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<String, LongAdder> methodCounts = new ConcurrentHashMap<>();
 
 
    protected void increase(String methodName) {
        methodCounts.computeIfAbsent(methodName,s -> new LongAdder())
                .increment();
    }
 
    public Map<String,Long> 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());
    }
}