From c8dff52e9ca92b9197c48e3e02e047ea50a35914 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Wed, 24 Aug 2022 19:10:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EAsgcCompiler=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=81.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/aop/compiler/AsgcCompilerTest.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompilerTest.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompilerTest.java index f522b272..ed5efb7c 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompilerTest.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompilerTest.java @@ -82,11 +82,32 @@ public class AsgcCompilerTest { method.invoke(instance); } + @Test + public void compileAndLoadClass4() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { + AsgcCompiler compiler = new AsgcCompiler(); + compiler.addClasspath("/Users/yangwen/my/tmp/java"); + compiler.addClasspath("/Users/yangwen/my/tmp/java/asgc-package-lab1-1.0-SNAPSHOT.jar"); + String code = "package a.b;\n" + + "import fun.asgc.lab.pkg.lab1.Dog1;\n" + + "import fun.asgc.Player;\n" + + "public class RadioPlayer implements Player {\n" + + "\tpublic void play() {\n" + +// "\t\tSystem.out.println(Dog.class);\n" + +// "\t\tSystem.out.println(new Dog(\"大黄\").eat(\"骨头\"));\n" + + "\t\tSystem.out.println(\"收音机播放\");\n" + + "\t}\n" + + "}\n"; + Class clazz = compiler.compileAndLoadClass("a.b","RadioPlayer", code); + Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("play")).findFirst().get(); + Object instance = clazz.newInstance(); + method.invoke(instance); + } + private Object eval(String expression) { String code = "package fun.asgc.test;\n" + "import static java.lang.Math.*;\n" + "public class Calc {\n" + - "\tpublic Object invoke(){\n" + + "\tpublic static Object invoke(){\n" + "\t\treturn " + expression + ";\n" + "\t}\n" + "}\n"; @@ -94,8 +115,8 @@ public class AsgcCompilerTest { try { Class clazz = compiler.compileAndLoadClass("fun.asgc.test", "Calc", code); Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("invoke")).findFirst().get(); - return method.invoke(clazz.newInstance()); - } catch (ClassNotFoundException|IllegalAccessException|InstantiationException|InvocationTargetException e) { + return method.invoke(null); + } catch (ClassNotFoundException|IllegalAccessException|InvocationTargetException e) { e.printStackTrace(); } return null;