Skip to content

Commit 6148420

Browse files
committed
Reflections demo has been added
1 parent c283838 commit 6148420

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed

Reflections/.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

Reflections/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Reflections</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
560 Bytes
Binary file not shown.
2.64 KB
Binary file not shown.

Reflections/src/com/cdac/Account.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.cdac;
2+
3+
public class Account {
4+
5+
private int balance;
6+
public Account() {
7+
}
8+
9+
public Account(int balance) {
10+
this.balance = balance;
11+
}
12+
13+
public int getBalance() {
14+
return balance;
15+
}
16+
17+
public void setBalance(int balance) {
18+
this.balance = balance;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.cdac;
2+
3+
import java.lang.reflect.Field;
4+
import java.lang.reflect.InvocationTargetException;
5+
import java.lang.reflect.Method;
6+
7+
public class ReflectionsDemo {
8+
9+
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException,
10+
IllegalArgumentException, IllegalAccessException, InvocationTargetException {
11+
12+
Class c = String.class;
13+
Method[] methods = c.getMethods();
14+
15+
System.out.println("Methods in String class :: ");
16+
for (Method m : methods)
17+
System.out.println(m.getName());
18+
19+
Account account = new Account(5000);
20+
Class accountClass = Class.forName("com.cdac.reflection.Account");
21+
methods = accountClass.getMethods();
22+
System.out.println("Methods in Account class");
23+
for (Method m : methods) {
24+
System.out.println(m.getName());
25+
if (m.getName().contains("set")) {
26+
m.invoke(account, 30000);
27+
}
28+
}
29+
System.out.println("Account Balance :: " + account.getBalance());
30+
31+
// when field is public
32+
/*
33+
* Field f = accountClass.getField("balance"); f.setInt(account, 10000);
34+
* System.out.println(account.getBalance());
35+
*/
36+
37+
// accessing the private fields
38+
Field privateField = accountClass.getDeclaredField("balance");
39+
privateField.setAccessible(true);
40+
privateField.set(account, 20000);
41+
System.out.println("New Balance is :: " + account.getBalance());
42+
43+
Field[] fields = accountClass.getDeclaredFields();
44+
System.out.println("Fields in Account class");
45+
for (Field field : fields)
46+
System.out.println(field.getName());
47+
}
48+
49+
}

0 commit comments

Comments
 (0)