0% found this document useful (0 votes)
8 views9 pages

Reflection

The document discusses Java reflection and the Reflection API. It explains that reflection allows examining and modifying classes, fields, methods, and objects at runtime. It covers obtaining Class objects, getting metadata about classes and other program elements, and common uses of reflection like in frameworks, serialization, and plugins.

Uploaded by

Amrendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Reflection

The document discusses Java reflection and the Reflection API. It explains that reflection allows examining and modifying classes, fields, methods, and objects at runtime. It covers obtaining Class objects, getting metadata about classes and other program elements, and common uses of reflection like in frameworks, serialization, and plugins.

Uploaded by

Amrendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

REFLECTION

IN JAVA
REFLECTION IN JAVA
• Java is a versatile language, and when we're writing Java code, we have lots of control over the details of a class. We can
define what kind of data it holds (the fields), and what actions it can perform (the methods). We can change these details
easily at this stage, known as the compile-time, whether these details are specifically named or not.
• However, once the program starts running, things change. This period is known as runtime. At runtime, changing the details
of a class becomes a lot harder. Imagine trying to change the parts of a moving car - it's not impossible, but it's significantly
more difficult, especially if we don't know much about the specific part we're trying to change.
• This is where a special feature of Java called "Reflection" comes to our rescue. Reflection provides us with a set of tools
that can inspect and change the behavior of a class, a field, or a method while the program is running.
• To put it simply, Reflection is like giving your program a mirror. It can look at itself in this mirror (inspect) and also change
its look (modify) in real-time while it's running. This mirror is not an actual mirror, but a part of Java known as the
Reflection API.
• This API gives program the power to learn about itself and change its behavior dynamically at runtime. So, even if program
encounters an unknown object at runtime, with Reflection, it can still learn about this object and even change its behavior if
required. This makes Reflection a very powerful and flexible feature in Java.
• Thus, Reflection, in essence, can be characterized as a method that allows for the examination and alteration of the runtime
behavior of an unidentified object. This object could be a class, a field, or a method.
REFLECTION IN JAVA CONTINUED…

Modify classes,
method or fields
Java Object Reflection Api
behavior at Run
time

• In the depiction, it's illustrated that we are dealing with a Java object. We then employ the Reflection API on
this object. Consequently, this allows us to alter the object's behavior while the program is running.

• The Reflection API in Java enables us to adjust an object's behavior within our programs. This object can be
elements like methods, classes, or interfaces. The process begins with inspecting these elements, and then,
using the Reflection API, we dynamically change their behavior during program execution. This creates a
powerful level of flexibility in managing object behaviors at runtime.

• In the Java programming language, the packages "java.lang" and "java.lang.reflect" serve as the two primary
packages that offer a set of classes specifically designed for implementing reflection capabilities.
KEY USE CASES OF JAVA
REFLECTION API
The Reflection API in Java is mainly used in the following contexts:
1. IDE (Integrated Development Environment) Development: Reflection is used in the development of IDEs like
Eclipse or IntelliJ to understand your code's structure, show method suggestions, etc.
2. Debuggers and Test Tools: Reflection is useful in creating debugging and testing tools, as it allows these tools to
inspect and modify the program's state or behavior.
3. Frameworks: Many Java frameworks like Spring or Hibernate use reflection to inspect the properties of your classes,
to instantiate your objects, or to invoke their methods.
4. Object Serialization: Reflection is used to convert objects to a format (like JSON or XML) that can be stored or
transmitted and then reconstructed later.
5. APIs and Libraries: Reflection is often used in APIs and libraries to provide more flexible ways to use them, such as
automatic handling of similar tasks for different classes.
6. Plugins: Reflection is used in applications that support plugins (add-ons), allowing the application to load, instantiate
and use classes that weren't known when the application was written.
7. JDBC Drivers: The Java Database Connectivity (JDBC) API uses Reflection to load database drivers. The class name
of the driver is specified as a string in the code, and Reflection is used to instantiate an instance of the driver when the
program is run.
REFLECTION API: TARGET ENTITIES
The Reflection API in Java can be used to inspect and manipulate the following entities in a Java program:
1. Classes: You can obtain the Class object for a class and get its metadata, such as its modifiers, package information,
superclass, implemented interfaces, constructors, methods, and fields.
2. Interfaces: Similarly, you can get information about an interface using the Reflection API.
3. Fields: You can read and write static and non-static fields, and get their type, modifier, and other metadata.
4. Methods: You can call static and non-static methods and get their return type, parameter types, exception types,
modifiers, and other metadata.
5. Constructors: You can create new instances of a class by invoking its constructors. You can also get the parameter
types, exception types, modifiers, and other metadata of constructors.
6. Arrays: You can inspect and manipulate arrays, including getting the length of an array, getting and setting values of
elements, and creating new arrays.
7. Enums: Reflection can be used to get the list of Enum constants of an Enum type.
8. Annotations: You can read annotation information from classes, fields, methods, and other program elements.
• Remember that while Reflection is powerful, it can make your code more complex and harder to understand or debug
and can have security and performance implications. So it should be used sparingly and responsibly.
REFLECTION API: CLASS
• The java.lang.Class class in the java.lang package is a key class involved in reflection. It represents a class or interface in Java and provides
methods to examine the structure, behavior, and properties of the class at runtime.
• There are three ways of Obtaining Class Object:
• Using
• forName () method takes the name of the class as an argument and returns the Class object.
• This is particularly useful when we know the fully qualified along with package of the class as a string.
• Syntax:

• Using method:
• If we have an instance of a class, we can use getClass()
• Syntax

• Using .class extension


• If the class is available at compile time, you can use .class syntax.
• Syntax
REFLECTION API: CLASS EXAMPLE

Output:
REFLECTION API: GETTING CLASS INFORMATION
• Once we've obtained the Class object, we can extract various pieces of information about the class:
• getName() - Returns the full class name.
• getSimpleName() - Returns the class name without the package
• getPackage() - Returns the package information for the class
• getModifiers() – Returns modifiers (like public, abstract, final, etc.) for the class
• getSuperclass()– Returns superclass of the class, if exists.
• isInterface(): This method returns true if this Class object represents an interface type
• isEnum(): This method returns true if this Class object represents an enumerated type.
• isAnnotation(): This method returns true if this Class object represents an annotation type.
• isArray(): This method returns true if this Class object represents an array class.

• Before looking into examples, let’s create interface, Enum, annotation, etc.

You might also like