Friday, 14 June 2013

How To Access Private Members of a Java class using Reflection


How To Access Private Members of a Java class using Reflection


How To Access Private Members of a Java class using Reflection.


java.lang.reflect package provides the AccessibleObject class that is parent class of Constructor, Method, and Field class. Using the AccessibleObject class, we can change the access control flags of a Reflection Object (Reflection is a feature in the Java programming language. It allows an executing Java program to examine or “introspect” upon itself, and manipulate internal properties of the program. For example, it’s possible for a Java class to obtain the names of all its members and display them.), and the setAccessible(boolean)method is used to change the access control flag of an Object.

For Example :


package test;

public class PrivateAccessMember {
private int privateInt = 10;
private String privateString = "temp String";
private long privateLong = 1234567890L;
private void resetFields(int i, String s, long l) {
privateInt = i;
privateString = s;
privateLong = l;
}
public void display() {
System.out.println("Int = " + privateInt);
System.out.println("String = " + privateString);
System.out.println("Long = " + privateLong);
}

}


package test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestPrivateMember {
public static void main(String[] args) {
PrivateAccessMember obj = new PrivateAccessMember();
obj.display();
Class clazz = obj.getClass();
try {
Field intField = clazz.getDeclaredField("privateInt");
Field strField = clazz.getDeclaredField("privateString");
Field longField = clazz.getDeclaredField("privateLong");
intField.setAccessible(true);
strField.setAccessible(true);
longField.setAccessible(true);
System.out.println("value of privateInt field is : " + intField.get(obj));
System.out.println("value of privateString field is : " + strField.get(obj));
System.out.println("value of privateLong field is : " + longField.get(obj));
intField.set(obj, 100);
strField.set(obj, "this is new string");
longField.set(obj, 55555L);
obj.display();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
try {
Method method = clazz.getDeclaredMethod("resetFields", int.class, String.class, long.class);
method.setAccessible(true);
method.invoke(obj, 25, "new String from resetField method.", 987654321L);
obj.display();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}


The Output are :

Int = 10
String = temp String
Long = 1234567890
value of privateInt field is : 10
value of privateString field is : temp String
value of privateLong field is : 1234567890
Int = 100
String = this is new string
Long = 55555
Int = 25
String = new String from resetField method.
Long = 987654321

No comments:

Post a Comment