yangys
2024-05-07 eaf6878850c029ac359d60409c7c9fcfa09c1852
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.qianwen.core.coderule.util;
 
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import com.qianwen.core.coderule.annotation.CodeRule;
import com.qianwen.core.tool.jackson.JsonUtil;
 
/* loaded from: blade-starter-coderule-9.3.0.0-SNAPSHOT.jar:org/springblade/core/coderule/util/CodeRuleHelper.class */
public class CodeRuleHelper {
    public static Map<String, Object> toUnderlineJSONString(Object object) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return JsonUtil.toMap(mapper.writeValueAsString(object));
    }
 
    public static <T> T toSnakeObject(String json, Class<T> clazz) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        T reqJson = (T) mapper.readValue(json, clazz);
        return reqJson;
    }
 
    public static Map<String, Object> getDeclaredFieldsInfoByFieldName(Object instance) {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = instance.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            try {
                Field field2 = clazz.getDeclaredField(field.getName());
                field2.setAccessible(true);
                map.put(field2.getName(), field2.get(instance));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e2) {
                e2.printStackTrace();
            }
        }
        return map;
    }
 
    /* JADX WARN: Multi-variable type inference failed */
    public static Map<String, Object> getDeclaredFieldsInfoByAnnotation(Object instance) {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = instance.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            map = toUnderlineJSONString(instance);
            for (Field field : fields) {
                Field field2 = clazz.getDeclaredField(field.getName());
                field2.setAccessible(true);
                boolean annotationPresent = field2.isAnnotationPresent(CodeRule.class);
                if (annotationPresent) {
                    String name = ((CodeRule) field2.getAnnotation(CodeRule.class)).code();
                    map.put(name, field2.get(instance));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e2) {
            e2.printStackTrace();
        } catch (JsonProcessingException e3) {
            e3.printStackTrace();
        }
        return map;
    }
 
    public static String getRealData(BigDecimal num) {
        if (num == null) {
            return "";
        }
        String value = num.stripTrailingZeros().toPlainString();
        return value;
    }
}