-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathJarMetrics.java
More file actions
230 lines (170 loc) · 7.48 KB
/
Copy pathJarMetrics.java
File metadata and controls
230 lines (170 loc) · 7.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.example;
import org.example.graph.Graph;
import org.example.graph.Node;
import org.example.visitor.ClassCalculateMetrics;
import org.objectweb.asm.ClassReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.json.JSONObject;
public class JarMetrics {
private String pathToJarFile;
private Node root;
private Map<String, Node> classMap;
private Integer countOfClass = 0;
private int a;
private int b;
private int c;
private int countOfFields;
private int countOfMethods;
int maxDepth;
double averageDepth;
int overrideMethods;
double averageOverrideMethods;
double averageCountOfFields;
private Graph graph = new Graph();
public JarMetrics(String pathToJarFile) {
this.pathToJarFile = pathToJarFile;
root = new Node("java/lang/Object", "", new HashSet<>(), List.of("hashCode()", "toString()", "equals(Ljava/lang/Object;)", "wait()", "notify()", "notifyAll()"));
classMap = new HashMap<>();
classMap.put(root.getClassName(), root);
}
public void getMetrics() {
try (JarFile sampleJar = new JarFile(pathToJarFile)) {
Enumeration<JarEntry> enumeration = sampleJar.entries();
while (enumeration.hasMoreElements()) {
JarEntry entry = enumeration.nextElement();
if (entry.getName().endsWith(".class")) {
ClassCalculateMetrics calculateMetrics = new ClassCalculateMetrics();
ClassReader cr = new ClassReader(sampleJar.getInputStream(entry));
cr.accept(calculateMetrics, 0);
countOfClass++;
a += calculateMetrics.getMetrics().getA();
b += calculateMetrics.getMetrics().getB();
c += calculateMetrics.getMetrics().getC();
countOfFields += calculateMetrics.getMetrics().getCountOfFields();
countOfMethods += calculateMetrics.getMetrics().getCountOfMethods();
Node classNode = new Node(calculateMetrics.getMetrics().getClassName(), calculateMetrics.getMetrics().getSuperClassName(), calculateMetrics.getMetrics().getNameOfInterfaces(), calculateMetrics.getMetrics().getNameOfMethods());
classMap.put(classNode.getClassName(), classNode);
}
}
createGraph();
// Запускаем dfs для подсчёта глубины наследования
graph.dfs(root, 0);
maxDepth = graph.getMaxDepth();
averageDepth = graph.averageDepth();
overrideMethods = calculateOverrideMethods();
averageOverrideMethods = (double) overrideMethods / countOfMethods;
averageCountOfFields = (double) countOfFields / countOfClass;
writeToJson(pathToJarFile, a, b, c, averageCountOfFields,
maxDepth, averageDepth, averageOverrideMethods, countOfMethods, overrideMethods);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void writeToJson(String jarFileName, double aMetric, double bMetric, double cMetric,
double fieldsAverage, double maxDepth, double averageDepth, double averageOverridenMethods,
int methods, int overridenMethods) {
JSONObject metricJson = new JSONObject();
metricJson.put("jarName", jarFileName);
metricJson.put("fieldsAverage", fieldsAverage);
metricJson.put("A", aMetric);
metricJson.put("B", bMetric);
metricJson.put("C", cMetric);
metricJson.put("ABC", Math.sqrt(aMetric * aMetric + bMetric * bMetric + cMetric * cMetric));
metricJson.put("maxInheritanceDepth", maxDepth);
metricJson.put("averageInheritanceDepth", averageDepth);
metricJson.put("averageOverridenMethods", averageOverridenMethods);
metricJson.put("methods", methods);
metricJson.put("overridenMethods", overridenMethods);
try {
File file = new File("metrics.json");
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
System.out.print(metricJson);
fileWriter.write(metricJson.toString(4));
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private int calculateOverrideMethods() {
// нужно посчитать переопределённые методы и у extends случаев и у implements
// superClass или интерфейсы
findAllSuperClasses();
findAllInterfaces();
return calculateOverrideMethodsInInterfaces() + calculateOverrideMethodsInSuperClasses();
}
private int calculateOverrideMethodsInSuperClasses() {
int result = 0;
for (Node node : classMap.values()) {
for (String method : node.getNameOfMethods()) {
for (Node parent : node.getSuperClasses()) {
if (parent != null && parent.getNameOfMethods().contains(method))
result++;
}
}
}
return result;
}
private int calculateOverrideMethodsInInterfaces() {
int result = 0;
for (Node node : classMap.values()) {
for (String method : node.getNameOfMethods()) {
for (String interfaceNode : node.getNameOfInterfaces()) {
Node parent = classMap.get(interfaceNode);
if (parent != null && parent.getNameOfMethods().contains(method))
result++;
}
}
}
return result;
}
private void findAllInterfaces() {
for (Node node : classMap.values()) {
findAllInterfaces(node, node);
}
}
private void findAllInterfaces(Node children, Node parent) {
Set<String> parentInterfaces = new HashSet<>(parent.getNameOfInterfaces());
for (String nameInterface : parentInterfaces) {
Node node = classMap.get(nameInterface);
if (node != null) {
children.getNameOfInterfaces().addAll(node.getNameOfInterfaces());
findAllInterfaces(children, node);
}
}
}
/**
* Здесь мы заполним всю цепочку наследования
*/
private void findAllSuperClasses() {
for (Node node : classMap.values()) {
node.getSuperClasses().add(classMap.get(node.getSuperClassName()));
findAllSuperClasses(node, node);
}
}
private void findAllSuperClasses(Node children, Node parent) {
Node nextParent = classMap.get(parent.getSuperClassName());
if (nextParent != null) {
children.getSuperClasses().add(nextParent);
findAllSuperClasses(children, nextParent);
}
}
private void createGraph() {
for (Node from : classMap.values()) {
Node to = classMap.get(from.getSuperClassName());
graph.addEdge(from, to);
}
for (Node from : classMap.values()) {
for (String interfacesName : from.getNameOfInterfaces()) {
Node to = classMap.get(interfacesName);
graph.addEdge(from, to);
}
}
}
}