diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicClassLoader.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicClassLoader.java index be7251c3..bba1c78c 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicClassLoader.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicClassLoader.java @@ -21,9 +21,7 @@ */ package fun.asgc.neutrino.core.aop.compiler; -import fun.asgc.neutrino.core.base.GlobalConfig; import fun.asgc.neutrino.core.util.ArrayUtil; -import fun.asgc.neutrino.core.util.ClassUtil; import fun.asgc.neutrino.core.util.CollectionUtil; import fun.asgc.neutrino.core.util.FileUtil; @@ -155,14 +153,20 @@ public class DynamicClassLoader extends ClassLoader { String className = fileName.substring(0, fileName.lastIndexOf(".")); String fullClassName = packageName + "." + className; Class clazz = null; - if (path.startsWith(GlobalConfig.getGeneratorCodeSavePath())) { - byte[] byteCode = FileUtil.readBytes(file); - clazz = super.defineClass(fullClassName, byteCode, 0, byteCode.length); - this.classMap.put(fullClassName, clazz); - } else { + try { clazz = Thread.currentThread().getContextClassLoader().loadClass(fullClassName); + } catch (ClassNotFoundException e) { + if (this.classMap.containsKey(fullClassName)) { + clazz = this.classMap.get(fileName); + } else { + byte[] byteCode = FileUtil.readBytes(file); + clazz = super.defineClass(fullClassName, byteCode, 0, byteCode.length); + this.classMap.put(fullClassName, clazz); + } + } + if (null != clazz) { + classes.add(clazz); } - classes.add(clazz); } else { String subPackagePath = path + "/" + fileName; String subPackageName = packageName + "." + fileName; diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicCompile.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicCompile.java deleted file mode 100644 index af0932b5..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicCompile.java +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package fun.asgc.neutrino.core.aop.compiler; - -/** - * - * @author: aoshiguchen - * @date: 2022/8/26 - */ -public @interface DynamicCompile { - -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicCompileProcess.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicCompileProcess.java deleted file mode 100644 index 959832b4..00000000 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicCompileProcess.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright (c) 2022 aoshiguchen - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package fun.asgc.neutrino.core.aop.compiler; - -import javax.annotation.processing.*; -import javax.lang.model.SourceVersion; -import javax.lang.model.element.TypeElement; -import javax.tools.Diagnostic; -import java.util.Set; - -/** - * - * @author: aoshiguchen - * @date: 2022/8/26 - */ -@SupportedAnnotationTypes("DynamicCompile") -@SupportedSourceVersion(SourceVersion.RELEASE_8) -public class DynamicCompileProcess extends AbstractProcessor { - -// @Override -// public synchronized void init(ProcessingEnvironment processingEnv) { -// super.init(processingEnv); -// } - - @Override - public boolean process(Set annotations, RoundEnvironment roundEnv) { - processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "---------Hello World!"); - return true; - } - -} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/PackageInternalsFinder.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/PackageInternalsFinder.java index b4e95e03..50723908 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/PackageInternalsFinder.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/PackageInternalsFinder.java @@ -21,8 +21,6 @@ */ package fun.asgc.neutrino.core.aop.compiler; -import fun.asgc.neutrino.core.util.CollectionUtil; - import javax.tools.JavaFileObject; import java.io.File; import java.io.IOException; 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 30775ce1..3eb2a08e 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 @@ -97,6 +97,7 @@ public class AsgcCompilerTest { "\t\tSystem.out.println(\"收音机播放\");\n" + "\t}\n" + "}\n"; +// GlobalConfig.setIsSaveGeneratorCode(true); 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();