Merge pull request #1 from hmantri05/spring-check-property-is-null-2
Spring check property is null 2
This commit is contained in:
commit
94d9626188
|
@ -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/
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>baeldung</groupId>
|
||||
<artifactId>call-all-getters</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>call-all-getters</name>
|
||||
<description>Calling all getters using Introspector</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.3.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
|
@ -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<String> nullProps = Utils.getNullPropertiesList(customer);
|
||||
System.out.println(nullProps);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<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()
|
||||
.filter(nulls(customer))
|
||||
.map(PropertyDescriptor::getName)
|
||||
.collect(Collectors.toList());
|
||||
return nullProps;
|
||||
}
|
||||
|
||||
private static Predicate<PropertyDescriptor> nulls(Customer customer) {
|
||||
Predicate<PropertyDescriptor> isNull = new Predicate<PropertyDescriptor>() {
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -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<String> result = Utils.getNullPropertiesList(customer);
|
||||
List<String> expectedFieldNames = Arrays.asList("emailId","phoneNumber");
|
||||
|
||||
Assert.assertTrue(result.size() == expectedFieldNames.size());
|
||||
Assert.assertTrue(result.containsAll(expectedFieldNames));
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue