DynamicClassLoader代码优化,解决部分情况下出现类重定义的问题
This commit is contained in:
+12
-8
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
-50
@@ -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<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "---------Hello World!");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
-2
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user