Skip to content

ReflectionUtils

Dokkaltek edited this page Apr 10, 2025 · 2 revisions

With this class you can get fields, annotations, and metadata of the class without relying on the getters and setters that it may have.

Get fields

You can get the fields of a class as Field objects:

Field nameField = ReflectionUtils.getClassField(SamplePojo.class, "name");

You can also get the field value of an object:

SamplePojo samplePojo = new SamplePojo("Guybrush");

// Asuming the object has the "name" field, it will return it's value
String name = ReflectionUtils.getField(samplePojo, "name");

// If we want to return an alternative value in case the name is null or it doesn't exist
Integer age = ReflectionUtils.getFieldOrElse(samplePojo, "age", 20);

// You can also throw an exception if the result is null
String description = ReflectionUtils.getFieldOrThrow(samplePojo, "description", "Alternative description");

You can also get static fields:

// Get a static value from a class
String staticValue = ReflectionUtils.getStaticField(SamplePojo.class, "SAMPLE_STATIC_VALUE");

// Get a static value from a class or a fallback value
staticValue = ReflectionUtils.getStaticFieldOrElse(SamplePojo.class, "SAMPLE_STATIC_VALUE", "some other value");

// Throw an error if the static value is null or doesn't exist, otherwise return the value
staticValue = ReflectionUtils.getStaticFieldOrThrow(SamplePojo.class, "SAMPLE_STATIC_VALUE");

Set fields

Regular fields

SamplePojo samplePojo = new SamplePojo();

// Sets the "name" field to "Guybrush"
ReflectionUtils.setField(samplePojo, "name", "Guybrush");

// Set the "age" field to 20, but if 20 was null, it wouldn't set it
ReflectionUtils.setFieldIfNewIsNotNull(samplePojo, "age", 20);

// Set the "description" field value only if it's null, otherwise set it to "A description"
ReflectionUtils.setFieldIfNull(samplePojo, "description", "A description");

Static fields

// Sets the "SAMPLE_STATIC_VALUE" static field to "Guybrush"
ReflectionUtils.setStaticField(SamplePojo.class, "SAMPLE_STATIC_VALUE", "Guybrush");

// Set the "SAMPLE_STATIC_VALUE" field to "Kevin", but if "Kevin" was null, it wouldn't set it
ReflectionUtils.setStaticFieldIfNewIsNotNull(SamplePojo.class, "SAMPLE_STATIC_VALUE", "Kevin");

// Set the "SAMPLE_STATIC_VALUE" field value only if it's null, otherwise set it to "Guybrush"
ReflectionUtils.setStaticFieldIfNull(SamplePojo.class, "SAMPLE_STATIC_VALUE", "Guybrush");

Get methods

// Gets a reference to a method without arguments
Method method = ReflectionUtils.getMethod(SamplePojo.class, "getName");

// Gets a reference to a method with one argument of type String
Method method = ReflectionUtils.getMethod(SamplePojo.class, "setName", String.class);

Invoke methods

Regular methods

// Invoke method without arguments and get the result
String name = ReflectionUtils.invokeMethod(SamplePojo.class, "getName");

// Invoke method with arguments without result
ReflectionUtils.invokeMethod(emptyPojo, "setDescription", "New description");

Static methods

// Invoke static method without arguments and get the result
String staticValue = ReflectionUtils.invokeStaticMethod(SamplePojo.class, "getSampleStaticValue")

// Invoke static method with arguments without result
ReflectionUtils.invokeStaticMethod(SamplePojo.class, "setEmptyStaticValue", "Some sample value");

Invoke constructor

// Invoke empty constructor
SamplePojo samplePojo = ReflectionUtils.invokeConstructor(SamplePojo.class);

// Invoke constructor with arguments
SamplePojo pojoWithName = ReflectionUtils.invokeConstructor(SamplePojo.class, "Guybrush");

Get annotations

// Get field annotation
SampleAnnotation ageAnnotation = ReflectionUtils.getFieldAnnotation(SamplePojo.class, "age", SampleAnnotation.class);

// Get method annotation
SampleAnnotation getAgeMethodAnnotation = ReflectionUtils.getMethodAnnotation(SamplePojo.class, "getAge", SampleAnnotation.class);

// For class annotations you can already use the default Reflection api, so no utility is provided
SampleAnnotation classAnnotation = SamplePojo.class.getAnnotation(SampleAnnotation.class);
Clone this wiki locally