Skip to content

Commit 4d60bb9

Browse files
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

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed
Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1-
/*
2-
Certainly! Reflection in Java is a powerful feature that allows you to inspect and manipulate classes, methods, and fields at runtime.
3-
This can be particularly useful for tasks like debugging, testing, or creating more flexible and dynamic applications.
4-
*/
51
import java.lang.reflect.*;
62

7-
83
public class ReflectionJAVA {
9-
public static void main(String args[])
10-
{
4+
public static void main(String args[]) {
115
try {
126
Class c = Class.forName(args[0]);
137
Method m[] = c.getDeclaredMethods();
8+
149
for (int i = 0; i < m.length; i++)
1510
System.out.println(m[i].toString());
1611
}
1712
catch (Throwable e) {
1813
System.err.println(e);
1914
}
2015
}
21-
}
16+
}
17+
18+
/*
19+
Certainly! Reflection in Java is a powerful feature that allows you to inspect and manipulate classes, methods, and fields at runtime.
20+
This can be particularly useful for tasks like debugging, testing, or creating more flexible and dynamic applications.
21+
22+
1. Command line se run karte waqt:
23+
java ReflectionJAVA java.util.ArrayList
24+
Ye ArrayList class ke saare declared methods print karega.
25+
26+
2. `Class.forName("package.ClassName")`:
27+
Dynamically load karta hai class ko runtime par.
28+
29+
3. `getDeclaredMethods()`:
30+
Sirf us class ke methods deta hai (private + protected + public).
31+
Agar tum `getMethods()` use karoge to uske parent (Object class) ke bhi public methods show honge.
32+
33+
4. Reflection ka use cases:
34+
Debugging tools
35+
Unit testing frameworks (JUnit)
36+
Dependency Injection frameworks (Spring)
37+
Serialization / Deserialization libraries (Jackson)
38+
39+
5. Drawbacks:
40+
Thoda slow hota hai (direct method call se zyada overhead).
41+
Security risk hota hai agar private fields/methods ko expose kar diya jaye.
42+
*/

0 commit comments

Comments
 (0)