Bean Mirror is a fluent, type-safe reflection library for the latest Java versions.
- Fluent API
- Type-safe
- Modularized
- Lightweight, no dependencies
Versions | Min JVM |
---|---|
2.0.0 | 17 |
1.1.0 | 11 |
There are several reflection based libraries which allow the developers to easily manipulate objects during runtime. What separates this library is that it uses the newer API (java.lang.invoke) which allows faster code if used properly, compared to the classic java.lang.reflect package.
Because several additions have been added in Java 9 setting the minimum required Java version to anything lower is not feasible (it would not make sense to create another library, which would not be any different from the existing ones).
The project was inspired by jOOR. If you are using an older Java I suggest using that instead.
// Get the name of the school principal by field access
final String principalName = BeanMirror.of(school, MethodHandles.lookup())
.field("principal", Principal.class)
.get("name", String.class);
// Create a setter function to set the value on any instance
final BiConsumer<Student, Integer> setter = BeanMirror.of(Student.class, MethodHandles.lookup())
.createSetter("startingYear", Integer.class);
setter.accept(student, 2018);
setter.accept(otherStudent, 2020);
implementation 'com.github.elopteryx:bean-mirror:2.0.0'
<dependency>
<groupId>com.github.elopteryx</groupId>
<artifactId>bean-mirror</artifactId>
<version>2.0.0</version>
</dependency>
Find available versions on Maven Central Repository.