From bc5e87c7ef6f108f4d5d94ce5b09820b7ef74b4d Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Mon, 5 Jun 2017 18:20:58 +0530 Subject: [PATCH 1/3] Adding new project : Spring check if a property is null Using Java Instrospector --- spring-check-if-a-property-is-null/.gitignore | 24 +++++++ spring-check-if-a-property-is-null/pom.xml | 50 +++++++++++++++ ...ringCheckIfAPropertyIsNullApplication.java | 21 +++++++ .../baeldung/reflection/model/Customer.java | 63 +++++++++++++++++++ .../com/baeldung/reflection/util/Utils.java | 34 ++++++++++ .../src/main/resources/application.properties | 0 ...heckIfAPropertyIsNullApplicationTests.java | 31 +++++++++ 7 files changed, 223 insertions(+) create mode 100644 spring-check-if-a-property-is-null/.gitignore create mode 100644 spring-check-if-a-property-is-null/pom.xml create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java create mode 100644 spring-check-if-a-property-is-null/src/main/resources/application.properties create mode 100644 spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java diff --git a/spring-check-if-a-property-is-null/.gitignore b/spring-check-if-a-property-is-null/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-check-if-a-property-is-null/.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/spring-check-if-a-property-is-null/pom.xml b/spring-check-if-a-property-is-null/pom.xml new file mode 100644 index 0000000000..d92e2a8e9c --- /dev/null +++ b/spring-check-if-a-property-is-null/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + baeldung + spring-check-null + 0.0.1-SNAPSHOT + jar + + spring-check-if-a-property-is-null + Calling 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/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java new file mode 100644 index 0000000000..24348a714e --- /dev/null +++ b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java @@ -0,0 +1,21 @@ +package com.baeldung; + +import java.util.List; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.baeldung.reflection.model.Customer; +import com.baeldung.reflection.util.Utils; + +@SpringBootApplication +public class SpringCheckIfAPropertyIsNullApplication { + + 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/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java new file mode 100644 index 0000000000..d0c6c31dce --- /dev/null +++ b/spring-check-if-a-property-is-null/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/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java new file mode 100644 index 0000000000..665717db09 --- /dev/null +++ b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java @@ -0,0 +1,34 @@ +package com.baeldung.reflection.util; + +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +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 = new ArrayList(); + + propDescList.stream().forEach(p -> { + Method getterMethod = p.getReadMethod(); + try { + if (getterMethod != null && getterMethod.invoke(customer) == null) { + // If the value if null for that field + nullProps.add(p.getName()); + } + } catch (Exception e) { + // Handle the exception + e.printStackTrace(); + } + }); + return nullProps; + } +} diff --git a/spring-check-if-a-property-is-null/src/main/resources/application.properties b/spring-check-if-a-property-is-null/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java b/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java new file mode 100644 index 0000000000..edd009e719 --- /dev/null +++ b/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java @@ -0,0 +1,31 @@ +package com.baeldung; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.reflection.model.Customer; +import com.baeldung.reflection.util.Utils; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringCheckIfAPropertyIsNullApplicationTests { + + @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)); + + } + +} From 7cf3afbd79a20485b70696871b2f711ed05e3b44 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Mon, 5 Jun 2017 21:53:31 +0530 Subject: [PATCH 2/3] Renaming the project Renaming the project --- .../.gitignore | 0 .../pom.xml | 6 +++--- .../main/java/com/baeldung/CallAllGettersApplication.java | 5 +---- .../main/java/com/baeldung/reflection/model/Customer.java | 0 .../src/main/java/com/baeldung/reflection/util/Utils.java | 0 .../src/main/resources/application.properties | 0 .../java/com/baeldung/CallAllGettersApplicationTests.java | 7 +------ 7 files changed, 5 insertions(+), 13 deletions(-) rename {spring-check-if-a-property-is-null => call-all-getters}/.gitignore (100%) rename {spring-check-if-a-property-is-null => call-all-getters}/pom.xml (89%) rename spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java => call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java (72%) rename {spring-check-if-a-property-is-null => call-all-getters}/src/main/java/com/baeldung/reflection/model/Customer.java (100%) rename {spring-check-if-a-property-is-null => call-all-getters}/src/main/java/com/baeldung/reflection/util/Utils.java (100%) rename {spring-check-if-a-property-is-null => call-all-getters}/src/main/resources/application.properties (100%) rename spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java => call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java (72%) diff --git a/spring-check-if-a-property-is-null/.gitignore b/call-all-getters/.gitignore similarity index 100% rename from spring-check-if-a-property-is-null/.gitignore rename to call-all-getters/.gitignore diff --git a/spring-check-if-a-property-is-null/pom.xml b/call-all-getters/pom.xml similarity index 89% rename from spring-check-if-a-property-is-null/pom.xml rename to call-all-getters/pom.xml index d92e2a8e9c..ed4735a861 100644 --- a/spring-check-if-a-property-is-null/pom.xml +++ b/call-all-getters/pom.xml @@ -4,12 +4,12 @@ 4.0.0 baeldung - spring-check-null + call-all-getters 0.0.1-SNAPSHOT jar - spring-check-if-a-property-is-null - Calling getters using Introspector + call-all-getters + Calling all getters using Introspector org.springframework.boot diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java b/call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java similarity index 72% rename from spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java rename to call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java index 24348a714e..5c10ed5b55 100644 --- a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java +++ b/call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java @@ -2,13 +2,10 @@ package com.baeldung; import java.util.List; -import org.springframework.boot.autoconfigure.SpringBootApplication; - import com.baeldung.reflection.model.Customer; import com.baeldung.reflection.util.Utils; -@SpringBootApplication -public class SpringCheckIfAPropertyIsNullApplication { +public class CallAllGettersApplication { public static void main(String[] args) throws Exception { diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java b/call-all-getters/src/main/java/com/baeldung/reflection/model/Customer.java similarity index 100% rename from spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java rename to call-all-getters/src/main/java/com/baeldung/reflection/model/Customer.java diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java b/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java similarity index 100% rename from spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java rename to call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java diff --git a/spring-check-if-a-property-is-null/src/main/resources/application.properties b/call-all-getters/src/main/resources/application.properties similarity index 100% rename from spring-check-if-a-property-is-null/src/main/resources/application.properties rename to call-all-getters/src/main/resources/application.properties diff --git a/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java b/call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java similarity index 72% rename from spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java rename to call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java index edd009e719..cca49b4359 100644 --- a/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java +++ b/call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java @@ -5,16 +5,11 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; import com.baeldung.reflection.model.Customer; import com.baeldung.reflection.util.Utils; -@RunWith(SpringRunner.class) -@SpringBootTest -public class SpringCheckIfAPropertyIsNullApplicationTests { +public class CallAllGettersApplicationTests { @Test public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception { From 5b9a1255814150972ac28ed1dc2d94516010a892 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Tue, 6 Jun 2017 15:20:48 +0530 Subject: [PATCH 3/3] Move null check logic to separate method Move null check logic to separate method --- .../com/baeldung/reflection/util/Utils.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) 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 index 665717db09..c9542bbdf2 100644 --- 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 @@ -3,9 +3,10 @@ package com.baeldung.reflection.util; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; import com.baeldung.reflection.model.Customer; @@ -15,20 +16,28 @@ public class Utils { PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors(); List propDescList = Arrays.asList(propDescArr); - List nullProps = new ArrayList(); - - propDescList.stream().forEach(p -> { - Method getterMethod = p.getReadMethod(); - try { - if (getterMethod != null && getterMethod.invoke(customer) == null) { - // If the value if null for that field - nullProps.add(p.getName()); - } - } catch (Exception e) { - // Handle the exception - e.printStackTrace(); - } - }); + 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; + } }