-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathExample.java
More file actions
105 lines (89 loc) · 3.88 KB
/
Copy pathExample.java
File metadata and controls
105 lines (89 loc) · 3.88 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.example;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.example.structs.ClassInfo;
import org.example.structs.MethodInfo;
import org.example.util.InheritanceDepthCalculator;
import org.example.util.OverrideDetector;
import org.example.visitor.ClassPrinter;
import org.objectweb.asm.ClassReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Example {
public static void main(String[] args) throws IOException {
List<ClassInfo> allClasses = new ArrayList<>();
try (JarFile sampleJar = new JarFile("src/main/resources/sample.jar")) {
Enumeration<JarEntry> enumeration = sampleJar.entries();
while (enumeration.hasMoreElements()) {
JarEntry entry = enumeration.nextElement();
if (entry.getName().endsWith(".class")) {
ClassPrinter cp = new ClassPrinter(allClasses);
InputStream inputStream = sampleJar.getInputStream(entry);
ClassReader reader = new ClassReader(inputStream);
reader.accept(cp, 0);
}
}
}
getStats(allClasses);
}
private static void getStats(
List<ClassInfo> allClasses
)
throws IOException
{
int totalFields = 0;
int totalA = 0;
int totalB = 0;
int totalC = 0;
int totalOverrideMethods = 0;
OverrideDetector detector = new OverrideDetector(allClasses);
InheritanceDepthCalculator calc = new InheritanceDepthCalculator(allClasses);
Map<String, String> superMap = new HashMap<>();
for (ClassInfo classInfo : allClasses) {
superMap.put(classInfo.name, classInfo.superName);
}
for (ClassInfo classInfo : allClasses) {
totalFields += classInfo.fields.size();
for (MethodInfo m : classInfo.methods) {
totalA += m.assignments;
totalB += m.branches;
totalC += m.conditions;
if (detector.isOverride(classInfo, m)) {
totalOverrideMethods++;
}
}
}
double abcMetric = Math.sqrt(
totalA * totalA +
totalB * totalB +
totalC * totalC
);
int maxDepth = calc.getMaxDepth(allClasses);
int avgDepth = calc.getAverageDepth(allClasses);
int avgFields = !allClasses.isEmpty() ? (totalFields / allClasses.size()) : 0;
int avgOverride = !allClasses.isEmpty() ? (totalOverrideMethods / allClasses.size()) : 0;
System.out.println("Средняя глубина наследования: " + avgDepth);
System.out.println("Максимальная глубина наследования: " + maxDepth);
System.out.println("Среднее количество полей в классе: " + avgFields);
System.out.println("Среднее количество переопределнных методов: " + avgOverride);
System.out.println("Метрика ABC: " + abcMetric);
Map<String, Object> metrics = new HashMap<>();
metrics.put("averageInheritanceDepth", avgDepth);
metrics.put("maxInheritanceDepth", maxDepth);
metrics.put("averageFieldsPerClass", avgFields);
metrics.put("averageOverriddenMethods", avgOverride);
metrics.put("ABC_metric", Map.of(
"A", totalA,
"B", totalB,
"C", totalC,
"value", abcMetric
));
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
writer.writeValue(new File("results.json"), metrics);
}
}