String classPathName = peakClippingDto.getClassPathName();
String methodName = peakClippingDto.getMethodName();
String argsType = peakClippingDto.getArgsType();
//反射获取类
Class aClass = Class.forName(classPathName);
//入参类型列表
List argTypes = JSONArray.parseArray(argsType, Class.class);
//获取方法
Method call = aClass.getDeclaredMethod(methodName, argTypes.toArray(new Class[]{}));
List paramList = JSONArray.parseArray(peakClippingDto.getParams(), String.class);
//反射调用入参
Object[] args =new Object[paramList.size()];
ObjectMapper objectMapper =new ObjectMapper();
for (int i =0; i < paramList.size(); i++) {
String param = paramList.get(i);
Class parameterType = argTypes.get(i);
//json对象不能用objectMapper转换
boolean valid = JSON.isValid(param);
if (valid){
args[i] = JSON.parseObject(param, parameterType);
}else {
args[i] = objectMapper.convertValue(param, parameterType);
}
}
//私有方法调用
call.setAccessible(true);
Object invoke = call.invoke(aClass.newInstance(), args);
AnnotatedType annotatedReturnType = call.getAnnotatedReturnType();
Type type = annotatedReturnType.getType();
String typeName = type.getTypeName();
if (!"void".equals(typeName)){
PeakClippingResultDto callResult = JSON.parseObject(invoke.toString(), PeakClippingResultDto.class);
System.out.println("-----------"+callResult.toString());
}
public static void main(String[] args)throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
Class ceshi = Class.forName("com.wanmi.sbc.saas.Ceshi");
String hehe1 = JSON.toJSONString(CeshiVo.builder().ha("hehe").build());
CeshiVo hehe = JSONObject.parseObject(hehe1, CeshiVo.class);
Method ceshi1 = ceshi.getDeclaredMethod("ceshi",new Class[]{CeshiVo.class});
AnnotatedType annotatedReturnType = ceshi1.getAnnotatedReturnType();
Object invoke = ceshi1.invoke(ceshi.newInstance(), hehe);
System.out.println("-----------"+invoke.toString());
}
A a =new A();
a.setA("a");
a.setB("b");
a.setC("c");
Object[] arr =new Object[]{a,"ll"};
Object[] ls = ArrayUtils.toArray(a, "l");
String s1 = JSON.toJSONString(ls);
Class aClass = Class.forName("com.wanmi.sbc.util.A");
Package aPackage = aClass.getPackage();
String name = aClass.getName();
String argsType=JSONArray.toJSONString(ArrayUtils.toArray(A.class.getName(),"java.lang.String", "java.lang.Integer"));
String param=JSONArray.toJSONString(ArrayUtils.toArray(JSON.toJSONString(a),"hehe", "12"));
List argList = JSONArray.parseArray(argsType, Class.class);
Class[] parameterTypes = argList.toArray(new Class[]{});
Method call = aClass.getDeclaredMethod("call", parameterTypes);
List objects = JSONArray.parseArray(param, String.class);
Object[] args1 =new Object[objects.size()];
ObjectMapper objectMapper =new ObjectMapper();
for (int i =0; i < objects.size(); i++) {
String s = objects.get(i);
Class parameterType = parameterTypes[i];
boolean valid = JSON.isValid(s);
Object o;
if (valid){
o = JSON.parseObject(s, parameterType);
}else {
o = objectMapper.convertValue(s, parameterType);
}
args1[i] = o;
}
Object invoke = call.invoke(aClass.newInstance(), args1);
AnnotatedType annotatedReturnType = call.getAnnotatedReturnType();
Type type = annotatedReturnType.getType();
String typeName = type.getTypeName();
if (!"void".equals(typeName)){
PeakClippingResultDto callResult = JSON.parseObject(invoke.toString(), PeakClippingResultDto.class);
System.out.println("-----------"+callResult.toString());
}
// c(new A(), o -> {
// System.out.println(o.toString()+"---------");
// return null;
// });
// Method call = A.class.getMethod("call", String.class);
// d(A.class, call);
// Class aClass = Class.forName("com.wanmi.sbc.util.A");
// Class string = Class.forName("java.lang.String");
// Class[] parameterTypes = {string, Integer.class};
// String s = JSONArray.toJSONString(parameterTypes);
// List classes = JSONArray.parseArray(s, Class.class);
// Method call = aClass.getDeclaredMethod("call", classes.toArray(new Class[]{}));
// Object invoke = call.invoke(aClass.newInstance(), "hehe",1);
// AnnotatedType annotatedReturnType = call.getAnnotatedReturnType();
// Type type = annotatedReturnType.getType();
// String typeName = type.getTypeName();
// if (!"void".equals(typeName)){
// System.out.println("-----------"+invoke.toString());
// }