Merge pull request #2034 from eugenp/introspector-refactor

Introspector refactor
This commit is contained in:
slavisa-baeldung 2017-06-10 12:40:28 +02:00 committed by GitHub
commit 4591eecddb
3 changed files with 15 additions and 20 deletions

View File

@ -11,33 +11,29 @@ import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class BaeldungReflectionUtils { class BaeldungReflectionUtils {
private static final Logger LOG = LoggerFactory.getLogger(BaeldungReflectionUtils.class); private static final Logger LOG = LoggerFactory.getLogger(BaeldungReflectionUtils.class);
static List<String> getNullPropertiesList(Customer customer) throws Exception {
public static List<String> getNullPropertiesList(Customer customer) throws Exception {
PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors(); PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors();
List<PropertyDescriptor> propDescList = Arrays.asList(propDescArr);
List<String> nullProps = propDescList.stream() return Arrays.stream(propDescArr)
.filter(nulls(customer)) .filter(nulls(customer))
.map(PropertyDescriptor::getName) .map(PropertyDescriptor::getName)
.collect(Collectors.toList()); .collect(Collectors.toList());
return nullProps;
} }
private static Predicate<PropertyDescriptor> nulls(Customer customer) { private static Predicate<PropertyDescriptor> nulls(Customer customer) {
Predicate<PropertyDescriptor> isNull = pd -> { return pd -> {
Method getterMethod = pd.getReadMethod();
boolean result = false; boolean result = false;
try { try {
Method getterMethod = pd.getReadMethod();
result = (getterMethod != null && getterMethod.invoke(customer) == null); result = (getterMethod != null && getterMethod.invoke(customer) == null);
} catch (Exception e) { } catch (Exception e) {
LOG.error("error invoking getter method"); LOG.error("error invoking getter method");
} }
return result; return result;
}; };
return isNull;
} }
} }

View File

@ -33,13 +33,11 @@ public class Customer {
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); return "Customer [id=" + id + ", name=" + name + ", emailId=" + emailId + ", phoneNumber=" +
builder.append("Customer [id=").append(id).append(", name=").append(name).append(", emailId=").append(emailId).append(", phoneNumber=") phoneNumber + "]";
.append(phoneNumber).append("]");
return builder.toString();
} }
public Customer(Integer id, String name, String emailId, Long phoneNumber) { Customer(Integer id, String name, String emailId, Long phoneNumber) {
super(); super();
this.id = id; this.id = id;
this.name = name; this.name = name;

View File

@ -1,11 +1,12 @@
package com.baeldung.reflection; package com.baeldung.reflection;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertTrue;
public class BaeldungReflectionUtilsTest { public class BaeldungReflectionUtilsTest {
@Test @Test
@ -15,8 +16,8 @@ public class BaeldungReflectionUtilsTest {
List<String> result = BaeldungReflectionUtils.getNullPropertiesList(customer); List<String> result = BaeldungReflectionUtils.getNullPropertiesList(customer);
List<String> expectedFieldNames = Arrays.asList("emailId", "phoneNumber"); List<String> expectedFieldNames = Arrays.asList("emailId", "phoneNumber");
Assert.assertTrue(result.size() == expectedFieldNames.size()); assertTrue(result.size() == expectedFieldNames.size());
Assert.assertTrue(result.containsAll(expectedFieldNames)); assertTrue(result.containsAll(expectedFieldNames));
} }