From 790ded499cf830728da4a48381d780bc1aca95f6 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Wed, 1 Mar 2023 12:05:14 +0800 Subject: [PATCH] =?UTF-8?q?AsgcCompiler=E4=BD=BF=E7=94=A8=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neutrino/core/aop/compiler/AsgcCompiler.java | 14 ++++++++++---- .../neutrino/core/aop/proxy/AsgcProxyFactory.java | 2 +- .../asgc/neutrino/core/aop/proxy/ProxyClass.java | 8 ++++++++ .../core/aop/compiler/AsgcCompilerTest.java | 12 ++++++------ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java index bcdc9e89..503f7a55 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java @@ -192,17 +192,23 @@ public class AsgcCompiler { /** * 编译代码 - * @param pkg 包名 * @param className 类名 * @param sourceCode 源代码 */ - public Class> compile(String pkg, String className, String sourceCode) throws ClassNotFoundException { + public Class> compile(String className, String sourceCode) throws ClassNotFoundException { log.info("options:" + getOptions()); + int index = className.lastIndexOf("."); + String pkg = ""; + String simpleClassName = className; + if (index >= 0) { + pkg = className.substring(0, index); + simpleClassName = className.substring(index + 1); + } JavaFileManager javaFileManager = new DynamicJavaFileManager(standardJavaFileManager, dynamicClassLoader); Iterable extends JavaFileObject> compilationUnits = Lists.newArrayList(new StringSource(className, sourceCode)); if (GlobalConfig.isSaveGeneratorCode()) { javaFileManager = standardJavaFileManager; - File file = FileUtil.save(GlobalConfig.getGeneratorCodeSavePath() + pkg.replaceAll("\\.", "/"), className + ".java", sourceCode); + File file = FileUtil.save(GlobalConfig.getGeneratorCodeSavePath() + pkg.replaceAll("\\.", "/"), simpleClassName + ".java", sourceCode); compilationUnits = standardJavaFileManager.getJavaFileObjects(file); addClasspath(GlobalConfig.getGeneratorCodeSavePath()); } @@ -229,7 +235,7 @@ public class AsgcCompiler { } } - return dynamicClassLoader.findClass(pkg + "." + className); + return dynamicClassLoader.findClass(className); } /** diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/AsgcProxyFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/AsgcProxyFactory.java index 4df457f3..8153a264 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/AsgcProxyFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/AsgcProxyFactory.java @@ -85,7 +85,7 @@ public class AsgcProxyFactory implements ProxyFactory { if (GlobalConfig.isPrintGeneratorCode()) { log.debug("类:{} 的代理类源码:\n{}", targetType.getName(), sourceCode); } - Class
retClass = (Class
)compiler.compile(proxyClass.getPkg(), proxyClass.getName(), proxyClass.getSourceCode()); + Class
retClass = (Class
)compiler.compile(proxyClass.getClassName(), proxyClass.getSourceCode()); P obj = retClass.newInstance(); return obj; } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/ProxyClass.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/ProxyClass.java index 4d9df772..fd1d8d01 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/ProxyClass.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/proxy/ProxyClass.java @@ -21,6 +21,7 @@ */ package fun.asgc.neutrino.core.aop.proxy; +import fun.asgc.neutrino.core.util.StringUtil; import lombok.Data; import java.util.Map; @@ -65,4 +66,11 @@ public class ProxyClass { public ProxyClass() { } + + public String getClassName() { + if (StringUtil.isEmpty(pkg)) { + return name; + } + return String.format("%s.%s", pkg, name); + } } 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 a88e5a93..65716fc6 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 @@ -44,7 +44,7 @@ public class AsgcCompilerTest { "\t\tSystem.out.println(\"hello\");\n" + "\t}\n" + "}\n"; - Class clazz = compiler.compile("a.b","Hello", code); + Class clazz = compiler.compile("a.b.Hello", code); Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("hello")).findFirst().get(); Object instance = clazz.newInstance(); method.invoke(instance); @@ -60,7 +60,7 @@ public class AsgcCompilerTest { "\t\tSystem.out.println(\"熊猫正在吃\" + food);\n" + "\t}\n" + "}\n"; - Class clazz = compiler.compile("a.b","Panda", code); + Class clazz = compiler.compile("a.b.Panda", code); Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("eat")).findFirst().get(); Object instance = clazz.newInstance(); method.invoke(instance, "竹子"); @@ -77,7 +77,7 @@ public class AsgcCompilerTest { "\t\tSystem.out.println(\"收音机播放\");\n" + "\t}\n" + "}\n"; - Class clazz = compiler.compile("a.b","RadioPlayer", code); + Class clazz = compiler.compile("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); @@ -99,7 +99,7 @@ public class AsgcCompilerTest { "\t}\n" + "}\n"; // GlobalConfig.setIsSaveGeneratorCode(true); - Class clazz = compiler.compile("a.b","RadioPlayer", code); + Class clazz = compiler.compile("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); @@ -138,7 +138,7 @@ public class AsgcCompilerTest { "\t}\n" + "}\n"; System.out.println(code); - Class clazz = compiler.compile("a.b","Test", code); + Class clazz = compiler.compile("a.b.Test", code); Method executeMethod = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("execute")).findFirst().get(); Method echoMethod = Stream.of(clazz.getDeclaredMethods()).filter(m -> m.getName().equals("echo")).findFirst().get(); Method dogEatMethod = Stream.of(clazz.getDeclaredMethods()).filter(m -> m.getName().equals("dogEat")).findFirst().get(); @@ -159,7 +159,7 @@ public class AsgcCompilerTest { "}\n"; AsgcCompiler compiler = new AsgcCompiler(); try { - Class clazz = compiler.compile( "fun.asgc.test","Calc", code); + Class clazz = compiler.compile( "fun.asgc.test.Calc", code); Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("invoke")).findFirst().get(); return method.invoke(null); } catch (ClassNotFoundException|IllegalAccessException|InvocationTargetException e) {