Repository navigation
Expand file tree
/
Copy pathMetricsCollector.java
More file actions
77 lines (59 loc) · 2.6 KB
/
Copy pathMetricsCollector.java
File metadata and controls
77 lines (59 loc) · 2.6 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
package org.example.analyzer;
import org.example.model.ClassInfo;
import org.example.model.Metrics;
import org.example.model.MethodInfo;
import org.example.visitor.*;
import org.objectweb.asm.ClassReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class MetricsCollector {
private final Map<String, ClassInfo> classInfoMap;
private final InheritanceVisitor inheritanceVisitor;
public MetricsCollector() {
this.classInfoMap = new HashMap<>();
this.inheritanceVisitor = new InheritanceVisitor(classInfoMap);
}
public void processClass(InputStream classStream) throws IOException {
ClassReader classReader = new ClassReader(classStream);
String className = classReader.getClassName();
ClassInfo classInfo = classInfoMap.computeIfAbsent(className,
k -> new ClassInfo(className, classReader.getSuperName()));
ClassMetricsVisitor metricsVisitor = new ClassMetricsVisitor(classInfo, classInfoMap);
classReader.accept(metricsVisitor, 0);
classInfo.setOverriddenMethodsCount(metricsVisitor.getOverriddenMethodsCount());
}
public Metrics calculateMetrics() {
Metrics metrics = new Metrics();
if (classInfoMap.isEmpty()) {
return metrics;
}
int totalInheritanceDepth = 0;
int maxInheritanceDepth = 0;
int totalAbcMetric = 0;
int totalFields = 0;
int totalOverriddenMethods = 0;
int classCount = classInfoMap.size();
for (ClassInfo classInfo : classInfoMap.values()) {
int depth = inheritanceVisitor.calculateInheritanceDepth(classInfo.getName());
classInfo.setInheritanceDepth(depth);
totalInheritanceDepth += depth;
maxInheritanceDepth = Math.max(maxInheritanceDepth, depth);
for (MethodInfo method : classInfo.getMethods()) {
totalAbcMetric += method.getAssignmentCount();
}
totalFields += classInfo.getFieldCount();
totalOverriddenMethods += classInfo.getOverriddenMethodsCount();
}
metrics.setMaxInheritanceDepth(maxInheritanceDepth);
metrics.setAvgInheritanceDepth((double) totalInheritanceDepth / classCount);
metrics.setTotalAbcMetric(totalAbcMetric);
metrics.setAvgOverriddenMethods((double) totalOverriddenMethods / classCount);
metrics.setAvgFieldsPerClass((double) totalFields / classCount);
return metrics;
}
public Map<String, ClassInfo> getClassInfoMap() {
return classInfoMap;
}
}