Commit ea2968b
committed
feat: demonstrate Reflection API usage to inspect class structure at runtime
WHAT was added:
- Implemented a `Refelectionpackage` program showcasing how to use Java Reflection API (`java.lang.reflect.*`).
- Created a sample class `My` with fields (private, protected, default, public), constructors, and methods.
- Demonstrated retrieval of:
- Class metadata via `My.class`
- Fields using `getDeclaredFields()`
- Public methods (including inherited ones) using `getMethods()`
- Constructors using `getConstructors()`
- Method parameters using `getParameters()`
WHY this matters:
- Reflection enables inspection and manipulation of classes at runtime without knowing their structure at compile time.
- Essential for building frameworks, libraries, debuggers, and tools that require dynamic behavior.
KEY FEATURES SHOWN:
1. **Access Class Information**
- `Class c = My.class;`
- Alternative: `obj.getClass()`
2. **Inspect Fields**
- `getDeclaredFields()` → returns all fields regardless of modifier (public, private, protected, package).
- Useful for serialization/deserialization libraries.
3. **Inspect Methods**
- `getMethods()` → returns all public methods, including those inherited from `Object`.
- Example: toString(), hashCode(), equals() also appear.
4. **Inspect Constructors**
- `getConstructors()` → returns all public constructors.
- Output for `My`:
```
public My()
public My(int,int)
```
5. **Inspect Parameters**
- `getParameters()` → retrieves metadata about method parameters.
- Useful for frameworks that need to auto-bind request parameters to method arguments.
6. **Security & Performance Considerations**
- Reflection bypasses normal access control (can access private members with `setAccessible(true)`).
- Slower than direct calls due to runtime type checks.
- Should be used carefully in performance-critical or secure applications.
REAL-WORLD APPLICATIONS:
- **Frameworks (Spring, Hibernate):** Dependency injection, ORM entity mapping, dynamic proxies.
- **Testing (JUnit, TestNG):** Auto-discovery and invocation of test methods via annotations.
- **IDE/Debugger Tools:** Inspect and modify objects at runtime.
- **Serialization Libraries (Jackson, Gson):** Dynamically map JSON/XML fields to Java objects.
- **Dependency Injection Containers:** Instantiate and configure beans without explicit `new`.
KEYWORDS:
Reflection, Class metadata, Fields, Methods, Constructors, Parameters, Java Introspection.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 3d3e28f commit ea2968b
1 file changed
+35
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | | - | |
6 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
7 | 11 | | |
8 | 12 | | |
9 | 13 | | |
| |||
15 | 19 | | |
16 | 20 | | |
17 | 21 | | |
18 | | - | |
19 | 22 | | |
20 | 23 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | | - | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
| 43 | + | |
41 | 44 | | |
42 | 45 | | |
43 | | - | |
44 | 46 | | |
45 | 47 | | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
49 | 51 | | |
50 | 52 | | |
51 | | - | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
56 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
0 commit comments