Java Reflection API

December 28, 2022

Java Reflection API

Java Reflection API is an advanced feature of Java used for introspecting and modifying applications from class levels during runtime. As it is powerful, Reflection API should be used by developers who understand the fundamental & philosophy of Java language. Reflection makes the impossible to possible during runtime. All class members can be retrieved by using Reflection API and then can be displayed. Even, private methods can be invoked.

reflectionAPI

This functionality is very powerful yet it should be used very careful while building our software. There are three main points that we need to take care of while using Reflection API;

  • Performance: Reflection API has slower performance as it is dynamically resolved.
  • Exposure of Internals: Private methods or fields can be reached by using Reflection API so can cause unexpected impacts. It also breaks encapsulation & abstraction of internals.
  • Security: It requires a runtime permission to introspect & modify the application.

And main features of Reflection API are;

  • Extensibility Feature: An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
  • Debugger & Test Tools: Debuggers even need private members of a class or test need calling of methods exposed by the class in order to understand the level of coverage by using Reflection API.
  • Visual Development Environments: IDEs can use type information or class members by using Reflection.

You can also refer to Reflection API Tutorial in order to reach these info related to Reflection API.

How to Get Instance of Classes

There are three main ways to get instance of classes in Reflection.

  • forName() method → It loads the class dynamically and returns the instance of a Class. It should be used if you know the exact name of the class.
  • getClass() method → It returns the instance of the class and should be used if type of the Class is known.
  • .class syntax → If there is no instance of the class, .class syntax can be used to get the instance of the class.

Please check the Gist that shows a code snippet can be examined to see how a class is initialized.

Methods in Reflection API

Below you can find useful methods in Reflection API other than the initialization ones;

  • getConstructors() → Used to get constructors represented by the class
  • getDeclaredConstructors() → Used to get declared constructors represented by the class
  • getDeclaredConstructor(@Nullable Class<?> …parameterTypes) → Used to get declared constructor represented by the class. parameterTypes is an array of Class objects that identify the class constructor in the declared order.
  • getMethods() → Used to get methods in the initialized class
  • getDeclaredMethods() → Used to get declared methods in the initialized class
  • getDeclaredMethod(@NonNls @NotNull String name, Class<?> …parameterTypes) → Used to get declared method represented by the class. name is the exact name of the method, parameterTypes is an array of Class objects that identify the method signature in the declared order.
  • getDeclaredPublicConstructor(@NonNls @NotNull String name, Class<?> …parameterTypes) → Used to get declared only public method represented by the class. name is the exact name of the method, parameterTypes is an array of Class objects that identify the method signature in the declared order.
  • getDeclaredFields() → Used to get declared fields represented by the class. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.
  • getDeclaredField(@NonNls @NotNull String name) → Used to get declared field with the name parameter represented by the class.
  • setAccessible(boolean flag) → Used to set the accessibility of the reflected object.
  • invoke(Object obj, Object… args) → Used to invoke the underlying method on the specified object with the given parameters. Parameters must be given to the invoke method with the declared order in the underlying method.
  • isDefault() → Used to understand whether the underlying method is a default method or not.

I try to give detail examples with the usage of the above methods and the logic of Reflection API in Java in my Github profile within public java-reflection-example repository.

As I mentioned in the beginning, Reflection API is powerful to introspect and modify objects during runtime but it must be used by if and only if you know the fundamentals and logical concepts of Java.

Hope this article makes the Reflection API in JAVA topic crystal clear !!!

Your comments & questions are welcomed.

Share: