Refactor introspector
This commit is contained in:
parent
c9cd941fc7
commit
9cb18aebba
|
@ -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<String> getNullPropertiesList(Customer customer) throws Exception {
|
||||
static List<String> getNullPropertiesList(Customer customer) throws Exception {
|
||||
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))
|
||||
.map(PropertyDescriptor::getName)
|
||||
.collect(Collectors.toList());
|
||||
return nullProps;
|
||||
}
|
||||
|
||||
private static Predicate<PropertyDescriptor> nulls(Customer customer) {
|
||||
Predicate<PropertyDescriptor> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<String> result = BaeldungReflectionUtils.getNullPropertiesList(customer);
|
||||
List<String> 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));
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue