解决动态生成类依赖jar in jar编译报找不到类的问题.

This commit is contained in:
aoshiguchen
2023-02-24 00:18:40 +08:00
parent bcf984e191
commit 0d4bc5284f
5 changed files with 61 additions and 27 deletions
@@ -22,6 +22,7 @@
package fun.asgc.neutrino.core.aop.compiler;
import com.google.common.collect.Lists;
import fun.asgc.neutrino.core.aop.compiler.internal.JarLauncher;
import fun.asgc.neutrino.core.base.GlobalConfig;
import fun.asgc.neutrino.core.util.CollectionUtil;
import fun.asgc.neutrino.core.util.FileUtil;
@@ -73,11 +74,23 @@ public class AsgcCompiler {
if (null == javaCompiler) {
throw new RuntimeException("Can not load JavaCompiler from javax.tools.ToolProvider#getSystemJavaCompiler(),\n please confirm the application running in JDK not JRE.");
}
ClassLoader parent = classLoader;
if (SystemUtil.isStartupFromJar() && GlobalConfig.isIsUseSpringBootJar()) {
try {
log.debug("使用LaunchedURLClassLoader jar路径:{}", SystemUtil.getCurrentJarFilePath());
JarLauncher jarLauncher = new JarLauncher(parent, SystemUtil.getCurrentJarFilePath());
parent = jarLauncher.createClassLoader();
log.debug("使用LaunchedURLClassLoader instance:{}", parent);
} catch (Exception e) {
// ignore
log.error("使用LaunchedURLClassLoader异常", e);
}
}
this.collector = new DiagnosticCollector<>();
this.standardJavaFileManager = javaCompiler.getStandardFileManager(collector, null, null);
this.isSaveClassFile = false;
this.generatorCodeSavePath = GlobalConfig.getGeneratorCodeSavePath();
this.dynamicClassLoader = new DynamicClassLoader(classLoader, this);
this.dynamicClassLoader = new DynamicClassLoader(parent, this);
addOption("-Xlint:unchecked");
addOption("-implicit:class");
@@ -33,13 +33,13 @@ public abstract class ExecutableArchiveLauncher extends Launcher {
private ClassPathIndexFile classPathIndex;
public ExecutableArchiveLauncher() {
try {
this.archive = createArchive();
this.classPathIndex = getClassPathIndex(this.archive);
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
// try {
// this.archive = createArchive();
// this.classPathIndex = getClassPathIndex(this.archive);
// }
// catch (Exception ex) {
// throw new IllegalStateException(ex);
// }
}
protected ExecutableArchiveLauncher(Archive archive) {
@@ -23,21 +23,25 @@ public final class SpringBootJarParser {
*/
public static InputStream getInputStream(URI uri) {
try {
if (uri.getScheme().equals("jar")) {
URL url = uri.toURL();
String path = url.getPath();
int index = path.indexOf("!");
if (index >= 0) {
path = path.substring(0, index);
return uri.toURL().openStream();
} catch (Exception e) {
try {
if (uri.getScheme().equals("jar")) {
URL url = uri.toURL();
String path = url.getPath();
int index = path.indexOf("!");
if (index >= 0) {
path = path.substring(0, index);
}
if (path.startsWith("file:")) {
path = path.substring(5);
}
JarFile jarFile = new JarFile(new File(path));
return JarURLConnection.get(url, jarFile).getInputStream();
}
if (path.startsWith("file:")) {
path = path.substring(5);
}
JarFile jarFile = new JarFile(new File(path));
return JarURLConnection.get(url, jarFile).getInputStream();
} catch (Exception e1) {
// ignore
}
} catch (Exception e1) {
// ignore
}
return null;
}
@@ -44,6 +44,10 @@ public class GlobalConfig {
* 当程序是以jar方式运行时,强制保存生成的代码
*/
private static volatile boolean isSaveGeneratorCode = false;
/**
* 是否使用SpringBoot方式的jar
*/
private static volatile boolean isUseSpringBootJar = true;
/**
* 自动生成代码保存路径
*/
@@ -88,4 +92,12 @@ public class GlobalConfig {
public static String getMapperXmlFileBasePath() {
return mapperXmlFileBasePath;
}
public static boolean isIsPrintGeneratorCode() {
return isPrintGeneratorCode;
}
public static boolean isIsUseSpringBootJar() {
return isUseSpringBootJar;
}
}
@@ -23,6 +23,7 @@
package fun.asgc.neutrino.core.util;
import fun.asgc.neutrino.core.base.CodeBlock;
import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
@@ -31,6 +32,7 @@ import java.util.Objects;
* @author: aoshiguchen
* @date: 2022/6/16
*/
@Slf4j
public class SystemUtil {
public static void addShutdownHook(Runnable runnable) {
@@ -98,14 +100,17 @@ public class SystemUtil {
if (!isStartupFromJar()) {
return null;
}
String url = SystemUtil.class.getResource("").getPath();
if (url.startsWith("file:")) {
url = url.substring(5);
String path = SystemUtil.class.getResource("").getPath();
String res = path;
if (res.startsWith("file:")) {
res = res.substring(5);
}
if (url.contains("!")) {
url = url.substring(0, url.indexOf("!"));
int index = res.indexOf("!");
if (index >= 0) {
res = res.substring(0, index);
}
return url;
log.debug("getCurrentJarFilePath path:{} index:{} url:{}", path, index, res);
return res;
}
public static boolean isWindows() {