leutu
2024-06-03 3ef35e6cd16bbfa206b26bb3271eac40ad020bcb
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
package com.fastbee.base.core.hanler;
 
import com.fastbee.common.core.protocol.Message;
import com.fastbee.base.session.Session;
 
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
 
import java.lang.reflect.Method;
import java.lang.reflect.Type;
 
/**
 * 基础处理类
 *
 * @author bill
 */
public abstract class BaseHandler {
 
    public static final int MESSAGE = 0;
    public static final int SESSION = 1;
 
    public final Object targetObject;
    public final Method targetMethod;
    public final int[] parameterTypes;
    public final boolean returnVoid;
    public final boolean async;
    public final String desc;
 
    public BaseHandler(Object target, Method targetMethod, String desc) {
        this(target, targetMethod, desc, false);
    }
 
    public BaseHandler(Object targetObject, Method targetMethod, String desc, boolean async) {
        this.targetObject = targetObject;
        this.targetMethod = targetMethod;
        this.returnVoid = targetMethod.getReturnType().isAssignableFrom(Void.TYPE);
        this.async = async;
        if (desc == null || desc.isEmpty())
            desc = targetMethod.getName();
        this.desc = desc;
 
        Type[] types = targetMethod.getGenericParameterTypes();
        int[] parameterTypes = new int[types.length];
        try {
            for (int i = 0; i < types.length; i++) {
                Type type = types[i];
                Class<?> clazz;
                if (type instanceof ParameterizedTypeImpl) {
                    clazz = (Class<?>) ((ParameterizedTypeImpl) type).getActualTypeArguments()[0];
                } else {
                    clazz = (Class<?>) type;
                }
 
                if (Message.class.isAssignableFrom(clazz)) {
                    parameterTypes[i] = MESSAGE;
                } else if (Session.class.isAssignableFrom(clazz)) {
                    parameterTypes[i] = SESSION;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        this.parameterTypes = parameterTypes;
    }
 
    public <T extends Message> T invoke(T request, Session session) throws Exception {
        Object[] args = new Object[parameterTypes.length];
 
        for (int i = 0; i < parameterTypes.length; i++) {
            int type = parameterTypes[i];
            switch (type) {
                case BaseHandler.MESSAGE:
                    args[i] = request;
                    break;
                case BaseHandler.SESSION:
                    args[i] = session;
                    break;
            }
        }
        return (T) targetMethod.invoke(targetObject, args);
    }
 
    @Override
    public String toString() {
        return desc;
    }
}