Hi All,
We can execute/call/invoke private method of a Class with the help of Reflection and Mockit.
I give an example in which i have one class Student and that class having one private method showStudentDetails.
I have one another class StudentHack in which i execute that private method in two different methods.
Let us have a Student class
package com.om.demo;
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private void showStudentDetails(){
System.out.println("Student Name : "+name);
}
}
Let us have another class StudentHack which Hack Student class
package com.om.demo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import mockit.Deencapsulation;
public class StudentHack {
// Object of Student class
private Student student = null;
// Name of the private method
private String methodName = "showStudentDetails";
{
student = new Student();
student.setName("Ram");
}
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException,
NoSuchMethodException, SecurityException {
StudentHack studentHack = new StudentHack();
studentHack.callPrivateMethodWithReflection();
studentHack.callPrivateMethodWithMockit();
}
public void callPrivateMethodWithReflection()
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException, SecurityException {
// Name of the class
Class c = Student.class;
// Create method instance
Method method = c.getDeclaredMethod(methodName);
// Allow to access private method
method.setAccessible(true);
// call private method
method.invoke(student, new Object[] {});
}
public void callPrivateMethodWithMockit() {
Deencapsulation.invoke(student, "showStudentDetails");
}
}
As shown above we can execute private method of a class with the help of Reflection and Mockit
Call private method with Reflection
Step 1. Create instance of java.lang.reflect.Method i.e. Method method = c.getDeclaredMethod(methodName);
where methodName is an argument, which we want to call
Step 2. Allow to access private method i.e. method.setAccessible(true);
Step 3. Invoke private method i.e. method.invoke(student, new Object[] {});
Here new Object[] {} is the array of objects which represents argument of method.
If method contains argument then we need to pass arguments here.
Call private method with Mockit
Step 1. Deencapsulation.invoke(student, "showStudentDetails");
If method contains argument then we pass array of Object as 3rd argument.
i.e. Deencapsulation.invoke(student, "showStudentDetails", new Object[] {});
We can execute/call/invoke private method of a Class with the help of Reflection and Mockit.
I give an example in which i have one class Student and that class having one private method showStudentDetails.
I have one another class StudentHack in which i execute that private method in two different methods.
Let us have a Student class
package com.om.demo;
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private void showStudentDetails(){
System.out.println("Student Name : "+name);
}
}
Let us have another class StudentHack which Hack Student class
package com.om.demo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import mockit.Deencapsulation;
public class StudentHack {
// Object of Student class
private Student student = null;
// Name of the private method
private String methodName = "showStudentDetails";
{
student = new Student();
student.setName("Ram");
}
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException, InvocationTargetException,
NoSuchMethodException, SecurityException {
StudentHack studentHack = new StudentHack();
studentHack.callPrivateMethodWithReflection();
studentHack.callPrivateMethodWithMockit();
}
public void callPrivateMethodWithReflection()
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException, NoSuchMethodException, SecurityException {
// Name of the class
Class c = Student.class;
// Create method instance
Method method = c.getDeclaredMethod(methodName);
// Allow to access private method
method.setAccessible(true);
// call private method
method.invoke(student, new Object[] {});
}
public void callPrivateMethodWithMockit() {
Deencapsulation.invoke(student, "showStudentDetails");
}
}
As shown above we can execute private method of a class with the help of Reflection and Mockit
Call private method with Reflection
Step 1. Create instance of java.lang.reflect.Method i.e. Method method = c.getDeclaredMethod(methodName);
where methodName is an argument, which we want to call
Step 2. Allow to access private method i.e. method.setAccessible(true);
Step 3. Invoke private method i.e. method.invoke(student, new Object[] {});
Here new Object[] {} is the array of objects which represents argument of method.
If method contains argument then we need to pass arguments here.
Call private method with Mockit
Step 1. Deencapsulation.invoke(student, "showStudentDetails");
If method contains argument then we pass array of Object as 3rd argument.
i.e. Deencapsulation.invoke(student, "showStudentDetails", new Object[] {});
No comments:
Post a Comment