*❗ Please note: Reflections library is currently NOT under active development or maintenance ❗*
*Thank you for your continuous support!
<br>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
[![Build Status](https://travis-ci.org/ronmamo/reflections.svg?branch=master)](https://travis-ci.org/ronmamo/reflections)
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](http://bill.burkecentral.com/2008/01/14/scanning-java-annotations-at-runtime/) library*
## Usage
Add Reflections dependency to your project:
```xml
# Maven
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
# Gradle
implementation 'org.reflections:reflections:0.10.2'
```
Create Reflections instance and use the query functions:
```java
Reflections reflections = new Reflections("com.my.project");
Set<Class<?>> subTypes =
reflections.get(SubTypes.of(SomeType.class).asClass());
Set<Class<?>> annotated =
reflections.get(SubTypes.of(TypesAnnotated.with(SomeAnnotation.class)).asClass());
```
Or using previous 0.9.x APIs, for example:
```java
Set<Class<? extends SomeType>> subTypes =
reflections.getSubTypesOf(SomeType.class);
Set<Class<?>> annotated =
reflections.getTypesAnnotatedWith(SomeAnnotation.class);
```
*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](https://ronmamo.github.io/reflections/org/reflections/util/ConfigurationBuilder.html), typically configured with packages and [Scanners](https://ronmamo.github.io/reflections/org/reflections/scanners/Scanners.html) to use:
```java
// 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:
```java
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](https://ronmamo.github.io/reflections/org/reflections/scanners).
* **All relevant URLs should be configured**
Consider `.filterInputsBy()` in case too many classes are scanned.
If required, Reflections will [expand super types](https://ronmamo.github.io/reflections/org/reflections/Reflections.html#expandSuperTypes(java.util.Map)) 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.
```java
import static org.reflections.scanners.Scanners.*;
// SubTypes
Set<Class<?>> modules =
reflections.get(SubTypes.of(Module.class).asClass());
// TypesAnnotated (*1)
Set<Class<?>> singletons =
reflections.get(TypesAnnotated.with(Singleton.class).asClass());
// MethodsAnnotated
Set<Method> resources =
reflections.get(MethodsAnnotated.with(GetMapping.class).as(Method.class));
// FieldsAnnotated
Set<Field> ids =
reflections.get(FieldsAnnotated.with(Id.class).as(Field.class));
// Resources
Set<String> properties =
reflections.get(Resources.with(".*\\.properties"));
```
More scanners:
```java
// MethodsReturn
Set<Method> voidMethods =
reflections.get(MethodsReturn.with(void.class).as(Method.class));
// MethodsSignature
Set<Method> someMethods =
reflections.get(MethodsSignature.of(long.class, int.class).as(Method.class));
// MethodsParameter
Set<Method> pathParam =
reflections.get(MethodsParameter.of(PathParam.class).as(Method.class));
// ConstructorsAnnotated
Set<Constructor> injectables =
reflections.get(ConstructorsAnnotated.with(Inject.class).as(Constructor.class));
// ConstructorsSignature
Set<Constructor> someConstructors =
reflections.get(ConstructorsSignature.of(String.class).as(Constructor.class));
// MethodParameterNamesScanner
List<String> parameterNames =
reflections.getMemberParameterNames(member);
// MemberUsageScanner
Set<Member> usages =
reflections.getMemberUsages(member)
```
*See more examples in [ReflectionsQueryTest](src/test/java/org/reflections/ReflectionsQueryTest.java).*
*Note that previous 0.9.x APIs are still supported*
<details>
<summary><i>Compare Scanners and previous 0.9.x API (*)</i></summary>
| Scanners | previous 0.9.x API | previous Scanner |
| -------- | ------------------ | ------ |
| `get(SubType.of(T))` | getSubTypesOf(T) | ~~SubTypesScanner~~ |
| `get(SubTypes.of(`<br> `TypesAnnotated.with(A)))` | getTypesAnnotatedWith(A) *(1)*| ~~TypeAnnotationsScanner~~ |
| `get(MethodsAnnotated.with(A))` | getMethodsAnnotatedWith(A) | ~~MethodAnnotationsScanner~~ |
| `get(ConstructorsAnnotated.with(A))` | getConstructorsAnnotatedWith(A) *(2)*| ~~MethodAnnotationsScanner~~ |
| `get(FieldsAnnotated.with(A))` | getFieldsAnnotatedWith(A) | ~~FieldAnnotationsScanner~~ |
| `get(Resources.with(regex))` | getResources(regex) | ~~ResourcesScanner~~ |
| `get(MethodsParameter.with(P))` | getMethodsWithParameter(P) *(3)*<br>~~getMethodsWithAnyParamAnnotated(P)~~| ~~MethodParameterScanner~~<br>*obsolete* |
| `get(MethodsSignature.of(P, ...))` | getMethodsWithSignature(P, ...) *(3)<br>~~getMethodsMatchParams(P, ...)~~*| " |
| `get(MethodsReturn.of(T))` | getMethodsReturn(T) *(3)*| " |
| `get(ConstructorsParameter.with(P))` | getConstructorsWithParameter(P) *(3)<br>~~getConstructorsWithAnyParamAnnotated(P)~~*| " |
| `get(ConstructorsSignature.of(P, ...))` | getConstructorsWithSignature(P, ...) *(3)<br>~~getConstructorsMatchParams(P, ...)~~*| " |
*Note: `asClass()` and `as()` mappings were omitted*
*(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*
</details>
## ReflectionUtils
Apart from scanning classpath metadata using [Javassist](https://github.com/jboss-javassist/javassist),
Java Reflection convenient methods are available using
[ReflectionsUtils](https://ronmamo.github.io/reflections/org/reflections/ReflectionUtils.html):
```java
import static org.reflections.ReflectionUtils.*;
Set<Class<?>> superTypes = get(SuperTypes.of(T));
Set<Field> fields = get(Fields.of(T));
Set<Constructor> constructors = get(Constructors.of(T));
Set<Methods> methods = get(Methods.of(T));
Set<URL> resources = get(Resources.with(T));
Set<Annotation> annotati
没有合适的资源?快使用搜索试试~ 我知道了~
Java runtime metadata analysis.zip
共77个文件
java:59个
xml:5个
yml:3个
需积分: 1 0 下载量 115 浏览量
2024-02-28
21:24:09
上传
评论
收藏 110KB ZIP 举报
温馨提示
Java runtime metadata analysis.zip
资源推荐
资源详情
资源评论
收起资源包目录
Java runtime metadata analysis.zip (77个子文件)
reflections-master
LICENSE-2.0.txt 11KB
.travis.yml 61B
pom.xml 9KB
.github
workflows
maven-ci.yml 1KB
FUNDING.yml 687B
src
test
resources
jarWithBootLibJar.jar 942B
another-project.jar 20KB
jarWithManifest.jar 633B
dir+with spaces
empty.file 0B
META-INF
reflections
inner
resource2-reflections.xml 369B
saved-testModel-reflections.xml 10KB
testModel-reflections.xml 10KB
resource1-reflections.xml 648B
testModel-reflections.json 7KB
saved-testModel-reflections.json 7KB
java
org
reflections
ReflectionsQueryTest.java 12KB
MoreTestsModel.java 2KB
ReflectionUtilsTest.java 6KB
ReflectionsTest.java 17KB
ReflectionsCollectTest.java 1KB
MyTestModelStore.java 2KB
FilterBuilderTest.java 2KB
VfsTest.java 6KB
JavaCodeSerializerTest.java 1KB
CombinedTestModel.java 1KB
TestModel.java 2KB
ReflectionUtilsQueryTest.java 7KB
ReflectionsExpandSupertypesTest.java 4KB
ClasspathHelperTest.java 2KB
NameHelperTest.java 3KB
UsageTestModel.java 979B
MoreTests.java 8KB
ConfigurationBuilderTest.java 2KB
main
java
org
reflections
ReflectionsException.java 396B
scanners
FieldAnnotationsScanner.java 404B
Scanners.java 8KB
AbstractScanner.java 453B
ResourcesScanner.java 458B
MethodParameterNamesScanner.java 2KB
MethodParameterScanner.java 968B
MethodAnnotationsScanner.java 631B
TypeElementsScanner.java 3KB
TypeAnnotationsScanner.java 443B
SubTypesScanner.java 1KB
MemberUsageScanner.java 5KB
Scanner.java 2KB
Reflections.java 30KB
vfs
Vfs.java 12KB
JbossFile.java 864B
SystemDir.java 1KB
JarInputFile.java 1KB
UrlTypeVFS.java 4KB
JarInputDir.java 2KB
ZipDir.java 1KB
ZipFile.java 913B
JbossDir.java 2KB
SystemFile.java 1KB
ReflectionUtils.java 13KB
Store.java 631B
Configuration.java 1KB
serializers
JavaCodeSerializer.java 8KB
Serializer.java 882B
XmlSerializer.java 3KB
JsonSerializer.java 2KB
util
NameHelper.java 7KB
ConfigurationBuilder.java 9KB
ReflectionUtilsPredicates.java 9KB
JavassistHelper.java 4KB
ClasspathHelper.java 16KB
UtilQueryBuilder.java 1KB
QueryBuilder.java 4KB
QueryFunction.java 5KB
AnnotationMergeCollector.java 3KB
FilterBuilder.java 6KB
COPYING.txt 484B
.gitignore 65B
README.md 12KB
共 77 条
- 1
资源评论
日刷百题
- 粉丝: 5360
- 资源: 951
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功