Reflection

Reflection lets code inspect classes, methods, fields, constructors, and annotations at runtime.

Class<?> type = User.class;

for (var method : type.getDeclaredMethods()) {
    System.out.println(method.getName());
}

Frameworks use reflection for dependency injection, serialization, ORM mapping, testing, and plugin systems. Reflection can weaken compile-time guarantees and complicate encapsulation, performance analysis, and native-image compatibility.

Use reflection at framework boundaries rather than spreading it through ordinary business logic.

Reflection is a tool, not a design default

Prefer normal Java calls for business logic; use reflection when the framework truly needs dynamic discovery.

Key Takeaways
  • Reflection provides runtime type inspection and invocation.
  • It trades some compile-time safety for flexibility.
  • Framework infrastructure is a better home for reflection.