ronmamo/reflections — 4.8k★ trên GitHub (Java). Java runtime metadata analysis
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
❗ Please note: Reflections library is currently NOT under active development or maintenance ❗
Thank you for your continuous support!
There are open issues and also workarounds. Release version will be considered incase contributing PR fixing the main issues.
Last released org.reflections:reflections:0.10.2 (Oct 2021)
Java runtime metadata analysis
Reflections scans and indexes your project's classpath metadata, allowing reverse transitive query of the type system on runtime.
Using Reflections you can query for example:
Subtypes of a type
Types annotated with an annotation
Methods with annotation, parameters, return type
Resources found in classpath
And more...
Reflections was written in the spirit of Scannotations library
Note that there are some breaking changes with Reflections 0.10+, along with performance improvements and more functional API, see below.
Scan
Creating Reflections instance requires ConfigurationBuilder, typically configured with packages and Scanners to use:
// typical usage: scan package with the default scanners SubTypes, TypesAnnotated
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.forPackage("com.my.project")
.filterInputsBy(new FilterBuilder().includePackage("com.my.project")));
Other examples:
import static org.reflections.scanners.Scanners.*;
// scan package with specific scanners
Reflections reflections = new Reflections(
new ConfigurationBuilder()
.forPackage("com.my.project")
.filterInputsBy(new FilterBuilder().includePackage("com.my.project").excludePackage("com.my.project.exclude"))
.setScanners(TypesAnnotated, MethodsAnnotated, MethodsReturn));
Note that:
Scanner must be configured in order to be queried, otherwise an empty result is returned
If not specified, default scanners will be used SubTypes, TypesAnnotated.
For all standard scanners use Scanners.values(). See more scanners in the source package.
All relevant URLs should be configured
Consider .filterInputsBy() in case too many classes are scanned.
If required, Reflections will expand super types in order to get the transitive closure metadata without scanning large 3rd party urls.
Classloader can optionally be used for resolving runtime classes from names.
Query
Once Reflections was instantiated and scanning was successful, it can be used for querying the indexed metadata.
(1): The equivalent of getTypesAnnotatedWith(A) is get(SubTypes.of(TypesAnnotated.with(A))), including SubTypes
(2): MethodsAnnotatedScanner does not include constructor annotation scanning, use instead Scanners.ConstructorsAnnotated
(3): MethodParameterScanner is obsolete, use instead as required:
Scanners.MethodsParameter, Scanners.MethodsSignature, Scanners.MethodsReturn, Scanners.ConstructorsParameter, Scanners.ConstructorsSignature
ReflectionUtils
Apart from scanning classpath metadata using Javassist,
Java Reflection convenient methods are available using
ReflectionsUtils:
Previous ReflectionUtils 0.9.x API is still supported though marked for removal, more info in the javadocs.
Query API
Each Scanner and ReflectionUtils function implements QueryBuilder, and supports:
get() - function returns direct values
with() or of() - function returns all transitive values
For example, Scanners.SubTypes.get(T) return direct subtypes,
while Scanners.SubTypes.of(T) return transitive subtypes hierarchy.
Same goes for Scanners.TypesAnnotated and ReflectionUtils.SuperTypes etc.
Next, each function implements QueryFunction,
and provides fluent functional interface for composing filter(), map(), flatMap(), as() and more, such that:
// filter, as/map
QueryFunction<Store, Method> getters =
Methods.of(C1.class)
.filter(withModifier(Modifier.PUBLIC))
.filter(withPrefix("get").and(withParametersCount(0)))
.as(Method.class);
// compose Scanners and ReflectionUtils functions
QueryFunction<Store, Method> methods =
SubTypes.of(type).asClass() // <-- classpath scanned metadata
.flatMap(Methods::of); // <-- java reflection api
// function of function
QueryFunction<Store, Class<? extends Annotation>> queryAnnotations =
Annotations.of(Methods.of(C4.class))
.map(Annotation::annotationType);
See more in ReflectionUtilsQueryTest
A more complex example demonstrates getting merged annotations of rest controllers endpoints:
// get all annotations of RequestMapping hierarchy (GetMapping, PostMapping, ...)
Set<Class<?>> metaAnnotations =
reflections.get(TypesAnnotated.getAllIncluding(RequestMapping.class.getName()).asClass());
QueryFunction<Store, Map<String, Object>> queryAnnotations =
// get all controller endpoint methods
MethodsAnnotated.with(metaAnnotations).as(Method.class)
.map(method ->
// get both method's + declaring class's RequestMapping annotations
get(Annotations.of(method.getDeclaringClass())
.add(Annotations.of(method))
.filter(a -> metaAnnotations.contains(a.annotationType())))
.stream()
// merge annotations' member values into a single hash map
.collect(new AnnotationMergeCollector(method)));
// apply query and map merged hashmap into java annotation proxy
Set<RequestMapping> mergedAnnotations =
reflections.get(mergedAnnotation
.map(map -> ReflectionUtils.toAnnotation(map, metaAnnotation)));
Check the tests folder for more examples and API usage
What else?
Integrating with build lifecycle
It is sometime useful to Reflections.save() the scanned metadata into xml/json as part of the build lifecycle for generating resources,
and then collect it on bootstrap with Reflections.collect() and avoid scanning. See reflections-maven for example.
JavaCodeSerializer - scanned metadata can be persisted into a generated Java source code.
Although less common, it can be useful for accessing types and members in a strongly typed manner. (see example)
AnnotationMergeCollector - can be used to merge similar annotations. (see test)
MemberUsageScanner - experimental scanner allow querying for member usages getMemberUsages() of packages/types/elements in the classpath.
Can be used for finding usages between packages, layers, modules, types etc.
Spread the spirit of open-source and collaboration, clean code and simplicity
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/ronmamo/reflections là nguồn chính thức.
ronmamo/reflections có phải mã nguồn mở không?
Có — ronmamo/reflections phát hành theo license WTFPL, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/ronmamo/reflections.
ronmamo/reflections có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho ronmamo/reflections. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
ronmamo/reflections dùng license gì?
ronmamo/reflections phát hành theo license WTFPL. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
ronmamo/reflections là gì?
ronmamo/reflections (ronmamo/reflections) là dự án Java trên GitHub. Theo mô tả gốc: Java runtime metadata analysis
Đọc đầy đủ README ở tab phía trên.
Chưa chắc reflections có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về reflections.