Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
90 lines (78 loc) · 3.48 KB
/
Copy pathMain.java
File metadata and controls
90 lines (78 loc) · 3.48 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.example;
import org.example.coverage.CoverageCollector;
import org.example.coverage.CoverageReport;
import org.example.coverage.CoverageTransformer;
import org.example.metrics.Metrics;
import org.example.visitor.ClassAnalyzer;
import org.objectweb.asm.ClassReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.Enumeration;
import java.util.Locale;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
private static final CoverageCollector collector = CoverageCollector.getInstance();
public static void premain(String agentArgs, Instrumentation inst) {
inst.addTransformer(new CoverageTransformer(), true);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("[LineCoverageAgent] Saving coverage report...");
CoverageReport report = collector.generateReport();
try {
report.saveToFile("coverage-report.json");
} catch (IOException e) {
throw new RuntimeException(e);
}
report.printSummary();
}));
}
public static void main(String[] args) throws IOException {
String inputJar = "src/main/resources/fescar.zip";
String outputFile = "src/main/resources/output.json";
Metrics metrics = new Metrics();
try (JarFile inputJarFile = new JarFile(inputJar)) {
Enumeration<JarEntry> enumeration = inputJarFile.entries();
while (enumeration.hasMoreElements()) {
JarEntry entry = enumeration.nextElement();
if (entry.getName().endsWith(".class")) {
ClassReader reader = new ClassReader(inputJarFile.getInputStream(entry));
ClassAnalyzer analyzer = new ClassAnalyzer(metrics);
reader.accept(analyzer, 0);
}
}
}
metrics.calculateClassesDepth();
printMetricsToConsole(metrics);
saveMetricsToJson(metrics, outputFile);
}
private static void printMetricsToConsole(Metrics metrics) {
System.out.println("Max Depth: " + metrics.getMaxDepth());
System.out.println("Average Depth: " + metrics.getAverageDepth());
System.out.println("Average ABC: " + metrics.getAverageAbc());
System.out.println("Average Overridden Methods: " + metrics.getAverageFieldsCount());
System.out.println("Average Fields Count: " + metrics.getAverageOverriddenMethods());
}
private static void saveMetricsToJson(Metrics metrics, String outputFile) {
String json = String.format(Locale.US,
"{\n" +
" \"maxDepth\": %d,\n" +
" \"averageDepth\": %f,\n" +
" \"averageAbc\": %f,\n" +
" \"averageOverriddenMethods\": %f,\n" +
" \"averageFieldsCount\": %f\n" +
"}",
metrics.getMaxDepth(),
metrics.getAverageDepth(),
metrics.getAverageAbc(),
metrics.getAverageOverriddenMethods(),
metrics.getAverageFieldsCount()
);
try (FileWriter writer = new FileWriter(outputFile)) {
writer.write(json);
System.out.println("Metrics saved to file: " + outputFile);
} catch (IOException e) {
System.err.println("Error write file: " + e.getMessage());
}
}
}