Added code to access private properties of a class

This commit is contained in:
Umang Budhwar 2020-06-05 23:56:54 +05:30
parent 315c91acc3
commit 566d795759
4 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package com.baeldung.reflection.access.privatefields;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class AccessPrivateProperties {
public void accessPrivateFields(String declaredField, Object declaredFieldValue, boolean accessible) throws NoSuchFieldException, NullPointerException, IllegalArgumentException, IllegalAccessException {
Person person = new Person();
Field field = person.getClass()
.getDeclaredField(declaredField);
field.setAccessible(accessible);
field.set(person, declaredFieldValue);
System.out.println(person.getName());
}
public void accessPrivateMethods(String declaredMethod, Class<?> parameterType, Object name, boolean accessible) throws NoSuchMethodException, IllegalAccessException, NullPointerException, IllegalArgumentException, InvocationTargetException {
Person person = new Person();
Method method = person.getClass()
.getDeclaredMethod(declaredMethod, parameterType);
method.setAccessible(accessible);
String greetMessage = (String) method.invoke(person, name);
System.out.println(greetMessage);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.reflection.access.privatefields;
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String greet(String number) {
return "Hi " + Integer.parseInt(number);
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.reflection.access.privatefields;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class AccessPrivateFieldsUnitTest {
private static AccessPrivateProperties accessPrivateProperties;
@BeforeAll
public static void beforeAll() {
accessPrivateProperties = new AccessPrivateProperties();
}
@Test
public void testForNoSuchFieldException() {
Assertions.assertThrows(NoSuchFieldException.class, () -> accessPrivateProperties.accessPrivateFields("firstName", "John", true));
}
@Test
public void testForIllegalAccessException() {
Assertions.assertThrows(IllegalAccessException.class, () -> accessPrivateProperties.accessPrivateFields("name", "John", false));
}
@Test
public void testForIllegalArgumentException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> accessPrivateProperties.accessPrivateFields("name", 25, true));
}
@Test
public void testForNullPointerException() {
Assertions.assertThrows(NullPointerException.class, () -> accessPrivateProperties.accessPrivateFields(null, "John", true));
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.reflection.access.privatefields;
import java.lang.reflect.InvocationTargetException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
public class AccessPrivateMethodsUnitTest {
private static AccessPrivateProperties accessPrivateProperties;
@BeforeAll
public static void beforeAll() {
accessPrivateProperties = new AccessPrivateProperties();
}
@Test
public void testForNoSuchMethodException() {
Assertions.assertThrows(NoSuchMethodException.class, () -> accessPrivateProperties.accessPrivateMethods("greeting", String.class, "5", true));
}
@Test
public void testForIllegalAccessException() {
Assertions.assertThrows(IllegalAccessException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, "5", false));
}
@Test
public void testForIllegalArgumentException() {
Assertions.assertThrows(IllegalArgumentException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, 5, true));
}
@Test
public void testForNullPointerException() {
Assertions.assertThrows(NullPointerException.class, () -> accessPrivateProperties.accessPrivateMethods(null, String.class, "5", true));
}
@Test
public void testForInvocationTargetException() {
Assertions.assertThrows(InvocationTargetException.class, () -> accessPrivateProperties.accessPrivateMethods("greet", String.class, "John", true));
}
}