hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.server.im;
 
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
 
import com.google.protobuf.GeneratedMessage;
 
public class WFCMessageParseTable {
 
    private static WFCMessageParseTable instance;
    private Map<Class<? extends GeneratedMessage>, Method> clazzMethodMap;
    
    public static synchronized WFCMessageParseTable getInstance() {
        if (instance == null) {
            instance = new WFCMessageParseTable();
        }
        
        return instance;
    }
    
    private WFCMessageParseTable() {
        clazzMethodMap = new HashMap<Class<? extends GeneratedMessage>, Method>();
    }
    
    public Method getMethod(Class<? extends GeneratedMessage> clazz) {
        if (clazz == null) {
            return null;
        }
        
        Method result = clazzMethodMap.get(clazz);
        
        if (result == null) {
            synchronized (this) {
                result = clazzMethodMap.get(clazz);
                
                if (result == null) {
                    result = doGetMethod(clazz);
                    clazzMethodMap.put(clazz, result);
                }
            }
        }
        
        return result;
    }
 
    private Method doGetMethod(Class<? extends GeneratedMessage> clazz) {
        try {
            Method method = clazz.getMethod("parseFrom", byte[].class);
            return method;
        } 
        catch (Exception e) {
        } 
        
        return null;
    }
    
}