[BAEL-13505] Move articles out of core-java part 1
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
## Relevant Articles
|
||||
|
||||
- [Void Type in Java](https://www.baeldung.com/java-void-type)
|
||||
- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields)
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class BaeldungReflectionUtils {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(BaeldungReflectionUtils.class);
|
||||
|
||||
static List<String> getNullPropertiesList(Customer customer) throws Exception {
|
||||
PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors();
|
||||
|
||||
return Arrays.stream(propDescArr)
|
||||
.filter(nulls(customer))
|
||||
.map(PropertyDescriptor::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static Predicate<PropertyDescriptor> nulls(Customer customer) {
|
||||
return pd -> {
|
||||
boolean result = false;
|
||||
try {
|
||||
Method getterMethod = pd.getReadMethod();
|
||||
result = (getterMethod != null && getterMethod.invoke(customer) == null);
|
||||
} catch (Exception e) {
|
||||
LOG.error("error invoking getter method");
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String emailId;
|
||||
private Long phoneNumber;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmailId() {
|
||||
return emailId;
|
||||
}
|
||||
|
||||
public void setEmailId(String emailId) {
|
||||
this.emailId = emailId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [id=" + id + ", name=" + name + ", emailId=" + emailId + ", phoneNumber=" +
|
||||
phoneNumber + "]";
|
||||
}
|
||||
|
||||
Customer(Integer id, String name, String emailId, Long phoneNumber) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.emailId = emailId;
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public Long getPhoneNumber() {
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
public void setPhoneNumber(Long phoneNumber) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Employee extends Person {
|
||||
|
||||
public int employeeId;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class MonthEmployee extends Employee {
|
||||
|
||||
protected double reward;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
public class Person {
|
||||
|
||||
protected String lastName;
|
||||
private String firstName;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class BaeldungReflectionUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception {
|
||||
Customer customer = new Customer(1, "Himanshu", null, null);
|
||||
|
||||
List<String> result = BaeldungReflectionUtils.getNullPropertiesList(customer);
|
||||
List<String> expectedFieldNames = Arrays.asList("emailId", "phoneNumber");
|
||||
|
||||
assertTrue(result.size() == expectedFieldNames.size());
|
||||
assertTrue(result.containsAll(expectedFieldNames));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package com.baeldung.reflection;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PersonAndEmployeeReflectionUnitTest {
|
||||
|
||||
// Fields names
|
||||
private static final String LAST_NAME_FIELD = "lastName";
|
||||
private static final String FIRST_NAME_FIELD = "firstName";
|
||||
private static final String EMPLOYEE_ID_FIELD = "employeeId";
|
||||
private static final String MONTH_EMPLOYEE_REWARD_FIELD = "reward";
|
||||
|
||||
@Test
|
||||
public void givenPersonClass_whenGetDeclaredFields_thenTwoFields() {
|
||||
// When
|
||||
Field[] allFields = Person.class.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
|
||||
public void givenEmployeeClass_whenGetDeclaredFields_thenOneField() {
|
||||
// When
|
||||
Field[] allFields = Employee.class.getDeclaredFields();
|
||||
|
||||
// Then
|
||||
assertEquals(1, allFields.length);
|
||||
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||
&& 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
|
||||
public void givenEmployeeClass_whenGetDeclaredFieldsOnBothClasses_thenThreeFields() {
|
||||
// When
|
||||
Field[] personFields = Employee.class.getSuperclass().getDeclaredFields();
|
||||
Field[] employeeFields = Employee.class.getDeclaredFields();
|
||||
Field[] allFields = new Field[employeeFields.length + personFields.length];
|
||||
Arrays.setAll(allFields, i -> (i < personFields.length ? personFields[i] : employeeFields[i - personFields.length]));
|
||||
|
||||
// Then
|
||||
assertEquals(3, 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))
|
||||
);
|
||||
assertTrue(Arrays.stream(allFields).anyMatch(field ->
|
||||
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||
&& field.getType().equals(int.class))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
List<Field> allFields = getAllFields(MonthEmployee.class);
|
||||
|
||||
// Then
|
||||
assertEquals(3, allFields.size());
|
||||
|
||||
assertTrue(allFields.stream().anyMatch(field ->
|
||||
field.getName().equals(LAST_NAME_FIELD)
|
||||
&& field.getType().equals(String.class))
|
||||
);
|
||||
assertTrue(allFields.stream().anyMatch(field ->
|
||||
field.getName().equals(EMPLOYEE_ID_FIELD)
|
||||
&& field.getType().equals(int.class))
|
||||
);
|
||||
assertTrue(allFields.stream().anyMatch(field ->
|
||||
field.getName().equals(MONTH_EMPLOYEE_REWARD_FIELD)
|
||||
&& field.getType().equals(double.class))
|
||||
);
|
||||
}
|
||||
|
||||
public List<Field> getAllFields(Class clazz) {
|
||||
if (clazz == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<Field> result = new ArrayList<>(getAllFields(clazz.getSuperclass()));
|
||||
List<Field> filteredFields = Arrays.stream(clazz.getDeclaredFields())
|
||||
.filter(f -> Modifier.isPublic(f.getModifiers()) || Modifier.isProtected(f.getModifiers()))
|
||||
.collect(Collectors.toList());
|
||||
result.addAll(filteredFields);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user