Skip to content

Reflection

Overview

Reflection helpers with cached metadata and property accessors.

Architecture

  • ClassInfo caches fields and methods for faster reflection.
  • PropertyAccessor resolves getters/setters via conventions or annotations.
  • ReflectionUtils offers convenient get/set operations and method lookups.

How to use

Gradle

implementation("ie.bitstep.mango:mango4j-reflection:1.0.0")

Maven

1
2
3
4
5
<dependency>
    <groupId>ie.bitstep.mango</groupId>
    <artifactId>mango4j-reflection</artifactId>
    <version>1.0.0</version>
</dependency>

Examples

Manipulate a property using a PropertyAccessor

1
2
3
4
5
Profile profile = new Profile(...);
PropertyAccessor<String> accessor = new PropertyAccessor<>(Profile.class, "firstName");

accessor.set(profile, "Fred");
System.out.println(accessor.get(profile));

Get a cached PropertyAccessor

1
2
3
4
5
6
7
Profile profile = new Profile(...);
PropertyAccessor<String> accessor = ReflectionUtils
    .getClassInfo(Profile.class)
    .getPropertyAccessor("firstName");

accessor.set(profile, "Fred");
System.out.println(accessor.get(profile));

Manipulate a property using ReflectionUtils

1
2
3
4
Profile profile = new Profile(...);

ReflectionUtils.setField(profile, "firstName", "Fred");
System.out.println(ReflectionUtils.getField(profile, "firstName"));