Skip to content

Commit cdc9f8f

Browse files
Merge pull request eugenp#2008 from eugenp/BAEL-839
BAEL-869 - Java 8 example of listing null properties
2 parents 0053a80 + 58a9ea0 commit cdc9f8f

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.reflection;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.beans.Introspector;
7+
import java.beans.PropertyDescriptor;
8+
import java.lang.reflect.Method;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.function.Predicate;
12+
import java.util.stream.Collectors;
13+
14+
public class BaeldungReflectionUtils {
15+
16+
private static final Logger LOG = LoggerFactory.getLogger(BaeldungReflectionUtils.class);
17+
18+
19+
public static List<String> getNullPropertiesList(Customer customer) throws Exception {
20+
PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors();
21+
List<PropertyDescriptor> propDescList = Arrays.asList(propDescArr);
22+
23+
List<String> nullProps = propDescList.stream()
24+
.filter(nulls(customer))
25+
.map(PropertyDescriptor::getName)
26+
.collect(Collectors.toList());
27+
return nullProps;
28+
}
29+
30+
private static Predicate<PropertyDescriptor> nulls(Customer customer) {
31+
Predicate<PropertyDescriptor> isNull = pd -> {
32+
Method getterMethod = pd.getReadMethod();
33+
boolean result = false;
34+
try {
35+
result = (getterMethod != null && getterMethod.invoke(customer) == null);
36+
} catch (Exception e) {
37+
LOG.error("error invoking getter method");
38+
}
39+
return result;
40+
};
41+
return isNull;
42+
}
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.reflection;
2+
3+
public class Customer {
4+
5+
private Integer id;
6+
private String name;
7+
private String emailId;
8+
private Long phoneNumber;
9+
10+
public Integer getId() {
11+
return id;
12+
}
13+
14+
public void setId(Integer id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public String getEmailId() {
27+
return emailId;
28+
}
29+
30+
public void setEmailId(String emailId) {
31+
this.emailId = emailId;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
StringBuilder builder = new StringBuilder();
37+
builder.append("Customer [id=").append(id).append(", name=").append(name).append(", emailId=").append(emailId).append(", phoneNumber=")
38+
.append(phoneNumber).append("]");
39+
return builder.toString();
40+
}
41+
42+
public Customer(Integer id, String name, String emailId, Long phoneNumber) {
43+
super();
44+
this.id = id;
45+
this.name = name;
46+
this.emailId = emailId;
47+
this.phoneNumber = phoneNumber;
48+
}
49+
50+
public Long getPhoneNumber() {
51+
return phoneNumber;
52+
}
53+
54+
public void setPhoneNumber(Long phoneNumber) {
55+
this.phoneNumber = phoneNumber;
56+
}
57+
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.reflection;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
public class BaeldungReflectionUtilsTest {
10+
11+
@Test
12+
public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception {
13+
Customer customer = new Customer(1, "Himanshu", null, null);
14+
15+
List<String> result = BaeldungReflectionUtils.getNullPropertiesList(customer);
16+
List<String> expectedFieldNames = Arrays.asList("emailId", "phoneNumber");
17+
18+
Assert.assertTrue(result.size() == expectedFieldNames.size());
19+
Assert.assertTrue(result.containsAll(expectedFieldNames));
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)