forked from TalhaBinAshraf1/SeleniumTestingInternetHerokuApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessPrivateMethods.java
More file actions
32 lines (23 loc) · 973 Bytes
/
Copy pathAccessPrivateMethods.java
File metadata and controls
32 lines (23 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package allTests;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import resources.ClassWithPrivateMethod;
public class AccessPrivateMethods {
int a=10;
static int b=15;
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
//To get the private method from a different class
ClassWithPrivateMethod obj = new ClassWithPrivateMethod();
//get the class of the object
Class<?> class1 = obj.getClass();
//from the class get the declared method
Method method = class1.getDeclaredMethod("privateMethod", null);
method.setAccessible(true);
method.invoke(obj, null);
//To get the private field from a different class
Field field = class1.getDeclaredField("var1");
field.setAccessible(true);
System.out.println(field.get(obj));
}
}