86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.dsh.utils;
 
import com.alibaba.fastjson.util.ParameterizedTypeImpl;
import org.apache.commons.lang3.ArrayUtils;
import sun.misc.Unsafe;
 
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
 
/**
 * * @author: lihong .
 * * @email: 765907456@qq.com .
 * * @createTime: 2019/10/12 下午5:24 .
 * * @description: .
 **/
public class ObjectUtils {
    private static Unsafe UNSAFE;
 
    static {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            UNSAFE = (Unsafe) field.get(null);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 实例化对象.不调用构造函数
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T newInstance(Class clazz) {
        try {
            return (T) UNSAFE.allocateInstance(clazz);
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * 构建泛型类型.
     *
     * @param types
     * @return
     */
    public static Type buildType(Type... types) {
        ParameterizedTypeImpl beforeType = null;
        if (types.length == 1) {
            return types[0];
        }
        if (types != null && types.length > 0) {
            for (int i = types.length - 1; i > 0; --i) {
                beforeType = new ParameterizedTypeImpl(new Type[]{beforeType == null ? types[i] : beforeType}, null, types[i - 1]);
            }
        }
        return beforeType;
    }
 
    /**
     * 获取类真实泛型类型.
     *
     * @param clazz        类型
     * @param genericIndex 泛型位置
     * @return
     */
    public static Class getGenericClassRealType(Class clazz, int genericIndex) {
        Type genericSuperclass = clazz.getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericSuperclass();
            return getGenericRealType(parameterizedType, genericIndex);
        }
        return null;
    }
 
    private static Class getGenericRealType(ParameterizedType parameterizedType, int genericIndex) {
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        if (actualTypeArguments.length <= genericIndex) {
            return null;
        }
        return (Class) actualTypeArguments[genericIndex];
    }
 
    public static Class getGenericInterfaceRealType(Class clazz, Type interfaceType, int genericIndex) {
        Type[] genericInterfaces = clazz.getGenericInterfaces();
        if (ArrayUtils.isEmpty(genericInterfaces)) {
            return null;
        }
        for (Type genericInterface : genericInterfaces) {
            if (genericInterface instanceof ParameterizedType && ((ParameterizedType) genericInterface).getRawType().equals(interfaceType)) {
                return getGenericRealType((ParameterizedType) genericInterface, genericIndex);
            }
        }
        return null;
    }
 
    public static String toBinaryString(Integer str) {
        String s = String.valueOf(Integer.toBinaryString(str));
        return s.substring(s.length() - 2, s.length() - 1);
    }
}