WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
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
package tech.powerjob.server.remote.server.redirector;
 
import com.alibaba.fastjson.JSONObject;
import tech.powerjob.server.common.utils.SpringUtils;
import org.springframework.util.ReflectionUtils;
 
import java.lang.reflect.Method;
 
/**
 * process remote request
 *
 * @author tjq
 * @since 2021/2/19
 */
public class RemoteRequestProcessor {
 
    public static Object processRemoteRequest(RemoteProcessReq req) throws ClassNotFoundException {
        Object[] args = req.getArgs();
        String[] parameterTypes = req.getParameterTypes();
        Class<?>[] parameters = new Class[parameterTypes.length];
 
        for (int i = 0; i < parameterTypes.length; i++) {
            parameters[i] = Class.forName(parameterTypes[i]);
            Object arg = args[i];
            if (arg != null) {
                args[i] = JSONObject.parseObject(JSONObject.toJSONBytes(arg), parameters[i]);
            }
        }
 
        Class<?> clz = Class.forName(req.getClassName());
 
        Object bean = SpringUtils.getBean(clz);
        Method method = ReflectionUtils.findMethod(clz, req.getMethodName(), parameters);
 
        assert method != null;
        return ReflectionUtils.invokeMethod(method, bean, args);
    }
}