This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
AJP/DiSLProject2022/src-profiler/ex10/Profiler.java

52 lines
2 KiB
Java
Raw Normal View History

2023-01-09 16:39:09 +00:00
package ex10;
2023-01-09 17:35:38 +00:00
import java.util.*;
2023-01-09 16:39:09 +00:00
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class Profiler {
private static final ConcurrentMap<String, Set<String>> methodInvocations = new ConcurrentHashMap<>();
2023-01-09 17:35:38 +00:00
private static final Map<String, Set<String>> methodDeclarations = new HashMap<>();
2023-01-09 16:39:09 +00:00
2023-01-09 17:35:38 +00:00
private static final Map<String, Set<String>> inheritedMethods = new HashMap<>();
2023-01-09 16:39:09 +00:00
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
for (final String className : methodInvocations.keySet()) {
System.out.println("=== Class Name: " + className + " ===");
2023-01-09 17:35:38 +00:00
for (final String method : methodDeclarations.getOrDefault(className, Set.of())) {
2023-01-09 16:39:09 +00:00
System.out.println("Declared: " + method);
}
2023-01-09 17:35:38 +00:00
for (final String method : inheritedMethods.getOrDefault(className, Set.of())) {
2023-01-09 16:39:09 +00:00
System.out.println("Inherited: " + method);
}
for (final String method : methodInvocations.get(className)) {
System.out.println("Executed: " + className.replace('.', '/') + "." + method);
}
}
}));
}
private Profiler() {
}
2023-01-09 17:35:38 +00:00
private static void register(final String className, final String methodName, final Map<String, Set<String>> methods) {
2023-01-09 16:39:09 +00:00
methods.computeIfAbsent(className, ignored -> Collections.synchronizedSet(new HashSet<>()))
.add(methodName);
}
public static void registerInvoke(final String className, final String methodName) {
register(className, methodName, methodInvocations);
}
2023-01-09 17:42:06 +00:00
public static void registerDeclared(final String className, final String methodName) {
2023-01-09 16:39:09 +00:00
register(className, methodName, methodDeclarations);
}
public static void registerInherited(final String className, final String methodName) {
register(className, methodName, inheritedMethods);
}
}