0
# Legacy API
1
2
Backward-compatible methods providing the same functionality as earlier versions of Reflections for easy migration and compatibility. These methods offer a simpler, more direct approach to querying metadata without the functional composition capabilities.
3
4
## Capabilities
5
6
### Type Query Methods
7
8
Legacy methods for querying type hierarchies and annotated types.
9
10
```java { .api }
11
/**
12
* Legacy type query methods in Reflections class
13
*/
14
class Reflections {
15
/** Get all subtypes of specified type */
16
<T> Set<Class<? extends T>> getSubTypesOf(Class<T> type);
17
18
/** Get types annotated with specific annotation class */
19
Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation);
20
21
/** Get types annotated with specific annotation class, optionally honoring @Inherited */
22
Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation, boolean honorInherited);
23
24
/** Get types annotated with specific annotation instance (matches annotation values) */
25
Set<Class<?>> getTypesAnnotatedWith(Annotation annotation);
26
27
/** Get types annotated with specific annotation instance, optionally honoring @Inherited */
28
Set<Class<?>> getTypesAnnotatedWith(Annotation annotation, boolean honorInherited);
29
}
30
```
31
32
### Method Query Methods
33
34
Legacy methods for querying methods with annotations, signatures, parameters, and return types.
35
36
```java { .api }
37
/**
38
* Legacy method query methods in Reflections class
39
*/
40
class Reflections {
41
/** Get methods annotated with specific annotation class */
42
Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation);
43
44
/** Get methods annotated with specific annotation instance (matches annotation values) */
45
Set<Method> getMethodsAnnotatedWith(Annotation annotation);
46
47
/** Get methods with specific parameter signature */
48
Set<Method> getMethodsWithSignature(Class<?>... types);
49
50
/** Get methods that have parameters of specified type */
51
Set<Method> getMethodsWithParameter(AnnotatedElement type);
52
53
/** Get methods with specific return type */
54
Set<Method> getMethodsReturn(Class<?> type);
55
}
56
```
57
58
### Constructor Query Methods
59
60
Legacy methods for querying constructors with annotations, signatures, and parameters.
61
62
```java { .api }
63
/**
64
* Legacy constructor query methods in Reflections class
65
*/
66
class Reflections {
67
/** Get constructors annotated with specific annotation class */
68
Set<Constructor> getConstructorsAnnotatedWith(Class<? extends Annotation> annotation);
69
70
/** Get constructors annotated with specific annotation instance (matches annotation values) */
71
Set<Constructor> getConstructorsAnnotatedWith(Annotation annotation);
72
73
/** Get constructors with specific parameter signature */
74
Set<Constructor> getConstructorsWithSignature(Class<?>... types);
75
76
/** Get constructors that have parameters of specified type */
77
Set<Constructor> getConstructorsWithParameter(AnnotatedElement type);
78
}
79
```
80
81
### Field Query Methods
82
83
Legacy methods for querying fields with annotations.
84
85
```java { .api }
86
/**
87
* Legacy field query methods in Reflections class
88
*/
89
class Reflections {
90
/** Get fields annotated with specific annotation class */
91
Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation);
92
93
/** Get fields annotated with specific annotation instance (matches annotation values) */
94
Set<Field> getFieldsAnnotatedWith(Annotation annotation);
95
}
96
```
97
98
### Resource Query Methods
99
100
Legacy methods for querying classpath resources.
101
102
```java { .api }
103
/**
104
* Legacy resource query methods in Reflections class
105
*/
106
class Reflections {
107
/** Get resources matching regex pattern string */
108
Set<String> getResources(String pattern);
109
110
/** Get resources matching compiled regex pattern */
111
Set<String> getResources(Pattern pattern);
112
}
113
```
114
115
### ReflectionUtils Legacy Methods
116
117
Legacy static methods in ReflectionUtils for direct reflection operations (marked for removal in future versions).
118
119
```java { .api }
120
/**
121
* Legacy methods in ReflectionUtils (marked for removal)
122
*/
123
abstract class ReflectionUtils {
124
/** @Deprecated Get all annotations of type with predicates */
125
@Deprecated static <T extends AnnotatedElement> Set<Annotation> getAllAnnotations(T type, Predicate<Annotation>... predicates);
126
127
/** @Deprecated Get all super types with predicates */
128
@Deprecated static Set<Class<?>> getAllSuperTypes(Class<?> type, Predicate<? super Class<?>>... predicates);
129
130
/** @Deprecated Get super types without predicates */
131
@Deprecated static Set<Class<?>> getSuperTypes(Class<?> type);
132
133
/** @Deprecated Get all methods with predicates */
134
@Deprecated static Set<Method> getAllMethods(Class<?> type, Predicate<? super Method>... predicates);
135
136
/** @Deprecated Get methods with predicates */
137
@Deprecated static Set<Method> getMethods(Class<?> type, Predicate<? super Method>... predicates);
138
139
/** @Deprecated Get all constructors with predicates */
140
@Deprecated static Set<Constructor> getAllConstructors(Class<?> type, Predicate<? super Constructor>... predicates);
141
142
/** @Deprecated Get constructors with predicates */
143
@Deprecated static Set<Constructor> getConstructors(Class<?> type, Predicate<? super Constructor>... predicates);
144
145
/** @Deprecated Get all fields with predicates */
146
@Deprecated static Set<Field> getAllFields(Class<?> type, Predicate<? super Field>... predicates);
147
148
/** @Deprecated Get fields with predicates */
149
@Deprecated static Set<Field> getFields(Class<?> type, Predicate<? super Field>... predicates);
150
151
/** @Deprecated Get annotations with predicates */
152
@Deprecated static <T extends AnnotatedElement> Set<Annotation> getAnnotations(T type, Predicate<Annotation>... predicates);
153
}
154
```
155
156
## Usage Examples
157
158
### Basic Type Queries
159
160
```java
161
import org.reflections.Reflections;
162
import java.util.Set;
163
164
Reflections reflections = new Reflections("com.mycompany");
165
166
// Get subtypes
167
Set<Class<? extends Service>> services = reflections.getSubTypesOf(Service.class);
168
Set<Class<? extends Repository>> repositories = reflections.getSubTypesOf(Repository.class);
169
Set<Class<? extends Controller>> controllers = reflections.getSubTypesOf(Controller.class);
170
171
// Get annotated types
172
Set<Class<?>> entities = reflections.getTypesAnnotatedWith(Entity.class);
173
Set<Class<?>> components = reflections.getTypesAnnotatedWith(Component.class);
174
Set<Class<?>> configurations = reflections.getTypesAnnotatedWith(Configuration.class);
175
176
// Honor inheritance for annotations
177
Set<Class<?>> inheritedServices = reflections.getTypesAnnotatedWith(Service.class, true);
178
Set<Class<?>> nonInheritedServices = reflections.getTypesAnnotatedWith(Service.class, false);
179
```
180
181
### Annotation Value Matching
182
183
```java
184
// Match specific annotation values
185
@RequestMapping(value = "/api", method = RequestMethod.GET)
186
class ApiController { }
187
188
// Create annotation instance for matching
189
RequestMapping requestMappingAnnotation = ApiController.class.getAnnotation(RequestMapping.class);
190
191
// Find types with matching annotation values
192
Set<Class<?>> apiControllers = reflections.getTypesAnnotatedWith(requestMappingAnnotation);
193
194
// This will only return classes with @RequestMapping(value = "/api", method = RequestMethod.GET)
195
```
196
197
### Method Queries
198
199
```java
200
// Get methods by annotation
201
Set<Method> requestMethods = reflections.getMethodsAnnotatedWith(RequestMapping.class);
202
Set<Method> autowiredMethods = reflections.getMethodsAnnotatedWith(Autowired.class);
203
Set<Method> transactionalMethods = reflections.getMethodsAnnotatedWith(Transactional.class);
204
205
// Get methods by signature
206
Set<Method> stringMethods = reflections.getMethodsWithSignature(String.class);
207
Set<Method> stringIntMethods = reflections.getMethodsWithSignature(String.class, int.class);
208
Set<Method> noParamMethods = reflections.getMethodsWithSignature();
209
210
// Get methods by parameter type
211
Set<Method> methodsWithStringParam = reflections.getMethodsWithParameter(String.class);
212
Set<Method> methodsWithUserParam = reflections.getMethodsWithParameter(User.class);
213
214
// Get methods by return type
215
Set<Method> voidMethods = reflections.getMethodsReturn(void.class);
216
Set<Method> stringReturningMethods = reflections.getMethodsReturn(String.class);
217
Set<Method> userReturningMethods = reflections.getMethodsReturn(User.class);
218
```
219
220
### Constructor Queries
221
222
```java
223
// Get constructors by annotation
224
Set<Constructor> injectConstructors = reflections.getConstructorsAnnotatedWith(Inject.class);
225
Set<Constructor> autowiredConstructors = reflections.getConstructorsAnnotatedWith(Autowired.class);
226
227
// Get constructors by signature
228
Set<Constructor> defaultConstructors = reflections.getConstructorsWithSignature();
229
Set<Constructor> stringConstructors = reflections.getConstructorsWithSignature(String.class);
230
Set<Constructor> stringIntConstructors = reflections.getConstructorsWithSignature(String.class, int.class);
231
232
// Get constructors by parameter type
233
Set<Constructor> constructorsWithStringParam = reflections.getConstructorsWithParameter(String.class);
234
Set<Constructor> constructorsWithUserParam = reflections.getConstructorsWithParameter(User.class);
235
```
236
237
### Field Queries
238
239
```java
240
// Get fields by annotation
241
Set<Field> autowiredFields = reflections.getFieldsAnnotatedWith(Autowired.class);
242
Set<Field> valueFields = reflections.getFieldsAnnotatedWith(Value.class);
243
Set<Field> resourceFields = reflections.getFieldsAnnotatedWith(Resource.class);
244
245
// Match specific field annotation values
246
@Value("${app.version}")
247
private String version;
248
249
Value valueAnnotation = MyClass.class.getDeclaredField("version").getAnnotation(Value.class);
250
Set<Field> appVersionFields = reflections.getFieldsAnnotatedWith(valueAnnotation);
251
```
252
253
### Resource Queries
254
255
```java
256
import java.util.regex.Pattern;
257
258
// Get resources by string pattern
259
Set<String> xmlFiles = reflections.getResources(".*\\.xml");
260
Set<String> propertyFiles = reflections.getResources(".*\\.properties");
261
Set<String> jsonFiles = reflections.getResources(".*\\.json");
262
Set<String> configFiles = reflections.getResources(".*config.*");
263
264
// Get resources by compiled pattern
265
Pattern xmlPattern = Pattern.compile(".*\\.xml");
266
Set<String> xmlResources = reflections.getResources(xmlPattern);
267
268
Pattern configPattern = Pattern.compile(".*(config|configuration).*", Pattern.CASE_INSENSITIVE);
269
Set<String> configResources = reflections.getResources(configPattern);
270
```
271
272
### Legacy ReflectionUtils Usage
273
274
```java
275
import org.reflections.ReflectionUtils;
276
import static org.reflections.ReflectionUtils.*;
277
278
// Get super types (deprecated but still available)
279
Set<Class<?>> superTypes = ReflectionUtils.getAllSuperTypes(MyClass.class);
280
Set<Class<?>> filteredSuperTypes = ReflectionUtils.getAllSuperTypes(MyClass.class,
281
clazz -> !clazz.equals(Object.class));
282
283
// Get methods (deprecated but still available)
284
Set<Method> allMethods = ReflectionUtils.getAllMethods(MyClass.class);
285
Set<Method> publicMethods = ReflectionUtils.getAllMethods(MyClass.class,
286
withPublic());
287
Set<Method> getters = ReflectionUtils.getAllMethods(MyClass.class,
288
withPrefix("get"), withParametersCount(0));
289
290
// Get fields (deprecated but still available)
291
Set<Field> allFields = ReflectionUtils.getAllFields(MyClass.class);
292
Set<Field> privateFields = ReflectionUtils.getAllFields(MyClass.class,
293
field -> Modifier.isPrivate(field.getModifiers()));
294
295
// Get constructors (deprecated but still available)
296
Set<Constructor> allConstructors = ReflectionUtils.getAllConstructors(MyClass.class);
297
Set<Constructor> publicConstructors = ReflectionUtils.getAllConstructors(MyClass.class,
298
withPublic());
299
```
300
301
### Migration from Legacy to Modern API
302
303
```java
304
// Legacy approach
305
Set<Class<? extends Service>> legacyServices = reflections.getSubTypesOf(Service.class);
306
Set<Method> legacyMethods = reflections.getMethodsAnnotatedWith(RequestMapping.class);
307
308
// Modern functional approach (equivalent)
309
Set<Class<?>> modernServices = reflections.get(SubTypes.of(Service.class).asClass());
310
Set<Method> modernMethods = reflections.get(MethodsAnnotated.with(RequestMapping.class).as(Method.class));
311
312
// Legacy with additional processing
313
Set<Class<? extends Service>> filteredLegacyServices = legacyServices.stream()
314
.filter(clazz -> !Modifier.isAbstract(clazz.getModifiers()))
315
.collect(Collectors.toSet());
316
317
// Modern with built-in filtering
318
Set<Class<?>> filteredModernServices = reflections.get(
319
SubTypes.of(Service.class)
320
.asClass()
321
.filter(clazz -> !Modifier.isAbstract(clazz.getModifiers())));
322
```
323
324
### Combining Legacy and Modern APIs
325
326
```java
327
// You can mix legacy and modern approaches
328
Reflections reflections = new Reflections("com.mycompany");
329
330
// Use legacy for simple queries
331
Set<Class<? extends Service>> services = reflections.getSubTypesOf(Service.class);
332
333
// Use modern for complex queries
334
Set<Method> complexMethods = reflections.get(
335
MethodsAnnotated.with(RequestMapping.class)
336
.as(Method.class)
337
.filter(method -> method.getParameterCount() > 0)
338
.filter(method -> !method.getName().startsWith("get")));
339
340
// Combine results
341
Set<String> allServiceMethods = services.stream()
342
.flatMap(clazz -> Arrays.stream(clazz.getDeclaredMethods()))
343
.map(Method::getName)
344
.collect(Collectors.toSet());
345
```
346
347
### Error Handling in Legacy API
348
349
```java
350
// Legacy methods may return empty sets but won't throw exceptions for missing types
351
Set<Class<? extends NonExistentInterface>> empty = reflections.getSubTypesOf(NonExistentInterface.class);
352
// empty.isEmpty() will be true
353
354
// Handle annotation classes that might not be present
355
try {
356
Class<? extends Annotation> annotationClass = (Class<? extends Annotation>)
357
Class.forName("com.example.MyAnnotation");
358
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(annotationClass);
359
} catch (ClassNotFoundException e) {
360
// Handle missing annotation class
361
Set<Class<?>> annotated = Collections.emptySet();
362
}
363
364
// Resource queries with invalid patterns
365
try {
366
Set<String> resources = reflections.getResources("[invalid regex");
367
} catch (PatternSyntaxException e) {
368
// Handle invalid regex pattern
369
Set<String> resources = Collections.emptySet();
370
}
371
```
372
373
## Legacy API Best Practices
374
375
### When to Use Legacy API
376
- **Simple queries**: When you need straightforward results without filtering or transformation
377
- **Migration**: When upgrading existing code that uses older Reflections versions
378
- **Compatibility**: When working with codebases that rely on the legacy API patterns
379
- **Learning curve**: When team members are more familiar with the direct method approach
380
381
### When to Use Modern API
382
- **Complex queries**: When you need filtering, mapping, or composition of results
383
- **Performance**: When you want to chain operations without intermediate collections
384
- **Type safety**: When you want better compile-time type checking
385
- **Functional style**: When working with modern functional programming patterns
386
387
### Migration Strategy
388
1. **Start with legacy methods** for basic functionality
389
2. **Gradually introduce modern API** for new complex queries
390
3. **Refactor legacy calls** to modern API when adding new filtering or processing
391
4. **Use both approaches** in the same application during transition period
392
393
## Types
394
395
```java { .api }
396
/**
397
* Common predicate utilities for legacy API filtering
398
*/
399
abstract class ReflectionUtilsPredicates {
400
/** Filter for public members */
401
static Predicate<Member> withPublic();
402
403
/** Filter by parameter count */
404
static Predicate<Member> withParametersCount(int count);
405
406
/** Filter by method name prefix */
407
static <T extends Member> Predicate<T> withPrefix(String prefix);
408
409
/** Filter by annotation presence */
410
static <T extends AnnotatedElement> Predicate<T> withAnnotation(Class<? extends Annotation> annotation);
411
412
/** Filter by method parameters */
413
static Predicate<Method> withParameters(Class<?>... types);
414
415
/** Filter by method return type */
416
static Predicate<Method> withReturnType(Class<?> type);
417
}
418
```