feat:规则引擎优化(可以测试多个方法)
parent
e18f69da9b
commit
912cdbdd49
|
@ -10,48 +10,50 @@ import java.net.URLClassLoader;
|
|||
* @author YunFei.Du
|
||||
* @date 13:50 2024/5/5
|
||||
*/
|
||||
public class CustomClassLoader extends URLClassLoader {
|
||||
|
||||
|
||||
|
||||
public CustomClassLoader(ClassLoader parent, String classPath) throws MalformedURLException {
|
||||
// ClassLoader:: 负责将 Class 的字节码形式转换成内存形式的 Class 对象
|
||||
super(new URL[]{new URL("file:" + classPath)},parent);
|
||||
public class CustomClassLoader extends ClassLoader {
|
||||
public Class<?> defineClassFromBytes(String name, byte[] data) {
|
||||
return defineClass ( name,data,0,data.length );
|
||||
}
|
||||
|
||||
@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 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 {
|
||||
// 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();
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ import javax.tools.ToolProvider;
|
|||
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;
|
||||
|
||||
|
@ -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 filename = javaPath+className+".java";
|
||||
File file = new File(filename);
|
||||
// 确保源文件所在的目录存在
|
||||
// 获取文件的父目录
|
||||
File fileParent = file.getParentFile();
|
||||
|
||||
// 检查父目录是否存在,如果不存在则创建
|
||||
if (!fileParent.exists()) {
|
||||
fileParent.mkdir();
|
||||
}
|
||||
|
@ -166,14 +169,16 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
|||
@Override
|
||||
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 {
|
||||
// 读取类文件
|
||||
byte[] classData = Files.readAllBytes ( Paths.get ( classPath ) );
|
||||
// 使用 自定义类 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();
|
||||
//获取类中所有方法(遍历并执行类的所有方法)
|
||||
|
@ -188,7 +193,6 @@ public class EngineMaintenanceServiceImpl implements IEngineMaintenanceService
|
|||
log.error(e.getMessage());
|
||||
Result.error("测试失败");
|
||||
}
|
||||
|
||||
return Result.success("测试成功");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue