Give default values to your objects properties

I'm a zebra

  • What to do and what to solve in this blog
  • What knowledge is needed to solve this problem
  • Specific code, effect
  1. In actual development, in order to avoid unnecessary null pointer exception. It is necessary to judge whether it is empty. If a method is called to judge whether it is null every time an object's property is fetched, the overall code will be more cumbersome.
  2. Therefore, it is necessary to write a tool class to complete judgment before creating and using the object, and give default values to the required attributes; in this tool class, use the reflection knowledge to obtain the attribute object, judge the type and value; in addition to reflection, there are also methods under the Commons beanautils package


Guide bag

    <!--Use@Data Annotation complete entity class set,get-->

An entity class

//I only have one zebra
import lombok.Data;

import java.util.List;

public class Banma {
    private String name;
    private char sex;
    private int age;
    private Double money;
    private String[] hobby;
    private List<String> strList;

One way

package com.banma.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class VoUtils {
     * I'm a zebra
     * Initialize the value of vo attribute, and assign a default value when the attribute is empty, such as 0 for Double type
     * @param obj
    public static void initVoNullProperties(Object obj) {
        if (obj == null) {
        try {
            if(obj instanceof List){
                List list = (List) obj;
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                Object p = PropertyUtils.getProperty(obj, field.getName());
                if (p == null) {
                    if(field.getType() == Double.class) {
                        BeanUtils.setProperty(obj, field.getName(), Double.valueOf(0));
                    } else if(field.getType() == String.class) {
                        BeanUtils.setProperty(obj, field.getName(), "");
                } else if(p instanceof List) {
                    List list = (List) PropertyUtils.getProperty(obj, field.getName());
            List<Class> classes = new ArrayList<Class>(Arrays.asList(Double.class,String.class,Integer.class,Long.class,long.class,int.class));
            Method[] methods = obj.getClass().getDeclaredMethods();
            for(Method method : methods){
                if(method.getName().startsWith("get") && !classes.contains(method.getReturnType())){
                    Object tmp = null;
                    tmp = method.invoke(obj);
                    if(tmp == null){
                        tmp = method.getReturnType().getConstructors().length>0 ? method.getReturnType().getConstructors()[0].newInstance():null;
                        if(tmp == null){
                        tmp = method.getReturnType().cast(tmp);

                    Method setter = obj.getClass().getDeclaredMethod("set"+method.getName().substring(3), method.getReturnType());
                    setter.invoke(obj, tmp);

        } catch (Exception e) {


     * Initializes the value of the list property. If the property is empty, assign a default value, such as 0 for Double type
     * @param list
    public static void initListNullProperties(List list) {
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            Object object = (Object);

Tests and test results

//I'm a zebra
import com.banma.entity.Banma;
import com.banma.utils.VoUtils;

public class Demo {
    public static void main(String[] args) {
        Banma banma = new Banma();

Keywords: Java Attribute Lombok Apache

Added by PHPThorsten on Mon, 02 Dec 2019 18:23:03 +0200