[BAEL-6794] Is Java Reflection Bad Practice?
This commit is contained in:
parent
a93751923d
commit
db9dc5f286
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.reflection.disadvantages.encapsulation;
|
||||||
|
|
||||||
|
public class MyClass {
|
||||||
|
|
||||||
|
private String veryPrivateField;
|
||||||
|
|
||||||
|
public MyClass() {
|
||||||
|
|
||||||
|
this.veryPrivateField = "Secret Information";
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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<Person> constructor = Person.class.getDeclaredConstructor(String.class, String.class, Integer.class);
|
||||||
|
Person person = constructor.newInstance("John", "Doe", 50);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user