From 566d7957592c40fcb28bd98484e6c8eec759382c Mon Sep 17 00:00:00 2001 From: Umang Budhwar Date: Fri, 5 Jun 2020 23:56:54 +0530 Subject: [PATCH] Added code to access private properties of a class --- .../AccessPrivateProperties.java | 27 ++++++++++++ .../access/privatefields/Person.java | 18 ++++++++ .../AccessPrivateFieldsUnitTest.java | 36 ++++++++++++++++ .../AccessPrivateMethodsUnitTest.java | 43 +++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100755 core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/AccessPrivateProperties.java create mode 100755 core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/Person.java create mode 100644 core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateFieldsUnitTest.java create mode 100644 core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateMethodsUnitTest.java diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/AccessPrivateProperties.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/AccessPrivateProperties.java new file mode 100755 index 0000000000..938f5fd4a2 --- /dev/null +++ b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/AccessPrivateProperties.java @@ -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); + } + +} diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/Person.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/Person.java new file mode 100755 index 0000000000..e59ff3bea8 --- /dev/null +++ b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/access/privatefields/Person.java @@ -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); + } +} diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateFieldsUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateFieldsUnitTest.java new file mode 100644 index 0000000000..8faea06f78 --- /dev/null +++ b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateFieldsUnitTest.java @@ -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)); + } + +} diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateMethodsUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateMethodsUnitTest.java new file mode 100644 index 0000000000..a2e0f3fe28 --- /dev/null +++ b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/access/privatefields/AccessPrivateMethodsUnitTest.java @@ -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)); + } + +}