[BAEL-2424] Updated tests to illustrate modifiers filtering and to disable order checking
This commit is contained in:
parent
bb966c8e14
commit
2acd1e2792
|
@ -2,6 +2,6 @@ package com.baeldung.reflection;
|
||||||
|
|
||||||
public class MonthEmployee extends Employee {
|
public class MonthEmployee extends Employee {
|
||||||
|
|
||||||
private double reward;
|
protected double reward;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package com.baeldung.reflection;
|
||||||
|
|
||||||
public class Person {
|
public class Person {
|
||||||
|
|
||||||
public String lastName;
|
protected String lastName;
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,13 @@ package com.baeldung.reflection;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@ -26,30 +29,14 @@ public class PersonAndEmployeeReflectionUnitTest {
|
||||||
// Then
|
// Then
|
||||||
assertEquals(2, allFields.length);
|
assertEquals(2, allFields.length);
|
||||||
|
|
||||||
Field lastNameField = allFields[0];
|
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||||
assertEquals(LAST_NAME_FIELD, lastNameField.getName());
|
field.getName().equals(LAST_NAME_FIELD)
|
||||||
assertEquals(String.class, lastNameField.getType());
|
&& field.getType().equals(String.class))
|
||||||
|
);
|
||||||
Field firstNameField = allFields[1];
|
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||||
assertEquals(FIRST_NAME_FIELD, firstNameField.getName());
|
field.getName().equals(FIRST_NAME_FIELD)
|
||||||
assertEquals(String.class, firstNameField.getType());
|
&& field.getType().equals(String.class))
|
||||||
}
|
);
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenEmployeeClass_whenSuperClassGetDeclaredFields_thenOneField() {
|
|
||||||
// When
|
|
||||||
Field[] allFields = Employee.class.getSuperclass().getDeclaredFields();
|
|
||||||
|
|
||||||
// Then
|
|
||||||
assertEquals(2, allFields.length);
|
|
||||||
|
|
||||||
Field lastNameField = allFields[0];
|
|
||||||
assertEquals(LAST_NAME_FIELD, lastNameField.getName());
|
|
||||||
assertEquals(String.class, lastNameField.getType());
|
|
||||||
|
|
||||||
Field firstNameField = allFields[1];
|
|
||||||
assertEquals(FIRST_NAME_FIELD, firstNameField.getName());
|
|
||||||
assertEquals(String.class, firstNameField.getType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -60,9 +47,28 @@ public class PersonAndEmployeeReflectionUnitTest {
|
||||||
// Then
|
// Then
|
||||||
assertEquals(1, allFields.length);
|
assertEquals(1, allFields.length);
|
||||||
|
|
||||||
Field employeeIdField = allFields[0];
|
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||||
assertEquals(EMPLOYEE_ID_FIELD, employeeIdField.getName());
|
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||||
assertEquals(int.class, employeeIdField.getType());
|
&& field.getType().equals(int.class))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmployeeClass_whenSuperClassGetDeclaredFields_thenOneField() {
|
||||||
|
// When
|
||||||
|
Field[] allFields = Employee.class.getSuperclass().getDeclaredFields();
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(2, allFields.length);
|
||||||
|
|
||||||
|
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||||
|
field.getName().equals(LAST_NAME_FIELD)
|
||||||
|
&& field.getType().equals(String.class))
|
||||||
|
);
|
||||||
|
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||||
|
field.getName().equals(FIRST_NAME_FIELD)
|
||||||
|
&& field.getType().equals(String.class))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -76,42 +82,56 @@ public class PersonAndEmployeeReflectionUnitTest {
|
||||||
// Then
|
// Then
|
||||||
assertEquals(3, allFields.length);
|
assertEquals(3, allFields.length);
|
||||||
|
|
||||||
Field lastNameField = allFields[0];
|
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||||
assertEquals(LAST_NAME_FIELD, lastNameField.getName());
|
field.getName().equals(LAST_NAME_FIELD)
|
||||||
assertEquals(String.class, lastNameField.getType());
|
&& field.getType().equals(String.class))
|
||||||
|
);
|
||||||
Field firstNameField = allFields[1];
|
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||||
assertEquals(FIRST_NAME_FIELD, firstNameField.getName());
|
field.getName().equals(FIRST_NAME_FIELD)
|
||||||
assertEquals(String.class, firstNameField.getType());
|
&& field.getType().equals(String.class))
|
||||||
|
);
|
||||||
Field employeeIdField = allFields[2];
|
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||||
assertEquals(EMPLOYEE_ID_FIELD, employeeIdField.getName());
|
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||||
assertEquals(int.class, employeeIdField.getType());
|
&& field.getType().equals(int.class))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMonthEmployeeClass_whenGetAllFields_thenFourFields() {
|
public void givenEmployeeClass_whenGetDeclaredFieldsOnEmployeeSuperclassWithModifiersFilter_thenOneFields() {
|
||||||
|
// When
|
||||||
|
List<Field> personFields = Arrays.stream(Employee.class.getSuperclass().getDeclaredFields())
|
||||||
|
.filter(f -> Modifier.isPublic(f.getModifiers()) || Modifier.isProtected(f.getModifiers()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(1, personFields.size());
|
||||||
|
|
||||||
|
assertTrue(personFields.stream().anyMatch(field ->
|
||||||
|
field.getName().equals(LAST_NAME_FIELD)
|
||||||
|
&& field.getType().equals(String.class))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMonthEmployeeClass_whenGetAllFields_thenThreeFields() {
|
||||||
// When
|
// When
|
||||||
List<Field> allFields = getAllFields(MonthEmployee.class);
|
List<Field> allFields = getAllFields(MonthEmployee.class);
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
assertEquals(4, allFields.size());
|
assertEquals(3, allFields.size());
|
||||||
|
|
||||||
Field lastNameField = allFields.get(0);
|
assertTrue(allFields.stream().anyMatch(field ->
|
||||||
assertEquals(LAST_NAME_FIELD, lastNameField.getName());
|
field.getName().equals(LAST_NAME_FIELD)
|
||||||
assertEquals(String.class, lastNameField.getType());
|
&& field.getType().equals(String.class))
|
||||||
|
);
|
||||||
Field firstNameField = allFields.get(1);
|
assertTrue(allFields.stream().anyMatch(field ->
|
||||||
assertEquals(FIRST_NAME_FIELD, firstNameField.getName());
|
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||||
assertEquals(String.class, firstNameField.getType());
|
&& field.getType().equals(int.class))
|
||||||
|
);
|
||||||
Field employeeIdField = allFields.get(2);
|
assertTrue(allFields.stream().anyMatch(field ->
|
||||||
assertEquals(EMPLOYEE_ID_FIELD, employeeIdField.getName());
|
field.getName().equals(MONTH_EMPLOYEE_REWARD_FIELD)
|
||||||
assertEquals(int.class, employeeIdField.getType());
|
&& field.getType().equals(double.class))
|
||||||
|
);
|
||||||
Field monthEmployeeRewardField = allFields.get(3);
|
|
||||||
assertEquals(MONTH_EMPLOYEE_REWARD_FIELD, monthEmployeeRewardField.getName());
|
|
||||||
assertEquals(double.class, monthEmployeeRewardField.getType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Field> getAllFields(Class clazz) {
|
public List<Field> getAllFields(Class clazz) {
|
||||||
|
@ -119,9 +139,11 @@ public class PersonAndEmployeeReflectionUnitTest {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Field> result = new ArrayList<>();
|
List<Field> result = new ArrayList<>(getAllFields(clazz.getSuperclass()));
|
||||||
result.addAll(getAllFields(clazz.getSuperclass()));
|
List<Field> filteredFields = Arrays.stream(clazz.getDeclaredFields())
|
||||||
result.addAll(Arrays.asList(clazz.getDeclaredFields()));
|
.filter(f -> Modifier.isPublic(f.getModifiers()) || Modifier.isProtected(f.getModifiers()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
result.addAll(filteredFields);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue