Skip to content

Commit 9cb18ae

Browse files
committed
Refactor introspector
1 parent c9cd941 commit 9cb18ae

3 files changed

Lines changed: 15 additions & 20 deletions

File tree

‎core-java/src/main/java/com/baeldung/reflection/BaeldungReflectionUtils.java‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,29 @@
1111
import java.util.function.Predicate;
1212
import java.util.stream.Collectors;
1313

14-
public class BaeldungReflectionUtils {
14+
class BaeldungReflectionUtils {
1515

1616
private static final Logger LOG = LoggerFactory.getLogger(BaeldungReflectionUtils.class);
1717

18-
19-
public static List<String> getNullPropertiesList(Customer customer) throws Exception {
18+
static List<String> getNullPropertiesList(Customer customer) throws Exception {
2019
PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors();
21-
List<PropertyDescriptor> propDescList = Arrays.asList(propDescArr);
2220

23-
List<String> nullProps = propDescList.stream()
24-
.filter(nulls(customer))
25-
.map(PropertyDescriptor::getName)
26-
.collect(Collectors.toList());
27-
return nullProps;
21+
return Arrays.stream(propDescArr)
22+
.filter(nulls(customer))
23+
.map(PropertyDescriptor::getName)
24+
.collect(Collectors.toList());
2825
}
2926

3027
private static Predicate<PropertyDescriptor> nulls(Customer customer) {
31-
Predicate<PropertyDescriptor> isNull = pd -> {
32-
Method getterMethod = pd.getReadMethod();
28+
return pd -> {
3329
boolean result = false;
3430
try {
31+
Method getterMethod = pd.getReadMethod();
3532
result = (getterMethod != null && getterMethod.invoke(customer) == null);
3633
} catch (Exception e) {
3734
LOG.error("error invoking getter method");
3835
}
3936
return result;
4037
};
41-
return isNull;
4238
}
4339
}

‎core-java/src/main/java/com/baeldung/reflection/Customer.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ public void setEmailId(String emailId) {
3333

3434
@Override
3535
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();
36+
return "Customer [id=" + id + ", name=" + name + ", emailId=" + emailId + ", phoneNumber=" +
37+
phoneNumber + "]";
4038
}
4139

42-
public Customer(Integer id, String name, String emailId, Long phoneNumber) {
40+
Customer(Integer id, String name, String emailId, Long phoneNumber) {
4341
super();
4442
this.id = id;
4543
this.name = name;

‎core-java/src/test/java/com/baeldung/reflection/BaeldungReflectionUtilsTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.baeldung.reflection;
22

3-
import org.junit.Assert;
43
import org.junit.Test;
54

65
import java.util.Arrays;
76
import java.util.List;
87

8+
import static org.junit.Assert.assertTrue;
9+
910
public class BaeldungReflectionUtilsTest {
1011

1112
@Test
@@ -15,8 +16,8 @@ public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Except
1516
List<String> result = BaeldungReflectionUtils.getNullPropertiesList(customer);
1617
List<String> expectedFieldNames = Arrays.asList("emailId", "phoneNumber");
1718

18-
Assert.assertTrue(result.size() == expectedFieldNames.size());
19-
Assert.assertTrue(result.containsAll(expectedFieldNames));
19+
assertTrue(result.size() == expectedFieldNames.size());
20+
assertTrue(result.containsAll(expectedFieldNames));
2021

2122
}
2223

0 commit comments

Comments
 (0)