AJP/DiSLProject2022/src-profiler/ex10/Profiler.java
2023-01-09 17:39:14 +01:00

55 lines
2.0 KiB
Java

package ex10;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class Profiler {
private static final ConcurrentMap<String, Set<String>> methodInvocations = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Set<String>> methodDeclarations = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Set<String>> inheritedMethods = new ConcurrentHashMap<>();
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
for (final String className : methodInvocations.keySet()) {
System.out.println("=== Class Name: " + className + " ===");
for (final String method : methodDeclarations.get(className)) {
System.out.println("Declared: " + method);
}
for (final String method : inheritedMethods.get(className)) {
System.out.println("Inherited: " + method);
}
for (final String method : methodInvocations.get(className)) {
System.out.println("Executed: " + className.replace('.', '/') + "." + method);
}
}
}));
}
private Profiler() {
}
private static void register(final String className, final String methodName, final ConcurrentMap<String, Set<String>> methods) {
methods.computeIfAbsent(className, ignored -> Collections.synchronizedSet(new HashSet<>()))
.add(methodName);
}
public static void registerInvoke(final String className, final String methodName) {
register(className, methodName, methodInvocations);
}
public static void registerDeclare(final String className, final String methodName) {
register(className, methodName, methodDeclarations);
}
public static void registerInherited(final String className, final String methodName) {
register(className, methodName, inheritedMethods);
}
}