Skip to content

Commit ea2968b

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

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed
Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
//https://www.geeksforgeeks.org/reflection-in-java/
2-
31
import java.lang.reflect.*;
42

5-
class My
6-
{
3+
/**
4+
* 📘 GeeksforGeeks – Reflection in Java
5+
* <p>
6+
* Article: <a href="https://www.geeksforgeeks.org/reflection-in-java/">
7+
* https://www.geeksforgeeks.org/reflection-in-java/</a>
8+
*/
9+
10+
class My {
711
private int a;
812
protected int b;
913
public int c;
@@ -15,17 +19,14 @@ public My(int x,int y) {}
1519

1620
public void display(String s1,String s2) {}
1721
public int show(int x,int y) {return 0;}
18-
1922
}
2023

21-
public class Refelectionpackage
22-
{
23-
public static void main(String[] args)
24-
{
25-
Class c=My.class; //Getting defination by assign ing .class file
26-
/* Or you also use
27-
ReflectionJAVA m=new ReflectionJAVA();
28-
Class c1=m.getClass();
24+
public class Refelectionpackage {
25+
public static void main(String[] args) {
26+
Class c=My.class;
27+
/* Getting definition by assigning .class file.
28+
29+
Or you also use, ReflectionJAVA m=new ReflectionJAVA(); Class c1=m.getClass();
2930
*/
3031
System.out.println(c.getName());
3132

@@ -36,21 +37,37 @@ public static void main(String[] args)
3637
for(Method m:meth)
3738
System.out.println(m);
3839

39-
//Getting a detail of the constructer in class.
40+
//Getting a detail of the constructor in class.
41+
4042
Constructor con[] = c.getConstructors();
43+
4144
for(Constructor ct:con)
4245
System.out.println(ct);
43-
4446
/*
4547
O/p:
4648
public My()
4749
public My(int,int)
4850
*/
4951

5052
Parameter param[]=meth[0].getParameters();
51-
for(Parameter p:param)
52-
{
53+
for(Parameter p:param) {
5354
System.out.println(p);
5455
}
5556
}
56-
}
57+
}
58+
59+
/*
60+
1. Reflection ek a runtime mechanism hai → Jisme hum classes, methods, fields, constructors ki info access kar sakte hain.
61+
2. Entry point: `Class` object (Class c = My.class; ya obj.getClass()).
62+
3. `getDeclaredFields()` → sabhi fields (private + protected + default + public).
63+
4. `getMethods()` → sirf public methods (including inherited from Object class like toString(), hashCode()).
64+
5. `getConstructors()` → sirf public constructors deta hai.
65+
6. `getParameters()` → ek method ke parameter list ka detail deta hai.
66+
67+
7. Reflection ka use:
68+
- Frameworks (Spring, Hibernate) → objects ko dynamically inspect aur manipulate karna.
69+
- Tools (JUnit, debuggers) → runtime par methods ko call karna ya inject karna.
70+
8. Cons:
71+
- Slow performance (direct call se slower).
72+
- Security risks (private fields/methods access karne ki ability).
73+
*/

0 commit comments

Comments
 (0)