java USES reflection to get all the properties and methods of an entity and assign values to them

  • 2020-05-30 20:17:17
  • OfStack

1 common entity Person:


private int id;
private String name;
private Date createdTime;
...
// Other fields 
// get set methods 
...............

Now we need to replace the null values of all the fields in Person that are sent through webService with ""

Implementation idea:

1. Get all the fields of the entity and traverse them
2. Get the field type
3. Call the get method of the field to determine whether the field value is empty
4. If the field value is empty, call the set method of the field and assign the value to the field

code:


   Field[] field = model.getClass().getDeclaredFields(); //  Gets all the properties of the entity class and returns Field An array of 
    try {
      for (int j = 0; j < field.length; j++) { //  Traverse all attributes 
        String name = field[j].getName(); //  Gets the name of the property 
        name = name.substring(0, 1).toUpperCase() + name.substring(1); //  Capitalize the first character of a property for easy construction get . set methods 
        String type = field[j].getGenericType().toString(); //  Gets the type of the property 
        if (type.equals("class java.lang.String")) { //  if type Is a class type, then contains "class " , followed by the class name 
          Method m = model.getClass().getMethod("get" + name);
          String value = (String) m.invoke(model); //  call getter Method gets the property value 
          if (value == null) {
            m = model.getClass().getMethod("set"+name,String.class);
            m.invoke(model, "");
          }
        }
        if (type.equals("class java.lang.Integer")) {
          Method m = model.getClass().getMethod("get" + name);
          Integer value = (Integer) m.invoke(model);
          if (value == null) {
            m = model.getClass().getMethod("set"+name,Integer.class);
            m.invoke(model, 0);
          }
        }
        if (type.equals("class java.lang.Boolean")) {
          Method m = model.getClass().getMethod("get" + name);
          Boolean value = (Boolean) m.invoke(model);
          if (value == null) {
            m = model.getClass().getMethod("set"+name,Boolean.class);
            m.invoke(model, false);
          }
        }
        if (type.equals("class java.util.Date")) {
          Method m = model.getClass().getMethod("get" + name);
          Date value = (Date) m.invoke(model);
          if (value == null) {
            m = model.getClass().getMethod("set"+name,Date.class);
            m.invoke(model, new Date());
          }
        }
         //  If needed , You can continue to expand as above , Add other types of judgments 
      }
    } catch (NoSuchMethodException e) {
      e.printStackTrace();
    } catch (SecurityException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: