-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMain.java
More file actions
67 lines (55 loc) · 2.87 KB
/
Copy pathMain.java
File metadata and controls
67 lines (55 loc) · 2.87 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
package org.example;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.configuration.ObjectMapperConfiguration;
import org.example.model.AnalyzeResult;
import org.example.model.JarFileMetrics;
import org.example.service.ClassAnalyzerService;
import org.example.service.util.ObjectMapperUtil;
import org.objectweb.asm.ClassReader;
import java.io.IOException;
import java.util.jar.JarFile;
@Slf4j
public class Main {
public static void main(String[] args) {
var jarPath = "src/main/resources/simple.jar";
if (args.length != 1) {
log.error("Wrong arguments. First should be a jar file to analyze");
System.exit(1);
}
var objectMapper = ObjectMapperConfiguration.getObjectMapper();
jarPath = args[0];
var finalResult = new AnalyzeResult();
processJarFile(jarPath, finalResult, objectMapper);
log.info("Final result is: {}", objectMapper.writeValueAsString(finalResult));
}
@SneakyThrows
private static void processJarFile(String jarFilePath, AnalyzeResult finalResult, ObjectMapperUtil objectMapperUtil) {
var jarMetrics = new JarFileMetrics();
try (var jarFile = new JarFile(jarFilePath)) {
jarFile.stream()
.filter(it -> it.getName().endsWith(".class"))
.forEach(it -> {
ClassReader reader;
try {
reader = new ClassReader(jarFile.getInputStream(it));
} catch (IOException e) {
throw new RuntimeException(e);
}
reader.accept(new ClassAnalyzerService(jarMetrics), 0);
});
}
jarMetrics.calculateMetrics();
log.info("Метрики по jar архиву: {}", objectMapperUtil.writeValueAsString(jarMetrics));
log.info("Максимальная глубина наследования: {}", jarMetrics.getMaxInheritanceDepth());
log.info("Средняя глубина наследования: {}", jarMetrics.getAvgInheritanceDepth());
log.info("Средняя метрика ABC: {}", jarMetrics.getAvgAbcMetric());
log.info("Среднее количество переопределенных методов: {}", jarMetrics.getAvgOverriddenMethods());
log.info("Среднее количество полей в классе: {}", jarMetrics.getAvgFieldCount());
finalResult.setMaxInheritanceDepth(jarMetrics.getMaxInheritanceDepth());
finalResult.setAverageExtendsLength(jarMetrics.getAvgInheritanceDepth());
finalResult.setAverageOverridesMethods(jarMetrics.getAvgOverriddenMethods());
finalResult.setAverageFieldsInClass(jarMetrics.getAvgFieldCount());
finalResult.setAverageAbcMetric(jarMetrics.getAvgAbcMetric());
}
}