Reflections8

Reflections8 - a Java runtime metadata analysis without guava on Java 8

License

License

Categories

Categories

Net Reflections Application Layer Libs Introspection
GroupId

GroupId

net.oneandone.reflections8
ArtifactId

ArtifactId

reflections8
Last Version

Last Version

0.11.7
Release Date

Release Date

Type

Type

jar
Description

Description

Reflections8
Reflections8 - a Java runtime metadata analysis without guava on Java 8
Project URL

Project URL

http://github.com/aschoerk/reflections8
Source Code Management

Source Code Management

https://github.com/aschoerk/reflections8

Download reflections8

How to add to project

<!-- https://jarcasting.com/artifacts/net.oneandone.reflections8/reflections8/ -->
<dependency>
    <groupId>net.oneandone.reflections8</groupId>
    <artifactId>reflections8</artifactId>
    <version>0.11.7</version>
</dependency>
// https://jarcasting.com/artifacts/net.oneandone.reflections8/reflections8/
implementation 'net.oneandone.reflections8:reflections8:0.11.7'
// https://jarcasting.com/artifacts/net.oneandone.reflections8/reflections8/
implementation ("net.oneandone.reflections8:reflections8:0.11.7")
'net.oneandone.reflections8:reflections8:jar:0.11.7'
<dependency org="net.oneandone.reflections8" name="reflections8" rev="0.11.7">
  <artifact name="reflections8" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.oneandone.reflections8', module='reflections8', version='0.11.7')
)
libraryDependencies += "net.oneandone.reflections8" % "reflections8" % "0.11.7"
[net.oneandone.reflections8/reflections8 "0.11.7"]

Dependencies

compile (5)

Group / Artifact Type Version
org.javassist : javassist Optional jar 3.22.0-GA
org.slf4j : slf4j-api Optional jar 1.7.24
org.dom4j : dom4j Optional jar 2.1.1
com.google.code.gson : gson Optional jar 2.8.0
org.slf4j : slf4j-simple Optional jar 1.7.24

provided (1)

Group / Artifact Type Version
javax.servlet : servlet-api Optional jar 2.5

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

This fork is changed to use java8 instead of guava.

<dependency>
    <groupId>net.oneandone.reflections8</groupId>
    <artifactId>reflections8</artifactId>
    <version>0.11.5</version>
</dependency>

To avoid mixup of classes between "pre java8"-reflections and reflections8 all is moved from org.reflections to org.reflections8 Build Status


Java runtime metadata analysis, in the spirit of Scannotations

Reflections scans your classpath, indexes the metadata, allows you to query it on runtime and may save and collect that information for many modules within your project.

Using Reflections you can query your metadata such as:

  • get all subtypes of some type
  • get all types/members annotated with some annotation
  • get all resources matching a regular expression
  • get all methods with specific signature including parameters, parameter annotations and return type

Build Status

Intro

Add Reflections to your project. for maven projects just add this dependency:

<dependency>
    <groupId>net.oneandone.reflections8</groupId>
    <artifactId>reflections8</artifactId>
    <version>0.11.5</version>
</dependency>

A typical use of Reflections would be:

Reflections reflections = new Reflections("my.project");

Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);

Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);

Usage

Basically, to use Reflections first instantiate it with urls and scanners

//scan urls that contain 'my.package', include inputs starting with 'my.package', use the default scanners
Reflections reflections = new Reflections("my.package");

//or using ConfigurationBuilder
new Reflections(new ConfigurationBuilder()
     .setUrls(ClasspathHelper.forPackage("my.project.prefix"))
     .setScanners(new SubTypesScanner(), 
                  new TypeAnnotationsScanner().filterResultsBy(optionalFilter), ...),
     .filterInputsBy(new FilterBuilder().includePackage("my.project.prefix"))
     ...);

Then use the convenient query methods: (depending on the scanners configured)

//SubTypesScanner
Set<Class<? extends Module>> modules = 
    reflections.getSubTypesOf(com.google.inject.Module.class);
//TypeAnnotationsScanner 
Set<Class<?>> singletons = 
    reflections.getTypesAnnotatedWith(javax.inject.Singleton.class);
//ResourcesScanner
Set<String> properties = 
    reflections.getResources(Pattern.compile(".*\\.properties"));
//MethodAnnotationsScanner
Set<Method> resources =
    reflections.getMethodsAnnotatedWith(javax.ws.rs.Path.class);
Set<Constructor> injectables = 
    reflections.getConstructorsAnnotatedWith(javax.inject.Inject.class);
//FieldAnnotationsScanner
Set<Field> ids = 
    reflections.getFieldsAnnotatedWith(javax.persistence.Id.class);
//MethodParameterScanner
Set<Method> someMethods =
    reflections.getMethodsMatchParams(long.class, int.class);
Set<Method> voidMethods =
    reflections.getMethodsReturn(void.class);
Set<Method> pathParamMethods =
    reflections.getMethodsWithAnyParamAnnotated(PathParam.class);
//MethodParameterNamesScanner
List<String> parameterNames = 
    reflections.getMethodParamNames(Method.class)
//MemberUsageScanner
Set<Member> usages = 
    reflections.getMethodUsages(Method.class)
  • If no scanners are configured, the default will be used - SubTypesScanner and TypeAnnotationsScanner.
  • Classloader can also be configured, which will be used for resolving runtime classes from names.
  • Reflections expands super types by default. This solves some problems with transitive urls are not scanned.

Checkout the javadoc for more info.

Also, browse the tests directory to see some more examples.

ReflectionUtils

ReflectionsUtils contains some convenient Java reflection helper methods for getting types/constructors/methods/fields/annotations matching some predicates, generally in the form of *getAllXXX(type, withYYY)

for example:

import static org.reflections8.ReflectionUtils.*;

Set<Method> getters = getAllMethods(someClass,
  withModifier(Modifier.PUBLIC), withPrefix("get"), withParametersCount(0));

//or
Set<Method> listMethodsFromCollectionToBoolean = 
  getAllMethods(List.class,
    withParametersAssignableTo(Collection.class), withReturnType(boolean.class));

Set<Field> fields = getAllFields(SomeClass.class, withAnnotation(annotation), withTypeAssignableTo(type));

See more in the ReflectionUtils javadoc

Integrating into your build lifecycle

Although scanning can be easily done on bootstrap time of your application - and shouldn't take long, it is sometime a good idea to integrate Reflections into your build lifecyle. With simple Maven/Gradle/SBT/whatever configuration you can save all scanned metadata into xml/json files just after compile time. Later on, when your project is bootstrapping you can let Reflections collect all those resources and re-create that metadata for you, making it available at runtime without re-scanning the classpath - thus reducing the bootstrapping time.

For Maven, see example using gmavenplus in the reflections-maven repository

Other use cases

See the UseCases wiki page

Contribute

Pull requests are welcomed!!

The license is WTFPL, just do what the fuck you want to.

This library is published as an act of giving and generosity, from developers to developers.

Please feel free to use it, and to contribute to the developers community in the same manner. Dāna Donate

Cheers

Versions

Version
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.10.5
0.10.4