From 07a9069d24283107600f429d2e67119f5fcc22a3 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Sun, 11 Sep 2022 13:40:55 +0800 Subject: [PATCH] =?UTF-8?q?DynamicClassLoader=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E8=A7=A3=E5=86=B3=E9=83=A8=E5=88=86=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B=E5=87=BA=E7=8E=B0=E7=B1=BB=E9=87=8D=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/aop/compiler/DynamicClassLoader.java | 20 +++++--- .../core/aop/compiler/DynamicCompile.java | 31 ------------ .../aop/compiler/DynamicCompileProcess.java | 50 ------------------- .../aop/compiler/PackageInternalsFinder.java | 2 - .../core/aop/compiler/AsgcCompilerTest.java | 1 + 5 files changed, 13 insertions(+), 91 deletions(-) delete mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicCompile.java delete mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/DynamicCompileProcess.java 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();