Methods to inspect and modify the behavior of a class at run time.
Methods to retrieve class metadata at run time.
This is the main class used for reflection. The class holds all the information and data about classes and objects at runtime. Next, we will discuss each of these classes and use programming examples to demonstrate the reflection on these classes.
Modifier: Modifier class gives us information about a specific access modifier.Īll the above classes are a part of package.
Constructor: Constructor class gives information about class constructor that includes constructor access modifier, constructor name, and parameter types.
Method: The Method class can help us to extract information like access modifier of the method, method return type, method name, method parameter types, and exception types raised by the method.
Field: The Field class has information that we use to declare a variable or a field like a datatype (int, double, String, etc.), access modifier (private, public, protected, etc.), name (identifier) and value.
Using Reflection API, we can implement the reflection on the following entities:
It is used, when your application has third-party libraries and when you want to know about the classes and methods available.
IDE (Integrated Development Environment) E.g.
Reflection is mainly used in debugging tools, JUnit, and frameworks to inspect and change the behavior at runtime.
A distinguishing feature of Reflection API is that we can also manipulate the private data members or methods of the class. We use Reflection API provided by the above packages to modify the class and its members including fields, methods, constructors, etc. The special class “” provides the methods and properties to extract metadata using which we can inspect and modify the class behavior. In Java, the “java.lang” and “” are the two packages that provide classes for reflection. We inspect these objects and then change their behavior at runtime using reflection API. The objects can be anything like methods, interfaces, classes, etc. Thus we can use Reflection API in our programs for the purpose of modifying the object’s behavior. As a result, we can modify the behavior of this object at runtime. Then we use the Reflection API on this object. In the above representation, we can see that we have an unknown object. The “Reflection” process is depicted below. Reflection is an “Application Programming Interface” (API) provided by Java. An object can be a class, a field, or a method.” Thus a Reflection can be defined as a “technique of inspecting and modifying the runtime behavior of an unknown object at run time. Java programming language provides a feature called “Reflection” that allows us to modify the runtime behavior of a class or field or method at runtime. In other words, it is very difficult to change the behavior of various programming components at runtime especially for unknown objects. Whether the properties and methods are anonymous or have names, they can be changed at our will during compile time.īut we cannot change these classes or methods or fields at runtime on the fly. All the public fields up the entire anycodings_reflection class hierarchy.Īll the fields, regardless of their anycodings_reflection accessibility but only for the current anycodings_reflection class, not any base classes that the anycodings_reflection current class might be inheriting from.We are aware that in a given class we can modify its properties and methods at compile time and it is very easy to do so.