This project provides a hands-on demonstration of key concepts and functionalities within Java's Reflection API. It illustrates how Java code can inspect and manipulate its own structure (classes, methods, fields, constructors) at runtime—a powerful capability fundamental to many advanced frameworks and dynamic applications.
Java Reflection allows programs to examine and modify their own structure and behavior at runtime. This project helps understand:
- How to obtain
Classobjects using various methods. - How to dynamically create instances of classes.
- How to discover and invoke methods.
- How to access and manipulate fields.
- Class loaders and package metadata.
- Advanced meta-programming techniques like direct memory access and annotation processing.
- Using
.class(e.g.,String.class) - Using
object.getClass() - Using
Class.forName("com.example.MyClass")
- Creating objects at runtime using
Constructorobjects. - Handling parameterized constructors.
- Discovering and invoking methods dynamically.
- Invoking private methods using
setAccessible(true).
- Reading and writing to fields (can be expanded further).
- Accessing class’s package details.
- Understanding
ClassLoader.loadClass().
- Demonstrates instantiation and inspection of final classes.
- Using
sun.misc.Unsafeto:- Allocate raw off-heap memory.
- Directly write/read bytes or primitives to memory.
- Bypass Java’s memory safety and encapsulation.
- Defining annotations:
@Component,@Service,@Model,@AutoWired - Processing them at runtime for custom behavior.
- Inspecting types with
java.lang.reflect.Type,ParameterizedType, etc.
- Accessing methods like
valueOf()dynamically.
- Using
Method.getParameterCount(), etc.
- Using reflection to bypass normal restrictions of
final.
- Dangerous and internal Java class.
- Used to demonstrate raw memory handling and final field mutation.
- With different
ElementTypes andRetentionPolicys. - Enables dynamic behaviors at runtime.
- Scans for annotated classes.
- Instantiates and injects dependencies using Reflection.
- Mimics core Spring features.
- Introspects method and class generics via:
TypeVariableParameterizedTypeGenericArrayType
- Java 17+
java.lang.reflect.*- Maven (optional build tool)
git clone https://github.com/sahilvadia/REFLECTION-API.git
cd REFLECTION-APImvn clean installmvn exec:java -Dexec.mainClass="org.reflection.runner.advanced.di.MyDIContext"# Unsafe Integer Manipulation
java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED org.reflection.runner.advanced.unsafe.UnsafeIntegerManipulation
# Unsafe Memory Access
java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED org.reflection.runner.advanced.unsafe.UnsafeMemoryAccessDemo| Package | Description |
|---|---|
org.reflection.runner.basic |
Fundamental demos (e.g., objectDotClass, Class.forName, loadClass) |
org.reflection.runner.advanced.annotation |
Custom annotations like @AutoWired, @Component, etc. |
org.reflection.runner.advanced.bean |
Sample bean classes like Student with @Model |
org.reflection.runner.advanced.service |
Service classes like StudentService, using field injection |
org.reflection.runner.advanced.di |
Core DI container logic (MyDIContext) |
org.reflection.runner.advanced.unsafe |
Unsafe demos: UnsafeIntegerManipulation, UnsafeMemoryAccessDemo |
org.reflection.runner.advanced.type |
Generic type inspection with TypeUseReflection |
- Runtime Type Info: Using
Classto inspect metadata at runtime. - Dynamic Behavior: Creating/invoking objects/methods dynamically.
- Introspection: Reading class/method/generic structures.
- Manipulation: Reading/writing fields/methods via reflection.
- Class Loaders: How classes are dynamically loaded.
- Exception Handling: Proper handling of common reflection-related exceptions.
- Meta-Programming: Combining annotations + reflection for frameworks.
- Unsafe Operations: Understanding how internal memory operations work.
Want to grow this further? Consider contributing:
- Field Access Demos: Add examples of
get/seton private fields. - Array Reflection: Manipulate arrays using
java.lang.reflect.Array. - Proxy API: Add examples using
ProxyandInvocationHandler. - Compile-Time Annotation Processing.
- Benchmarking Reflection vs. Direct Calls.
- Advanced Exception & Circular Dependency Handling in DI.
Contributions welcome! Please fork, clone, open an issue or submit a pull request.