-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassScanner.java
More file actions
122 lines (98 loc) · 3.23 KB
/
Copy pathClassScanner.java
File metadata and controls
122 lines (98 loc) · 3.23 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
package ru.cwcode.cwutils.reflection;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class ClassScanner {
File jar;
List<ClassApplier> classAppliers = new ArrayList<>();
List<MethodApplier> methodAppliers = new ArrayList<>();
List<FieldApplier> fieldAppliers = new ArrayList<>();
Predicate<String> filter = filter -> true;
public ClassScanner(File jar) {
this.jar = jar;
}
public ClassScanner apply(ClassApplier applier) {
classAppliers.add(applier);
return this;
}
public ClassScanner apply(MethodApplier applier) {
methodAppliers.add(applier);
return this;
}
public ClassScanner apply(FieldApplier applier) {
fieldAppliers.add(applier);
return this;
}
public ClassScanner classFilter(Predicate<String> filter) {
this.filter = filter;
return this;
}
public void scan(JavaPlugin plugin) {
if (classAppliers.isEmpty() && methodAppliers.isEmpty() && fieldAppliers.isEmpty()) return;
String packageName = plugin.getClass().getPackage().getName();
int i = 0;
long start = System.currentTimeMillis();
try {
for (var clazz : ReflectionUtils.getClasses(jar, packageName, filter)) {
i++;
handle(clazz);
}
plugin.getLogger().info("Scanned " + i + " classes, took " + (System.currentTimeMillis() - start) + "ms");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
classAppliers.clear();
methodAppliers.clear();
fieldAppliers.clear();
}
private void handle(Class<?> classInfo) throws ClassNotFoundException {
for (ClassApplier classApplier : classAppliers) {
if (classApplier.predicate.test(classInfo)) {
classApplier.consumer.accept(classInfo);
}
}
for (Method method : classInfo.getDeclaredMethods()) {
for (MethodApplier x : methodAppliers) {
if (x.predicate.test(method)) {
x.consumer.accept(method);
}
}
}
for (Field field : classInfo.getDeclaredFields()) {
for (FieldApplier x : fieldAppliers) {
if (x.predicate.test(field)) {
x.consumer.accept(field);
}
}
}
}
public static class ClassApplier {
private final Predicate<Class<?>> predicate;
private final Consumer<Class<?>> consumer;
public ClassApplier(Predicate<Class<?>> predicate, Consumer<Class<?>> consumer) {
this.predicate = predicate;
this.consumer = consumer;
}
}
public static class MethodApplier {
private final Predicate<Method> predicate;
private final Consumer<Method> consumer;
public MethodApplier(Predicate<Method> predicate, Consumer<Method> consumer) {
this.predicate = predicate;
this.consumer = consumer;
}
}
public static class FieldApplier {
private final Predicate<Field> predicate;
private final Consumer<Field> consumer;
public FieldApplier(Predicate<Field> predicate, Consumer<Field> consumer) {
this.predicate = predicate;
this.consumer = consumer;
}
}
}