package com.muyu.test; import com.muyu.BasicEngine; import com.muyu.core.domain.DataValue; import com.muyu.domain.EngineVersion; import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class RuleTest { static Map> engineMap = new ConcurrentHashMap<>(); //外部类文件路径 public static final String engineWorkSourcePath = "/home/lib/"; public static final String className = "com.muyu.abstracts.generate."; public static void test(EngineVersion engineVersion) { try { //外部类文件路径 String externalClassFilePath = engineWorkSourcePath + "/com/muyu/abstracts/" + engineVersion.getName() + ".class"; System.out.println("6786786786785768567845687456858757885:" + externalClassFilePath); Path classFilePath = Paths.get(externalClassFilePath); //获取外部类所在的目录 String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf('/')); URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()}; ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls); //加载类 全限定名 Class clazz = externalClassLoader.loadClassFromPath(classFilePath, className + engineVersion.getName()); Object instance = clazz.getDeclaredConstructor().newInstance(); engineMap.put(engineVersion.getName(), (BasicEngine) instance); } catch (IOException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } DataValue dataValue = DataValue.builder() .type("String") .label("手机号") .key("phone") .value("张三") .build(); BasicEngine engine = engineMap.get(engineVersion.getName()); engine.set(dataValue); engine.execution(); } }