Get Started with Java Reflection API

 

The full code is available on Github.

1- Overview

Java Reflection is a feature of Java programming language that allows to examine or modify the behavior of methods, classes and interfaces at runtime. In order words, Java Reflection allows an executing Java program to examine or introspect upon itself, and manipulate internal properties of the program.

The Java Reflection API is used by many popular frameworks. For example JUnit use reflection to parse @Test annotation to get the test methods and invoke them. We have also the very popular Spring Framework which uses reflection to create its beans.

In practice, Java Reflection allows many operations such as setting and getting field values, invoking class methods, creating new instances of objects using specific constructors, etc… In this tutorial, we will use the Java Reflection API to create some generic helper methods to set and get any static of instance members field values.

 

2- Create and Instance of Field

The first step to get or set any kind of field (instance field or static field) is to create an instance of the java.lang.reflect.Field class. We can do that by using the getDeclaredField of java.lang.Class. The piece of code to do so looks as follow:

Field field = clazz.getDeclaredField(fieldName);

3- Make the field accessible

This step is required only if the field that we want to access is not visible. For example when it is a private field. We can make the field accessible by using the setAccessible method as follow:

field.setAccessible(Boolean.TRUE);

4- Get the field value

After making the field accessible, we can then use the get method to get the value of the field. The Field class contains many variants of the get method. You can read all the existing varient here. The get method expects as input parameter the instance of the class. In the case of static field, we can pass null as input parameter of we can also pass the related Class instance.

The complete helper method to get field value looks as follow :

5- Turn off the final check

This step is required when we want to set the value of a final field. The Java compiler does not allow to change the value of a final field. Using reflection, we can turn off the final check and then be able to change the value of the field. We can do that by using the following steps:

  • Access the modifiers field of java.lang.reflect.Field
  • Make the modifiers field accessible
  • Disable the final of our field

The code to complete these steps looks as follow:

Field modifierField = Field.class.getDeclaredField(MODIFIER_FIELD_NAME);
modifierField.setAccessible(Boolean.TRUE);
modifierField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

6- Set a new value for the field

We set a new value for the field by using the set method of java.lang.reflect.Field. There are also mulpitle variant of the set. You can read them here.

The set method expects two inputs parameters which are respectively an instance of the class and the field value. Same as the get method, in the case of a static field, the first parameter can be null null or can be the related Class instance.

The complete helper method to set field value looks as follow :

7- Unit test for our helper method

In order to validate that our helper methods perform as designed, we have written some unit test for that. All the unit test are writtent with JUnit 5.0.1. The full explanation of the JUnit test is out of the scope of this tutorial.

For our test, we have created a Java class named User that looks as follow:

 

The unit tests looks as follow:

The full code is available on Github.

Thanks for reading. Please leave feedback and questions in the comments!

You May Also Like

About the Author: Miguel KAKANAKOU

Leave a Reply

WP to LinkedIn Auto Publish Powered By : XYZScripts.com