-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMain.java
More file actions
75 lines (61 loc) · 2.41 KB
/
Copy pathMain.java
File metadata and controls
75 lines (61 loc) · 2.41 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
package org.example;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import org.example.analyzer.JarAnalyzer;
import org.example.analyzer.MetricsCalculator;
import org.example.model.ClassInfo;
import org.example.model.MetricsResult;
import org.example.output.ConsoleMetricsPrinter;
import org.example.output.JsonMetricsPrinter;
import org.example.output.MetricsPrinter;
public class Main {
public static void main(String[] args) {
String inputPath = null;
String outputPath = null;
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "--input" -> {
if (i + 1 < args.length) {
inputPath = args[++i];
}
}
case "--output" -> {
if (i + 1 < args.length) {
outputPath = args[++i];
}
}
default -> throw new IllegalArgumentException("Unknown option: " + args[i]);
}
}
Path jarPath = getValidJarPath(inputPath);
try {
analyzeJarInternal(jarPath, outputPath);
} catch (IOException e) {
throw new RuntimeException("Error analyzing jar " + jarPath + ": " + e.getMessage(), e);
}
}
private static Path getValidJarPath(String inputPath) {
if (inputPath == null) {
throw new IllegalArgumentException("JAR file is not provided");
}
Path jarPath = Path.of(inputPath);
if (!Files.exists(jarPath)) {
throw new IllegalArgumentException("JAR file does not exist: " + jarPath);
}
return jarPath;
}
private static void analyzeJarInternal(Path jarPath, String outputPath) throws IOException {
JarAnalyzer jarAnalyzer = new JarAnalyzer();
MetricsCalculator calculator = new MetricsCalculator();
Map<String, ClassInfo> classes = jarAnalyzer.getJarClassInfo(jarPath);
MetricsResult result = calculator.calculate(classes, jarPath.getFileName().toString());
MetricsPrinter consoleMetricsPrinter = new ConsoleMetricsPrinter();
consoleMetricsPrinter.print(result);
if (outputPath != null) {
MetricsPrinter jsonMetricsPrinter = new JsonMetricsPrinter(Path.of(outputPath));
jsonMetricsPrinter.print(result);
}
}
}