package com.shhaoxin.cmms.admin.util
import com.shhaoxin.cmms.admin.api.entity.BatchInfo
import org.springframework.util.ObjectUtils
import org.springframework.util.StringUtils
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.LinkedList
import java.util.List
/**
* @author YAO_WENLEI
* @description:
* @since 2021-12-07 13:48:45
*/
public class JobInvokeUtils {
private static final int INDEX_NOT_FOUND = -1
public static void invokerMethod(BatchInfo batchInfo) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//获取执行字符串
String invokeTarget = batchInfo.getProcName()
//获取执行目标类
String beanName = getBeanName(invokeTarget)
//获取执行目标方法
String methodName = getMethodName(invokeTarget)
//获取执行方法参数
List<Object[]> methodParams = getMethodParams(invokeTarget)
//判断是否是执行类的方法
if (isValidClassName(invokeTarget)) {
//执行类方法
Object bean = Class.forName(beanName).newInstance()
invokerMethod(bean, methodName, methodParams)
} else {
//todo 执行bean方法
}
}
public static void invokerMethod(Object bean, String methodName, List<Object[]> methodParams) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//判断是否有参数
if (!ObjectUtils.isEmpty(methodParams)) {
Method method = bean.getClass().getDeclaredMethod(methodName, getMethodParamsType(methodParams))
method.invoke(bean, getMethodParamValue(methodParams))
} else {
Method method = bean.getClass().getDeclaredMethod(methodName)
method.invoke(bean)
}
}
//获取方法的执行参数
public static Object[] getMethodParamValue(List<Object[]> methodParams) {
Object[] objects = new Object[methodParams.size()]
int index = 0
for (Object[] methodParam : methodParams) {
objects[index] = methodParam[0]
index++
}
return objects
}
//获取方法执行参数类型
public static Class<?>[] getMethodParamsType(List<Object[]> methodParams) {
Class<?>[] classs = new Class<?>[methodParams.size()]
int index = 0
for (Object[] methodParam : methodParams) {
classs[index] = (Class<?>) methodParam[1]
index++
}
return classs
}
//判断是否执行某个类的方法
public static boolean isValidClassName(String invokeTarget) {
char[] chars = invokeTarget.toCharArray()
int count = 0
for (char aChar : chars) {
if (aChar == '.') {
count++
}
}
return count > 1
}
/*
* @description: 获取方法的参数列表
* @param invokeTarget
* @return: java.util.List<java.lang.Object[]>
*/
public static List<Object[]> getMethodParams(String invokeTarget) {
String methodStr = substringBetween(invokeTarget, "(", ")")
if (StringUtils.isEmpty(methodStr)) {
return null
}
String[] methodParams = methodStr.split(",")
List<Object[]> classs = new LinkedList<>()
for (int i = 0
String str = methodParams[i] == null ? "" : methodParams[i].trim()
// String字符串类型,包含'
if (str.contains("'")) {
classs.add(new Object[]{StringUtils.replace(str, "'", ""), String.class})
}
// boolean布尔类型,等于true或者false
else if ("true".equals(str) || "false".equals(str)) {
classs.add(new Object[]{Boolean.valueOf(str), Boolean.class})
}
// long长整形,包含L
else if (str.contains("l") || str.contains("L")) {
if (str.contains("l")) {
classs.add(new Object[]{Long.valueOf(StringUtils.replace(str, "l", "")), Long.class})
} else {
classs.add(new Object[]{Long.valueOf(StringUtils.replace(str, "L", "")), Long.class})
}
}
// double浮点类型,包含D
else if (str.contains("d") || str.contains("D")) {
if (str.contains("d")) {
classs.add(new Object[]{Double.valueOf(StringUtils.replace(str, "d", "")), Double.class})
} else {
classs.add(new Object[]{Double.valueOf(StringUtils.replace(str, "D", "")), Double.class})
}
}
// 其他类型归类为整形
else {
classs.add(new Object[]{Integer.valueOf(str), Integer.class})
}
}
return classs
}
//获取方法名称
public static String getMethodName(String invokeTarget) {
String methodName = subStringBefore(invokeTarget, "(")
return subStringAfterLast(methodName, ".")
}
//获取类名
public static String getBeanName(String invokeTarget) {
String beanName = subStringBefore(invokeTarget, "(")
return subStringBeforeLast(beanName, ".")
}
//获取指定符号之间的字符串
public static String substringBetween(String str, String open, String close) {
if (StringUtils.isEmpty(str) || StringUtils.isEmpty(open) || StringUtils.isEmpty(close)) {
return null
}
final int start = str.indexOf(open)
if (start != INDEX_NOT_FOUND) {
final int end = str.indexOf(close, start + open.length())
if (end != INDEX_NOT_FOUND) {
return str.substring(start + open.length(), end)
}
}
return null
}
//获取指定最后一个字符之后的字符串
public static String subStringAfterLast(String str, String separator) {
if (StringUtils.isEmpty(str) || StringUtils.isEmpty(separator)) {
return ""
}
int index = str.lastIndexOf(separator)
if (INDEX_NOT_FOUND == index || index == str.length() - separator.length()) {
return ""
}
return str.substring(index + separator.length())
}
//获取指定最后一个字符之前的字符串
public static String subStringBeforeLast(String str, String separator) {
if (StringUtils.isEmpty(str) || StringUtils.isEmpty(separator)) {
return ""
}
int index = str.lastIndexOf(separator)
if (INDEX_NOT_FOUND == index) {
return ""
}
return str.substring(0, index)
}
//获取指定字符之前的字符串
public static String subStringBefore(String str, String separator) {
if (StringUtils.isEmpty(str) || StringUtils.isEmpty(separator)) {
return ""
}
int index = str.indexOf(separator)
if (INDEX_NOT_FOUND == index) {
return ""
}
return str.substring(0, index)
}
}