From 58a9ea09393025aa9ed9d3066488b58eca68e390 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Wed, 7 Jun 2017 09:12:49 +0100 Subject: [PATCH] BAEL-869 - Java 8 example of listing null properties --- .../reflection/BaeldungReflectionUtils.java | 43 ++++++++++++++ .../com/baeldung/reflection/Customer.java | 58 +++++++++++++++++++ .../BaeldungReflectionUtilsTest.java | 23 ++++++++ 3 files changed, 124 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java create mode 100644 core-java/src/main/java/com/baeldung/reflection/Customer.java create mode 100644 core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java diff --git a/core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java b/core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java new file mode 100644 index 0000000000..a5b3bf8d2d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java @@ -0,0 +1,43 @@ +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; + +public class BaeldungReflectionUtils { + + private static final Logger LOG = LoggerFactory.getLogger(BaeldungReflectionUtils.class); + + + public static List getNullPropertiesList(Customer customer) throws Exception { + PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors(); + List propDescList = Arrays.asList(propDescArr); + + List nullProps = propDescList.stream() + .filter(nulls(customer)) + .map(PropertyDescriptor::getName) + .collect(Collectors.toList()); + return nullProps; + } + + private static Predicate nulls(Customer customer) { + Predicate isNull = pd -> { + Method getterMethod = pd.getReadMethod(); + boolean result = false; + try { + result = (getterMethod != null && getterMethod.invoke(customer) == null); + } catch (Exception e) { + LOG.error("error invoking getter method"); + } + return result; + }; + return isNull; + } +} diff --git a/core-java/src/main/java/com/baeldung/reflection/Customer.java b/core-java/src/main/java/com/baeldung/reflection/Customer.java new file mode 100644 index 0000000000..13d61578fe --- /dev/null +++ b/core-java/src/main/java/com/baeldung/reflection/Customer.java @@ -0,0 +1,58 @@ +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() { + StringBuilder builder = new StringBuilder(); + builder.append("Customer [id=").append(id).append(", name=").append(name).append(", emailId=").append(emailId).append(", phoneNumber=") + .append(phoneNumber).append("]"); + return builder.toString(); + } + + public 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; + } + +} diff --git a/core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java b/core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java new file mode 100644 index 0000000000..76916df6ce --- /dev/null +++ b/core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java @@ -0,0 +1,23 @@ +package com.baeldung.reflection; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +public class BaeldungReflectionUtilsTest { + + @Test + public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception { + Customer customer = new Customer(1, "Himanshu", null, null); + + List result = BaeldungReflectionUtils.getNullPropertiesList(customer); + List expectedFieldNames = Arrays.asList("emailId", "phoneNumber"); + + Assert.assertTrue(result.size() == expectedFieldNames.size()); + Assert.assertTrue(result.containsAll(expectedFieldNames)); + + } + +}