All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.table.utils.RequestMapUtils Maven / Gradle / Ivy

There is a newer version: 1.4.8
Show newest version
package com.litongjava.table.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

public class RequestMapUtils {

  public static void putEntityToMap(Map map, Object reqVo) {
    Arrays.stream(reqVo.getClass().getDeclaredMethods())
        // 1.转为stream对象 2.过滤到get方法
        .filter(method -> method.getName().startsWith("get") && method.getParameterCount() == 0)
        // 2.遍历get方法
        .forEach(method -> {
          String fieldName = Character.toLowerCase(method.getName().charAt(3)) + method.getName().substring(4);
          try {
            Optional.ofNullable(method.invoke(reqVo)).ifPresent(value -> map.put(fieldName, value));
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          } catch (InvocationTargetException e) {
            e.printStackTrace();
          }

        });
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy