Commit 4d60bb9
committed
feat: add ReflectionJAVA program to list declared methods of a class at runtime
WHAT was added:
- Implemented `ReflectionJAVA` class demonstrating reflection with `Class.forName()`.
- Program accepts class name as a runtime argument (`args[0]`).
- Retrieves all declared methods using `getDeclaredMethods()`.
- Prints each method’s full signature (return type, name, parameters, modifiers).
WHY this matters:
- Shows how to dynamically inspect a class’s behavior at runtime without knowing its structure at compile time.
- Essential for building frameworks, tools, and debuggers that adapt to different classes dynamically.
KEY LEARNINGS DEMONSTRATED:
1. **Loading a class dynamically**
- `Class.forName(args[0])` loads a class by its fully qualified name.
- Example: `java.lang.String` as input loads the `String` class.
2. **Inspecting methods**
- `getDeclaredMethods()` retrieves *all methods* declared in the class, regardless of access modifiers (public, private, protected, package).
- Unlike `getMethods()`, it does not include inherited methods from superclasses.
3. **Method details**
- Each method’s signature (return type, parameters, exceptions) is printed using `toString()`.
- Example output for `java.lang.String`:
```
public int java.lang.String.length()
public boolean java.lang.String.isEmpty()
public char java.lang.String.charAt(int)
...
```
4. **Exception handling**
- Wraps reflection logic in a `try-catch` block to gracefully handle errors like `ClassNotFoundException` or security restrictions.
REAL-WORLD APPLICATIONS:
- **Testing frameworks** (JUnit, TestNG): Discover and execute annotated methods at runtime.
- **Dependency injection (Spring, Guice)**: Dynamically inspect constructors and methods for wiring dependencies.
- **ORM frameworks (Hibernate, JPA)**: Map class fields/methods to database columns and operations.
- **Debugging/Profiling tools**: Inspect objects and methods without modifying source code.
- **Dynamic plugin systems**: Load external classes and invoke methods without recompilation.
LIMITATIONS:
- Slower than direct method calls due to runtime inspection overhead.
- Can break encapsulation by accessing private methods/fields.
- Requires careful exception handling (checked exceptions like `ClassNotFoundException`, `IllegalAccessException`).
KEYWORDS:
Reflection API, Class.forName, getDeclaredMethods, runtime inspection, dynamic method discovery.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent ea2968b commit 4d60bb9
1 file changed
+29
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | 1 | | |
6 | 2 | | |
7 | | - | |
8 | 3 | | |
9 | | - | |
10 | | - | |
| 4 | + | |
11 | 5 | | |
12 | 6 | | |
13 | 7 | | |
| 8 | + | |
14 | 9 | | |
15 | 10 | | |
16 | 11 | | |
17 | 12 | | |
18 | 13 | | |
19 | 14 | | |
20 | 15 | | |
21 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
0 commit comments