From db9dc5f286bae244ca4467f6443fee09df286c8c Mon Sep 17 00:00:00 2001 From: ovidiu-mihai98 <138307181+ovidiumihaitacu@users.noreply.github.com> Date: Sat, 30 Sep 2023 23:21:21 +0300 Subject: [PATCH 1/3] [BAEL-6794] Is Java Reflection Bad Practice? --- .../disadvantages/encapsulation/MyClass.java | 11 +++++ .../performance/BenchmarkRunner.java | 7 +++ .../performance/InitializationBenchmark.java | 29 +++++++++++ .../MethodInvocationBenchmark.java | 48 +++++++++++++++++++ .../disadvantages/performance/Person.java | 38 +++++++++++++++ .../ReflectionEncapsulationUnitTest.java | 21 ++++++++ 6 files changed, 154 insertions(+) create mode 100644 core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java create mode 100644 core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java create mode 100644 core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java create mode 100644 core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java create mode 100644 core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java create mode 100644 core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java new file mode 100644 index 0000000000..680079adea --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java @@ -0,0 +1,11 @@ +package com.baeldung.reflection.disadvantages.encapsulation; + +public class MyClass { + + private String veryPrivateField; + + public MyClass() { + + this.veryPrivateField = "Secret Information"; + } +} diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java new file mode 100644 index 0000000000..1de026f796 --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java @@ -0,0 +1,7 @@ +package com.baeldung.reflection.disadvantages.performance; + +public class BenchmarkRunner { + public static void main(String[] args) throws Exception { + org.openjdk.jmh.Main.main(args); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java new file mode 100644 index 0000000000..e6c0c3daea --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java @@ -0,0 +1,29 @@ +package com.baeldung.reflection.disadvantages.performance; + +import org.openjdk.jmh.annotations.*; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.concurrent.TimeUnit; + +public class InitializationBenchmark { + + @Benchmark + @Fork(value = 1, warmups = 1) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public void directInit() { + + Person person = new Person("John", "Doe", 50); + } + + @Benchmark + @Fork(value = 1, warmups = 1) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public void reflectiveInit() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + + Constructor constructor = Person.class.getDeclaredConstructor(String.class, String.class, Integer.class); + Person person = constructor.newInstance("John", "Doe", 50); + } +} diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java new file mode 100644 index 0000000000..1128cf8f33 --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java @@ -0,0 +1,48 @@ +package com.baeldung.reflection.disadvantages.performance; + +import org.openjdk.jmh.annotations.*; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.concurrent.TimeUnit; + +public class MethodInvocationBenchmark { + + @Benchmark + @Fork(value = 1, warmups = 1) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public void directCall() { + + directCall(new Person("John", "Doe", 50)); + } + + @Benchmark + @Fork(value = 1, warmups = 1) + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public void reflectiveCall() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { + + reflectiveCall(new Person("John", "Doe", 50)); + } + + + private void directCall(Person person) { + + person.getFirstName(); + person.getLastName(); + person.getAge(); + } + + private void reflectiveCall(Person person) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + + Method getFirstNameMethod = Person.class.getMethod("getFirstName"); + getFirstNameMethod.invoke(person); + + Method getLastNameMethod = Person.class.getMethod("getLastName"); + getLastNameMethod.invoke(person); + + Method getAgeMethod = Person.class.getMethod("getAge"); + getAgeMethod.invoke(person); + } +} diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java new file mode 100644 index 0000000000..014c82ba45 --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java @@ -0,0 +1,38 @@ +package com.baeldung.reflection.disadvantages.performance; + +public class Person { + + private String firstName; + private String lastName; + private Integer age; + + public Person(String firstName, String lastName, Integer age) { + this.firstName = firstName; + this.lastName = lastName; + this.age = age; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } +} diff --git a/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java b/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java new file mode 100644 index 0000000000..efb0a7da31 --- /dev/null +++ b/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.reflection.disadvantages.encapsulation; + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ReflectionEncapsulationUnitTest { + + @Test + public void givenPrivateField_whenUsingReflection_thenIsAccessible() throws IllegalAccessException, NoSuchFieldException { + MyClass myClassInstance = new MyClass(); + + Field privateField = MyClass.class.getDeclaredField("veryPrivateField"); + privateField.setAccessible(true); + + String accessedField = privateField.get(myClassInstance).toString(); + assertEquals(accessedField, "Secret Information"); + } +} From b60089275f70f8651b6439a967da7d2d57cb00de Mon Sep 17 00:00:00 2001 From: ovidiu-mihai98 <138307181+ovidiumihaitacu@users.noreply.github.com> Date: Wed, 11 Oct 2023 20:58:43 +0300 Subject: [PATCH 2/3] Created another module for core-java-reflection --- .../core-java-reflection-3/README.md | 1 + .../core-java-reflection-3/pom.xml | 78 +++++++++++++++++++ .../disadvantages/encapsulation/MyClass.java | 0 .../performance/BenchmarkRunner.java | 0 .../performance/InitializationBenchmark.java | 0 .../MethodInvocationBenchmark.java | 0 .../disadvantages/performance/Person.java | 0 .../ReflectionEncapsulationUnitTest.java | 0 core-java-modules/pom.xml | 1 + 9 files changed, 80 insertions(+) create mode 100644 core-java-modules/core-java-reflection-3/README.md create mode 100644 core-java-modules/core-java-reflection-3/pom.xml rename core-java-modules/{core-java-reflection-2 => core-java-reflection-3}/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java (100%) rename core-java-modules/{core-java-reflection-2 => core-java-reflection-3}/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java (100%) rename core-java-modules/{core-java-reflection-2 => core-java-reflection-3}/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java (100%) rename core-java-modules/{core-java-reflection-2 => core-java-reflection-3}/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java (100%) rename core-java-modules/{core-java-reflection-2 => core-java-reflection-3}/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java (100%) rename core-java-modules/{core-java-reflection-2 => core-java-reflection-3}/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java (100%) diff --git a/core-java-modules/core-java-reflection-3/README.md b/core-java-modules/core-java-reflection-3/README.md new file mode 100644 index 0000000000..e320af31b4 --- /dev/null +++ b/core-java-modules/core-java-reflection-3/README.md @@ -0,0 +1 @@ +### Relevant Articles: \ No newline at end of file diff --git a/core-java-modules/core-java-reflection-3/pom.xml b/core-java-modules/core-java-reflection-3/pom.xml new file mode 100644 index 0000000000..fa5fe897e0 --- /dev/null +++ b/core-java-modules/core-java-reflection-3/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + core-java-reflection-3 + core-java-reflection-3 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + org.springframework + spring-test + ${spring.version} + test + + + org.reflections + reflections + ${reflections.version} + + + + + core-java-reflection-3 + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${source.version} + ${target.version} + -parameters + + + + org.jacoco + jacoco-maven-plugin + 0.8.8 + + + + prepare-agent + + + + report + prepare-package + + report + + + + + + + + + 0.9.12 + 1.8 + 1.8 + 5.3.4 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java similarity index 100% rename from core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java rename to core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/encapsulation/MyClass.java diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java similarity index 100% rename from core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java rename to core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/performance/BenchmarkRunner.java diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java similarity index 100% rename from core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java rename to core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/performance/InitializationBenchmark.java diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java similarity index 100% rename from core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java rename to core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/performance/MethodInvocationBenchmark.java diff --git a/core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java b/core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java similarity index 100% rename from core-java-modules/core-java-reflection-2/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java rename to core-java-modules/core-java-reflection-3/src/main/java/com/baeldung/reflection/disadvantages/performance/Person.java diff --git a/core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java b/core-java-modules/core-java-reflection-3/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java similarity index 100% rename from core-java-modules/core-java-reflection-2/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java rename to core-java-modules/core-java-reflection-3/src/test/java/com/baeldung/reflection/disadvantages/encapsulation/ReflectionEncapsulationUnitTest.java diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index d0cce5e66f..e32ed1dbca 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -155,6 +155,7 @@ core-java-properties core-java-reflection core-java-reflection-2 + core-java-reflection-3 core-java-scanner core-java-security-2 core-java-security-3 From d083e32b1a9faa1b41edac9d03361a5508f5aee8 Mon Sep 17 00:00:00 2001 From: Maiklins Date: Wed, 11 Oct 2023 23:36:00 +0200 Subject: [PATCH 3/3] Update README.md --- core-java-modules/core-java-reflection-3/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-java-modules/core-java-reflection-3/README.md b/core-java-modules/core-java-reflection-3/README.md index e320af31b4..d997aa22f1 100644 --- a/core-java-modules/core-java-reflection-3/README.md +++ b/core-java-modules/core-java-reflection-3/README.md @@ -1 +1,2 @@ -### Relevant Articles: \ No newline at end of file +### Relevant Articles: +- [Is Java Reflection Bad Practice?](https://www.baeldung.com/java-reflection-bad-practice)