-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMain.java
More file actions
76 lines (63 loc) · 3.09 KB
/
Copy pathMain.java
File metadata and controls
76 lines (63 loc) · 3.09 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
package org.example;
import org.example.metrics.GlobalMetrics;
import org.example.metrics.visitors.ClassAnalyzer;
import org.objectweb.asm.ClassReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
public static void main(String[] args) throws IOException {
String inputJar = "src/main/resources/guava.jar";
String outputFile = "src/main/resources/output.json";
if (args.length == 2) {
inputJar = args[0];
outputFile = args[1];
}
GlobalMetrics globalMetrics = new GlobalMetrics();
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(globalMetrics);
reader.accept(analyzer, 0);
}
}
}
globalMetrics.calculateFinalMetrics();
printMetricsToConsole(globalMetrics);
saveMetricsToJson(globalMetrics, outputFile);
}
private static void printMetricsToConsole(GlobalMetrics metrics) {
System.out.println("Максимальная глубина наследования: " + metrics.getMaxInheritanceDepth());
System.out.println("Средняя глубина наследования: " + metrics.getAvgInheritanceDepth());
System.out.println("Средняя метрика ABC: " + metrics.getAvgAbcMetric());
System.out.println("Среднее количество переопределенных методов: " + metrics.getAvgOverriddenMethods());
System.out.println("Среднее количество полей в классе: " + metrics.getAvgFieldCount());
}
private static void saveMetricsToJson(GlobalMetrics metrics, String outputFile) {
String json = String.format(
"{\n" +
" \"maxInheritanceDepth\": %d,\n" +
" \"avgInheritanceDepth\": %f,\n" +
" \"avgAbcMetric\": %f,\n" +
" \"avgOverriddenMethods\": %f,\n" +
" \"avgFieldCount\": %f\n" +
"}",
metrics.getMaxInheritanceDepth(),
metrics.getAvgInheritanceDepth(),
metrics.getAvgAbcMetric(),
metrics.getAvgOverriddenMethods(),
metrics.getAvgFieldCount()
);
try (FileWriter writer = new FileWriter(outputFile)) {
writer.write(json);
System.out.println("Метрики сохранены в файл: " + outputFile);
} catch (IOException e) {
System.err.println("Ошибка при записи в файл: " + e.getMessage());
}
}
}