summaryrefslogtreecommitdiff
path: root/src/main/java/com/mesasoft/cn/util/BeanUtils.java
blob: 152c490c1366481028d6b0ecff4352c1a8b92ffe (plain)
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
package com.mesasoft.cn.util;

import com.alibaba.fastjson.JSONObject;
import com.zhazhapan.modules.constant.ValueConsts;
import com.zhazhapan.util.Checker;
import com.zhazhapan.util.Formatter;
import com.zhazhapan.util.enums.FieldModifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;

/**
 * @author pantao
 * @since 2018/1/18
 */
public class BeanUtils {

    private static final String ERROR_JSON = "{\"error\":\"internal error, please try again later\"}";

    private static Logger logger = LoggerFactory.getLogger(BeanUtils.class);

    private BeanUtils() {}

    /**
     * 将权限字符串装换成权限数组
     *
     * @param auth 权限字符串
     *
     * @return 权限数组
     */
    public static int[] getAuth(String auth) {
        int[] a = new int[5];
        if (Checker.isNotEmpty(auth)) {
            String[] u = auth.split(ValueConsts.COMMA_SIGN);
            int len = Math.min(a.length, u.length);
            for (int i = 0; i < len; i++) {
                a[i] = Formatter.stringToInt(u[i]);
            }
        }
        return a;
    }

    /**
     * 将Bean转换成JSON
     *
     * @param object Bean对象
     *
     * @return {@link String}
     */
    public static String toPrettyJson(Object object) {
        String result;
        try {
            result = com.zhazhapan.util.BeanUtils.toPrettyJson(object, FieldModifier.PRIVATE);
        } catch (IllegalAccessException e) {
            result = Formatter.formatJson(ERROR_JSON);
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 将类属性装换成JSON(只能转换有get方法的)
     *
     * @param object 转换的对象
     *
     * @return {@link JSONObject}
     */
    public static JSONObject beanToJson(Object object) {
        try {
            return com.zhazhapan.util.BeanUtils.beanToJson(object);
        } catch (IllegalAccessException | InvocationTargetException e) {
            logger.error(e.getMessage());
            return null;
        }
    }
}