From 9cb18aebbae75a47cac665d18b36f2a5f69d18c6 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 10 Jun 2017 11:58:42 +0200 Subject: [PATCH] Refactor introspector --- .../reflection/BaeldungReflectionUtils.java | 20 ++++++++----------- .../com/baeldung/reflection/Customer.java | 8 +++----- .../BaeldungReflectionUtilsTest.java | 7 ++++--- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java b/core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java index a5b3bf8d2d..45c13f735d 100644 --- a/core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java +++ b/core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java @@ -11,33 +11,29 @@ import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; -public class BaeldungReflectionUtils { +class BaeldungReflectionUtils { private static final Logger LOG = LoggerFactory.getLogger(BaeldungReflectionUtils.class); - - public static List getNullPropertiesList(Customer customer) throws Exception { + 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; + return Arrays.stream(propDescArr) + .filter(nulls(customer)) + .map(PropertyDescriptor::getName) + .collect(Collectors.toList()); } private static Predicate nulls(Customer customer) { - Predicate isNull = pd -> { - Method getterMethod = pd.getReadMethod(); + 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; }; - 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 index 13d61578fe..a0239f7239 100644 --- a/core-java/src/main/java/com/baeldung/reflection/Customer.java +++ b/core-java/src/main/java/com/baeldung/reflection/Customer.java @@ -33,13 +33,11 @@ public class Customer { @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(); + return "Customer [id=" + id + ", name=" + name + ", emailId=" + emailId + ", phoneNumber=" + + phoneNumber + "]"; } - public Customer(Integer id, String name, String emailId, Long phoneNumber) { + Customer(Integer id, String name, String emailId, Long phoneNumber) { super(); this.id = id; this.name = name; diff --git a/core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java b/core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java index 76916df6ce..bba867f50f 100644 --- a/core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java +++ b/core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java @@ -1,11 +1,12 @@ package com.baeldung.reflection; -import org.junit.Assert; import org.junit.Test; import java.util.Arrays; import java.util.List; +import static org.junit.Assert.assertTrue; + public class BaeldungReflectionUtilsTest { @Test @@ -15,8 +16,8 @@ public class BaeldungReflectionUtilsTest { List result = BaeldungReflectionUtils.getNullPropertiesList(customer); List expectedFieldNames = Arrays.asList("emailId", "phoneNumber"); - Assert.assertTrue(result.size() == expectedFieldNames.size()); - Assert.assertTrue(result.containsAll(expectedFieldNames)); + assertTrue(result.size() == expectedFieldNames.size()); + assertTrue(result.containsAll(expectedFieldNames)); }