Backend Development 12 min read

Understanding Java Reflection: Principles, Usage, and Source Code Analysis

This article provides a comprehensive overview of Java reflection, explaining its definition, underlying JVM mechanisms, practical usage with code examples, detailed source‑code walkthrough, advantages and disadvantages, and its critical role in popular frameworks such as Spring.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Understanding Java Reflection: Principles, Usage, and Source Code Analysis

In Java, reflection is a powerful mechanism that allows runtime inspection and modification of classes, interfaces, fields, and methods, enabling dynamic object creation, method invocation, and access to private members.

1. What is Reflection?

According to the official Oracle definition, reflection enables Java code to discover information about the fields, methods, and constructors of loaded classes and to operate on them within security restrictions.

In practice, Java reflection means that at runtime you can know all attributes and methods of any class and invoke any method on any object.

The core reflection classes reside in the java.lang.reflect package, such as Class , Constructor , Field , and Method .

2. How Reflection Works

The principle can be broken down into four steps:

Class Loading : The class loader loads the bytecode of a class and stores it in the JVM method area.

Creating Class Objects : During loading, the JVM automatically creates a Class object that holds metadata.

Obtaining Class Objects : You can obtain a Class object via .class , obj.getClass() , or Class.forName() .

Access and Manipulation : Using the Class object, you can retrieve Field , Method , and Constructor objects and manipulate them, even accessing private members.

All of this relies on JVM support for class loading, storage, and access.

3. Using Reflection – Example

The following example demonstrates how to use reflection to invoke the greet() method of a Person class with a name parameter.

Steps:

Obtain the Class object via Person.class .

Retrieve class name and modifiers using clazz.getName() and clazz.getModifiers() .

List all declared methods with clazz.getDeclaredMethods() .

Create a Person instance using clazz.getDeclaredConstructor().newInstance() .

Get the greet method via clazz.getDeclaredMethod() and make it accessible with setAccessible(true) .

Invoke the method with Method.invoke() , passing name = "Java" .

Result:

4. Source Code Deep Dive

The core of reflection lies in Method.invoke() . The call chain goes through MethodAccessor.invoke() , which is implemented by three classes. The actual native method invoke0() finally executes the call at the OS level.

5. Advantages and Disadvantages

Advantages:

Dynamic: Enables runtime discovery and manipulation of class information.

Flexible: Allows bypassing access modifiers to work with private members.

Framework Development: Powers dependency injection, annotation processing, and more.

Debugging: Helps explore class structures during debugging.

Disadvantages:

Performance Overhead: Reflective calls are slower than direct calls.

Security and Stability: Breaking encapsulation can lead to unsafe code.

6. Why Use Reflection?

Common scenarios include runtime type checking, dynamic object creation, accessing/modifying private members, dynamic method invocation, and building flexible frameworks.

7. Popular Frameworks Using Reflection

Spring (Framework, MVC, Boot, Cloud) heavily relies on reflection for dependency injection, autowiring, AOP, dynamic proxies, and bean post‑processing. Other frameworks such as JUnit, Jackson, and Hibernate also use reflection for testing, JSON binding, and ORM mapping.

8. Conclusion

Java reflection provides the foundation for many powerful frameworks, offering flexibility at the cost of some performance and security considerations. Mastering reflection helps developers understand and troubleshoot frameworks like Spring.

Investing in yourself is the greatest wealth.

JavaJVMbackend developmentReflectionFrameworks
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.