|
1 | 1 | package org.example; |
2 | 2 |
|
3 | | -import org.example.visitor.ClassPrinter; |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import org.example.dto.Result; |
| 7 | +import org.example.model.JarMetrics; |
| 8 | +import org.example.visitor.CustomClassVisitor; |
4 | 9 | import org.objectweb.asm.ClassReader; |
5 | 10 |
|
6 | 11 | import java.io.IOException; |
7 | | -import java.util.Enumeration; |
| 12 | +import java.nio.file.Paths; |
8 | 13 | import java.util.jar.JarEntry; |
9 | 14 | import java.util.jar.JarFile; |
10 | 15 |
|
| 16 | +@Slf4j |
11 | 17 | public class Example { |
12 | 18 |
|
13 | 19 | public static void main(String[] args) throws IOException { |
14 | | -// var printer = new ByteCodePrinter(); |
15 | | -// printer.printBubbleSortBytecode(); |
16 | | - try (JarFile sampleJar = new JarFile("src/main/resources/sample.jar")) { |
17 | | - Enumeration<JarEntry> enumeration = sampleJar.entries(); |
18 | | - |
19 | | - while (enumeration.hasMoreElements()) { |
20 | | - JarEntry entry = enumeration.nextElement(); |
21 | | - if (entry.getName().endsWith(".class")) { |
22 | | - ClassPrinter cp = new ClassPrinter(); |
23 | | - ClassReader cr = new ClassReader(sampleJar.getInputStream(entry)); |
24 | | - cr.accept(cp, 0); |
25 | | - } |
26 | | - } |
| 20 | + if (args.length != 2) { |
| 21 | + log.error("Ошибка чтения аргументов"); |
| 22 | + System.exit(1); |
27 | 23 | } |
| 24 | + |
| 25 | + var jarPath = args[0]; |
| 26 | + var outputFileName = args[1]; |
| 27 | + |
| 28 | + var result = processJarFile(jarPath); |
| 29 | + writeResultAsJson(result, outputFileName); |
| 30 | + } |
| 31 | + |
| 32 | + private static Result processJarFile(String jarFilePath) throws IOException { |
| 33 | + var jarMetrics = new JarMetrics(); |
| 34 | + |
| 35 | + try (var jarFile = new JarFile(jarFilePath)) { |
| 36 | + jarFile.stream() |
| 37 | + .filter(e -> e.getName().endsWith(".class")) |
| 38 | + .forEach(entry -> processClass(entry, jarFile, jarMetrics)); |
| 39 | + } |
| 40 | + |
| 41 | + jarMetrics.calculateMetrics(); |
| 42 | + |
| 43 | + var stringBuilder = new StringBuilder() |
| 44 | + .append("Максимальная глубина наследования: ").append(jarMetrics.getMaxDepthOfInheritance()).append('\n') |
| 45 | + .append("Средняя глубина наследования: ").append(jarMetrics.getAverageInheritanceDepth()).append('\n') |
| 46 | + .append("Метрика ABC: ").append(jarMetrics.getAverageAbcScore()).append('\n') |
| 47 | + .append("Сколичество переопределенных методов: ").append(jarMetrics.getAverageOverriddenMethods()).append('\n') |
| 48 | + .append("Среднее количество полей в классе: ").append(jarMetrics.getAverageFieldCount()); |
| 49 | + |
| 50 | + log.info(stringBuilder.toString()); |
| 51 | + |
| 52 | + return buildFinalResult(jarMetrics); |
| 53 | + } |
| 54 | + |
| 55 | + private static void processClass(JarEntry entry, JarFile jarFile, JarMetrics metrics) { |
| 56 | + try { |
| 57 | + var reader = new ClassReader(jarFile.getInputStream(entry)); |
| 58 | + reader.accept(new CustomClassVisitor(metrics), 0); |
| 59 | + } catch (IOException e) { |
| 60 | + throw new RuntimeException("Ошибка чтения файла. Причина: " + entry.getName(), e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static Result buildFinalResult(JarMetrics metrics) { |
| 65 | + return Result.builder() |
| 66 | + .maxDepthOfInheritance(metrics.getMaxDepthOfInheritance()) |
| 67 | + .averageInheritanceDepth(metrics.getAverageInheritanceDepth()) |
| 68 | + .averageOverriddenMethods(metrics.getAverageOverriddenMethods()) |
| 69 | + .averageFieldCount(metrics.getAverageFieldCount()) |
| 70 | + .averageAbcScore(metrics.getAverageAbcScore()) |
| 71 | + .build(); |
| 72 | + } |
| 73 | + |
| 74 | + private static void writeResultAsJson(Object result, String outputFileName) throws IOException { |
| 75 | + var mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); |
| 76 | + |
| 77 | + var file = Paths.get(outputFileName).toFile(); |
| 78 | + |
| 79 | + if (file.getParentFile() != null && !file.getParentFile().exists()) { |
| 80 | + file.getParentFile().mkdirs(); |
| 81 | + } |
| 82 | + |
| 83 | + if (!file.exists()) { |
| 84 | + file.createNewFile(); |
| 85 | + } |
| 86 | + |
| 87 | + mapper.writeValue(file, result); |
| 88 | + |
| 89 | + log.info("Метрики сохранены в файл: {}", file.getAbsolutePath()); |
28 | 90 | } |
29 | 91 | } |
0 commit comments