fix: 修复了规则引擎修改规则代码后,再次初始化,测试调用没有更新的问题
parent
b92a8cef22
commit
56074aea9c
|
@ -11,44 +11,8 @@ import java.net.URLClassLoader;
|
|||
* @Author Xin.Yao
|
||||
* @Date 2024/5/1 19:37
|
||||
*/
|
||||
public class CustomClassLoader extends URLClassLoader {
|
||||
public CustomClassLoader(ClassLoader parent, String classPath) throws MalformedURLException {
|
||||
super(new URL[]{new URL("file:" + classPath)},parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
try {
|
||||
byte[] classBytes = loadClassData(name);
|
||||
return defineClass(name, classBytes, 0, classBytes.length);
|
||||
} catch (IOException e) {
|
||||
throw new ClassNotFoundException(name, e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] loadClassData(String className) throws IOException {
|
||||
String fileName = className.replace('.', File.separatorChar) + ".class";
|
||||
File classFile = new File(getPathForResource(fileName));
|
||||
if (!classFile.exists()) {
|
||||
throw new FileNotFoundException("Class file not found: " + classFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
try (InputStream in = new FileInputStream(classFile)) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[4096];
|
||||
int n;
|
||||
while ((n = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, n);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
}
|
||||
|
||||
private String getPathForResource(String resource) {
|
||||
URL url = super.getResource(resource);
|
||||
if (url == null) {
|
||||
throw new IllegalArgumentException("Resource not found: " + resource);
|
||||
}
|
||||
return url.getPath();
|
||||
public class CustomClassLoader extends ClassLoader {
|
||||
public Class<?> defineClassFromBytes(String name, byte[] data) {
|
||||
return defineClass(name, data, 0, data.length);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package com.muyu.rule.engine.service.impl;
|
|||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -167,13 +169,13 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
|||
|
||||
@Override
|
||||
public Result testMethod(String code) {
|
||||
String className = "Rule"+Character.toUpperCase(code.charAt(0)) + code.substring(1)+"Class";
|
||||
String classPath = "D:\\work\\ruoyi-cloud-server\\muyu-modules\\muyu-rule-engine\\target\\classes\\";
|
||||
String className = "com.muyu.rule.engine.domain.Rule"+Character.toUpperCase(code.charAt(0)) + code.substring(1)+"Class";
|
||||
String classPath = "D:\\work\\ruoyi-cloud-server\\muyu-modules\\muyu-rule-engine\\target\\classes\\"+className.replace(".", "\\")+".class";
|
||||
try {
|
||||
// 使用CustomClassLoader加载class到内存
|
||||
CustomClassLoader customClassLoader = new CustomClassLoader(ClassLoader.getSystemClassLoader(), classPath);
|
||||
// 加载类
|
||||
Class<?> aClass = customClassLoader.loadClass("com.muyu.rule.engine.domain."+className);
|
||||
// 读取类文件
|
||||
byte[] classData = Files.readAllBytes(Paths.get(classPath));
|
||||
CustomClassLoader customClassLoader = new CustomClassLoader();
|
||||
Class<?> aClass = customClassLoader.defineClassFromBytes(className,classData);
|
||||
// 实例化类
|
||||
Object obj = aClass.newInstance();
|
||||
//获取类中所有方法
|
||||
|
@ -189,4 +191,26 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
|||
|
||||
return Result.success("测试成功");
|
||||
}
|
||||
|
||||
public String getTypeCodeText(EngineMaintenance engineMaintenance) {
|
||||
String className =Character.toUpperCase(engineMaintenance.getCode().charAt(0)) + engineMaintenance.getCode().substring(1);
|
||||
switch (engineMaintenance.getType()) {
|
||||
case "data-set":
|
||||
className = className+"DataSetContext";
|
||||
return "package com.muyu.rule.engine.domain;\n" +
|
||||
"\n" +
|
||||
"public class "+className+" {\n" +
|
||||
"\n" +
|
||||
" private final RecordContext recordContext;\n" +
|
||||
"\n" +
|
||||
" public DataSetContext (RecordContext recordContext) {\n" +
|
||||
" this.recordContext = recordContext;\n" +
|
||||
" }\n" +
|
||||
"}\n";
|
||||
case "data-record":
|
||||
return "记录";
|
||||
default:
|
||||
return "数据字段";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue