-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodScanner.java
More file actions
78 lines (60 loc) · 2.79 KB
/
MethodScanner.java
File metadata and controls
78 lines (60 loc) · 2.79 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
import io.github.classgraph.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MethodScanner {
public static void main(String[] args) throws IOException {
if (args.length < 1 || args.length > 2) {
System.out.println("Usage: java MethodLister <path_to_target_classes> [output_file_path]");
return;
}
File classesDir = new File(args[0]);
if (!classesDir.exists() || !classesDir.isDirectory()) {
System.out.println("Invalid directory: " + args[0]);
return;
}
File outputFile;
if (args.length == 2) {
outputFile = new File(args[1]);
} else {
// Use parent of 'target/classes' and name the file 'fully-qualified-methods.txt'
File parentDir = classesDir.getParentFile(); // should be 'target'
if (parentDir != null && parentDir.getName().equals("target")) {
File projectRoot = parentDir.getParentFile();
outputFile = new File(projectRoot, "fully-qualified-methods.txt");
} else {
// fallback: use current working directory
outputFile = new File("fully-qualified-methods.txt");
}
}
outputFile.getParentFile().mkdirs();
try (PrintWriter writer = new PrintWriter(new FileWriter(outputFile))) {
try (ScanResult scanResult = new ClassGraph()
.overrideClasspath(classesDir)
.enableClassInfo()
.enableMethodInfo()
.scan()) {
for (ClassInfo classInfo : scanResult.getAllClasses()) {
for (MethodInfo methodInfo : classInfo.getDeclaredMethodInfo()) {
String className = classInfo.getName();
String methodName = methodInfo.getName();
List<String> paramTypes = Arrays.stream(methodInfo.getParameterInfo())
.map(p -> p.getTypeDescriptor().toString())
.collect(Collectors.toList());
String returnType = methodInfo.getTypeDescriptor().getResultType().toString();
String methodSignature = className + "." + methodName + "(" +
String.join(", ", paramTypes) + ") : " + returnType;
writer.println(methodSignature);
}
}
}
System.out.println("Method list saved to "+ outputFile);
} catch (Exception e) {
System.err.println("Failed to write output: " + e.getMessage());
}
}
}