判断数据类型为基本数据类型或者是其包装类


 private final Pattern compile = Pattern.compile("^(is|get)(.)(.*)$");

 private Map<String, String> getNormalKeyValue(Object obj) {
        Map<String, String> map = new HashMap<>(16);
        Method[] declaredMethods = obj.getClass().getDeclaredMethods();
        for (Method method : declaredMethods) {
            Class<?> returnType = method.getReturnType();
            try {
                if (method.getModifiers() == Modifier.PUBLIC && method.getParameterCount() == 0
                        && (method.getName().startsWith("get") || method.getName().startsWith("is")) && (String.class.equals(returnType)
                        || returnType.isPrimitive() || ((Class) returnType.getField("TYPE").get(null)).isPrimitive())) {
                    Object fieldV = method.invoke(obj);
                    if (Objects.isNull(fieldV)) {
                        continue;
                    }
                    Matcher matcher = compile.matcher(method.getName());
                    if (matcher.find()) {
                        map.put("_requestManager." + matcher.group(2).toLowerCase() + matcher.group(3), fieldV.toString());
                    }
                }
            } catch (Exception e) {
                writeLog(e);
            }
        }
        return map;
    }


posted @ 2023-02-12 20:26  菜阿  阅读(9)  评论(0编辑  收藏  举报