AsgcCompiler使用方式优化

This commit is contained in:
aoshiguchen
2023-03-01 12:05:14 +08:00
parent b8d957fccb
commit 790ded499c
4 changed files with 25 additions and 11 deletions
@@ -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);
}
/**
@@ -85,7 +85,7 @@ public class AsgcProxyFactory implements ProxyFactory {
if (GlobalConfig.isPrintGeneratorCode()) {
log.debug("类:{} 的代理类源码:\n{}", targetType.getName(), sourceCode);
}
Class<P> retClass = (Class<P>)compiler.compile(proxyClass.getPkg(), proxyClass.getName(), proxyClass.getSourceCode());
Class<P> retClass = (Class<P>)compiler.compile(proxyClass.getClassName(), proxyClass.getSourceCode());
P obj = retClass.newInstance();
return obj;
}
@@ -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);
}
}
@@ -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) {