feat:规则引擎优化(可以测试多个方法)

master_fei
Yunfei Du 2024-05-05 16:55:00 +08:00
parent e18f69da9b
commit 912cdbdd49
2 changed files with 53 additions and 47 deletions

View File

@ -10,48 +10,50 @@ import java.net.URLClassLoader;
* @author YunFei.Du * @author YunFei.Du
* @date 13:50 2024/5/5 * @date 13:50 2024/5/5
*/ */
public class CustomClassLoader extends URLClassLoader { public class CustomClassLoader extends ClassLoader {
public Class<?> defineClassFromBytes(String name, byte[] data) {
return defineClass ( name,data,0,data.length );
public CustomClassLoader(ClassLoader parent, String classPath) throws MalformedURLException {
// ClassLoader:: 负责将 Class 的字节码形式转换成内存形式的 Class 对象
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 { // public CustomClassLoader(ClassLoader parent, String classPath) throws MalformedURLException {
String fileName = className.replace('.', File.separatorChar) + ".class"; // // ClassLoader:: 负责将 Class 的字节码形式转换成内存形式的 Class 对象
File classFile = new File(getPathForResource(fileName)); // super(new URL[]{new URL("file:" + classPath)},parent);
if (!classFile.exists()) { // }
throw new FileNotFoundException("Class file not found: " + classFile.getAbsolutePath()); //
} // @Override
// protected Class<?> findClass(String name) throws ClassNotFoundException {
try (InputStream in = new FileInputStream(classFile)) { // try {
ByteArrayOutputStream out = new ByteArrayOutputStream(); // byte[] classBytes = loadClassData(name);
byte[] buffer = new byte[4096]; // return defineClass(name, classBytes, 0, classBytes.length);
int n; // } catch (IOException e) {
while ((n = in.read(buffer)) != -1) { // throw new ClassNotFoundException(name, e);
out.write(buffer, 0, n); // }
} // }
return out.toByteArray(); //
} // private byte[] loadClassData(String className) throws IOException {
} // String fileName = className.replace('.', File.separatorChar) + ".class";
// File classFile = new File(getPathForResource(fileName));
private String getPathForResource(String resource) { // if (!classFile.exists()) {
URL url = super.getResource(resource); // throw new FileNotFoundException("Class file not found: " + classFile.getAbsolutePath());
if (url == null) { // }
throw new IllegalArgumentException("Resource not found: " + resource); //
} // try (InputStream in = new FileInputStream(classFile)) {
return url.getPath(); // 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();
// }
} }

View File

@ -17,6 +17,8 @@ import javax.tools.ToolProvider;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -122,9 +124,10 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
String classPath = "D:\\projects\\shixun\\fei-cloud-service\\etl-modules\\etl-rule-engine\\target\\classes\\"; String classPath = "D:\\projects\\shixun\\fei-cloud-service\\etl-modules\\etl-rule-engine\\target\\classes\\";
String filename = javaPath+className+".java"; String filename = javaPath+className+".java";
File file = new File(filename); File file = new File(filename);
// 确保源文件所在的目录存在 // 获取文件的父目录
File fileParent = file.getParentFile(); File fileParent = file.getParentFile();
// 检查父目录是否存在,如果不存在则创建
if (!fileParent.exists()) { if (!fileParent.exists()) {
fileParent.mkdir(); fileParent.mkdir();
} }
@ -166,14 +169,16 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
@Override @Override
public Result testMethod(String code) { public Result testMethod(String code) {
// 构建类名 // 构建类名
String className = "Rule"+Character.toUpperCase(code.charAt(0)) + code.substring(1)+"Class"; String className = "com.etl.rule.engine.domain.Rule"+Character.toUpperCase(code.charAt(0)) + code.substring(1)+"Class";
// 设置类路径 // 设置类路径
String classPath = "D:\\projects\\shixun\\fei-cloud-service\\etl-modules\\etl-rule-engine\\target\\classes\\"; String classPath = "D:\\projects\\shixun\\fei-cloud-service\\etl-modules\\etl-rule-engine\\target\\classes\\"+className.replace (".", "\\")+".class";
try { try {
// 读取类文件
byte[] classData = Files.readAllBytes ( Paths.get ( classPath ) );
// 使用 自定义类 CustomClassLoader加载器 加载class类到内存 // 使用 自定义类 CustomClassLoader加载器 加载class类到内存
CustomClassLoader customClassLoader = new CustomClassLoader(ClassLoader.getSystemClassLoader(), classPath); CustomClassLoader customClassLoader = new CustomClassLoader();
Class< ? > aClass = customClassLoader.defineClassFromBytes ( className, classData );
// 加载指定的类 // 加载指定的类
Class<?> aClass = customClassLoader.loadClass("com.etl.rule.engine.domain."+className);
// 实例化加载的类 // 实例化加载的类
Object obj = aClass.newInstance(); Object obj = aClass.newInstance();
//获取类中所有方法(遍历并执行类的所有方法) //获取类中所有方法(遍历并执行类的所有方法)
@ -188,7 +193,6 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
log.error(e.getMessage()); log.error(e.getMessage());
Result.error("测试失败"); Result.error("测试失败");
} }
return Result.success("测试成功"); return Result.success("测试成功");
} }
} }