-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathClassPathScanner.java
More file actions
228 lines (197 loc) · 5.98 KB
/
ClassPathScanner.java
File metadata and controls
228 lines (197 loc) · 5.98 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package javasabr.rlib.classpath;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.function.Predicate;
import javasabr.rlib.classpath.impl.ClassPathScannerImpl;
import javasabr.rlib.collections.array.Array;
import javasabr.rlib.collections.array.MutableArray;
import org.jspecify.annotations.Nullable;
/**
* Scanner for discovering classes and resources on the classpath.
*
* @since 10.0.0
*/
public interface ClassPathScanner {
/**
* The JAR file extension.
*/
String JAR_EXTENSION = ".jar";
/**
* The Java source file extension.
*/
String SOURCE_EXTENSION = ".java";
/**
* The compiled class file extension.
*/
String CLASS_EXTENSION = ".class";
/**
* A null scanner constant.
*/
@Nullable ClassPathScanner NULL_SCANNER = null;
/**
* An empty scanner that performs no operations.
*/
ClassPathScanner EMPTY_SCANNER = new ClassPathScannerImpl(ClassPathScanner.class.getClassLoader()) {
@Override
public void addClasses(Array<Class<?>> classes) {}
@Override
public void addAdditionalPath(String path) {}
@Override
public void addAdditionalPaths(String[] paths) {}
@Override
public void addResources(Array<String> resources) {}
@Override
public void scan(Predicate<String> filter) {}
};
/**
* An empty class loader with no URLs.
*/
URLClassLoader EMPTY_CLASS_LOADER = new URLClassLoader(new URL[0], ClassPathScanner.class.getClassLoader());
/**
* A null class loader constant.
*/
@Nullable URLClassLoader NULL_CLASS_LOADER = null;
/**
* Adds discovered classes to the specified collection.
*
* @param classes the collection to add classes to
* @since 10.0.0
*/
void addClasses(Array<Class<?>> classes);
/**
* Adds discovered resources to the specified collection.
*
* @param resources the collection to add resources to
* @since 10.0.0
*/
void addResources(Array<String> resources);
/**
* Sets whether to include the system classpath in scanning.
*
* @param useSystemClasspath true to include system classpath
* @since 10.0.0
*/
void useSystemClassPath(boolean useSystemClasspath);
/**
* Finds all implementations of the specified interface.
*
* @param <T> the type of the interface
* @param interfaceClass the interface class to find implementations for
* @return an array of implementing classes
* @since 10.0.0
*/
default <T> Array<Class<T>> findImplementations(Class<T> interfaceClass) {
MutableArray<Class<T>> result = MutableArray.ofType(Class.class);
findImplementationsTo(result, interfaceClass);
return result;
}
/**
* Finds all implementations of the specified interface and adds them to the container.
*
* @param <T> the type of the interface
* @param container the container to add implementations to
* @param interfaceClass the interface class to find implementations for
* @since 10.0.0
*/
<T> void findImplementationsTo(MutableArray<Class<T>> container, Class<T> interfaceClass);
/**
* Finds all classes that inherit from the specified parent class.
*
* @param <T> the type of the parent class
* @param parentClass the parent class to find subclasses for
* @return an array of subclasses
* @since 10.0.0
*/
default <T> Array<Class<T>> findInherited(Class<T> parentClass) {
MutableArray<Class<T>> result = MutableArray.ofType(Class.class);
findInheritedTo(result, parentClass);
return result;
}
/**
* Finds all classes that inherit from the specified parent class and adds them to the container.
*
* @param <T> the type of the parent class
* @param container the container to add subclasses to
* @param parentClass the parent class to find subclasses for
* @since 10.0.0
*/
<T> void findInheritedTo(MutableArray<Class<T>> container, Class<T> parentClass);
/**
* Finds all classes annotated with the specified annotation.
*
* @param annotationClass the annotation class to search for
* @return an array of annotated classes
* @since 10.0.0
*/
default Array<Class<?>> findAnnotated(Class<? extends Annotation> annotationClass) {
MutableArray<Class<?>> result = MutableArray.ofType(Class.class);
findAnnotatedTo(result, annotationClass);
return result;
}
/**
* Finds all classes annotated with the specified annotation and adds them to the container.
*
* @param container the container to add annotated classes to
* @param annotationClass the annotation class to search for
* @since 10.0.0
*/
void findAnnotatedTo(MutableArray<Class<?>> container, Class<? extends Annotation> annotationClass);
/**
* Adds all found classes to the specified container.
*
* @param container the container to add classes to
* @since 10.0.0
*/
void foundClassesTo(MutableArray<Class<?>> container);
/**
* Adds all found resources to the specified container.
*
* @param container the container to add resources to
* @since 10.0.0
*/
void foundResourcesTo(MutableArray<String> container);
/**
* Returns all classes found during scanning.
*
* @return an array of found classes
* @since 10.0.0
*/
Array<Class<?>> foundClasses();
/**
* Returns all resources found during scanning.
*
* @return an array of found resource paths
* @since 10.0.0
*/
Array<String> foundResources();
/**
* Scans the classpath without any filter.
*
* @since 10.0.0
*/
default void scan() {
scan(null);
}
/**
* Scans the classpath with the specified filter.
*
* @param filter the filter to apply during scanning, or null for no filter
* @since 10.0.0
*/
void scan(@Nullable Predicate<String> filter);
/**
* Adds an additional path to scan.
*
* @param path the path to add
* @since 10.0.0
*/
void addAdditionalPath(String path);
/**
* Adds additional paths to scan.
*
* @param paths the paths to add
* @since 10.0.0
*/
void addAdditionalPaths(String[] paths);
}