diff --git a/call-all-getters/.gitignore b/call-all-getters/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/call-all-getters/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/call-all-getters/pom.xml b/call-all-getters/pom.xml new file mode 100644 index 0000000000..ed4735a861 --- /dev/null +++ b/call-all-getters/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + baeldung + call-all-getters + 0.0.1-SNAPSHOT + jar + + call-all-getters + Calling all getters using Introspector + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java b/call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java new file mode 100644 index 0000000000..5c10ed5b55 --- /dev/null +++ b/call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java @@ -0,0 +1,18 @@ +package com.baeldung; + +import java.util.List; + +import com.baeldung.reflection.model.Customer; +import com.baeldung.reflection.util.Utils; + +public class CallAllGettersApplication { + + public static void main(String[] args) throws Exception { + + Customer customer = new Customer(1, "Himanshu", null, null); + List nullProps = Utils.getNullPropertiesList(customer); + System.out.println(nullProps); + } + + +} diff --git a/call-all-getters/src/main/java/com/baeldung/reflection/model/Customer.java b/call-all-getters/src/main/java/com/baeldung/reflection/model/Customer.java new file mode 100644 index 0000000000..d0c6c31dce --- /dev/null +++ b/call-all-getters/src/main/java/com/baeldung/reflection/model/Customer.java @@ -0,0 +1,63 @@ +package com.baeldung.reflection.model; + +/** + * + * @author himanshumantri + * + */ +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/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java b/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java new file mode 100644 index 0000000000..c9542bbdf2 --- /dev/null +++ b/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java @@ -0,0 +1,43 @@ +package com.baeldung.reflection.util; + +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; + +import com.baeldung.reflection.model.Customer; + +public class Utils { + + 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 = new Predicate() { + @Override + public boolean test(PropertyDescriptor pd) { + Method getterMethod = pd.getReadMethod(); + boolean result = false; + try { + result = (getterMethod != null && getterMethod.invoke(customer) == null); + } catch (Exception e) { + // Handle the exception + e.printStackTrace(); + } + return result; + } + }; + return isNull; + } +} diff --git a/call-all-getters/src/main/resources/application.properties b/call-all-getters/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java b/call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java new file mode 100644 index 0000000000..cca49b4359 --- /dev/null +++ b/call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java @@ -0,0 +1,26 @@ +package com.baeldung; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +import com.baeldung.reflection.model.Customer; +import com.baeldung.reflection.util.Utils; + +public class CallAllGettersApplicationTests { + + @Test + public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception { + Customer customer = new Customer(1, "Himanshu", null, null); + + List result = Utils.getNullPropertiesList(customer); + List expectedFieldNames = Arrays.asList("emailId","phoneNumber"); + + Assert.assertTrue(result.size() == expectedFieldNames.size()); + Assert.assertTrue(result.containsAll(expectedFieldNames)); + + } + +}