Profile Photo

Reflection API

Created on: Jan 17, 2025

Java Reflection is a powerful feature in the Java language that allows inspection and manipulation of classes, methods, fields, and constructors at runtime, even if they are private. It is part of the java.lang.reflect package and is commonly used in scenarios such as:

  • Accessing private fields and methods.
  • Analyzing or modifying the behavior of a class during runtime.
  • Framework and library development, such as in dependency injection or testing frameworks.

Key Components of Java Reflection

  1. Class: Represents a Java class or interface.
  2. Field: Represents a class field (attribute).
  3. Method: Represents a class method.
  4. Constructor: Represents a class constructor.

Example: Using Java Reflection

package org.example; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; class Person { private String name; private int age; // Constructor public Person(String name, int age) { this.name = name; this.age = age; } // Public method public void greet() { System.out.println("Hello, my name is " + name + " and I am " + age + " years old."); } // Private method private void privateInfo() { System.out.println("This is a private method."); } } public class Solution { public static void main(String[] args) { try { // Load the Person class Class<?> personClass = Person.class; // Access and instantiate the constructor Constructor<?> constructor = personClass.getConstructor(String.class, int.class); Object personInstance = constructor.newInstance("Alice", 30); // Access public methods Method greetMethod = personClass.getMethod("greet"); greetMethod.invoke(personInstance); // Access private field Field nameField = personClass.getDeclaredField("name"); nameField.setAccessible(true); // Allow access to private field System.out.println("Private field value (before): " + nameField.get(personInstance)); nameField.set(personInstance, "Bob"); System.out.println("Private field value (after): " + nameField.get(personInstance)); // Access private methods Method privateMethod = personClass.getDeclaredMethod("privateInfo"); privateMethod.setAccessible(true); // Allow access to private method privateMethod.invoke(personInstance); } catch (Exception e) { e.printStackTrace(); } } }
Hello, my name is Alice and I am 30 years old. Private field value (before): Alice Private field value (after): Bob This is a private method.

Internal working

When a .class file is loaded by the JVM, it contains metadata describing the class, including its fields, methods, constructors, annotations, and other attributes. The JVM loads this metadata into the method area (or class metadata area in modern JVMs).

Points to Note

  1. Security Implications:

    • Reflection can break encapsulation (e.g., access private fields and methods).
    • It is slower than normal method calls because it involves runtime checks.
  2. Applications:

    • Used in frameworks like Spring, Hibernate, and JUnit.
    • Helpful in scenarios like dependency injection, proxies, or testing private methods.
    • Aspect-Oriented Programming (AOP)
    • Serialization and Deserialization:

Reference