From 7f80810f5349349b473ec39d56f50875cf8cb9d8 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Mon, 5 Jun 2017 11:53:02 +0530 Subject: [PATCH] Adding new Project : Spring check if a property is null Using Java Introspector to check if a field is null in an object --- .gitignore | 4 ++ 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 +++++++++ 8 files changed, 227 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/.gitignore b/.gitignore index fb88a82371..1890e8bd0e 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,7 @@ spring-all/*.log *.jar SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties + +spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties + +spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties 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)); + + } + +}