From e651a70c703fd5eaf91808d67c7f67b4aa403fe0 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Thu, 8 Sep 2022 23:20:29 +0800 Subject: [PATCH] =?UTF-8?q?AsgcCompiler=E9=80=BB=E8=BE=91=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/aop/compiler/AsgcCompiler.java | 65 +++++++++++++++++-- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java index 128c4151..e6696879 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/aop/compiler/AsgcCompiler.java @@ -24,10 +24,13 @@ package fun.asgc.neutrino.core.aop.compiler; import com.google.common.collect.Lists; import fun.asgc.neutrino.core.base.GlobalConfig; import fun.asgc.neutrino.core.util.CollectionUtil; +import fun.asgc.neutrino.core.util.SystemUtil; import lombok.extern.slf4j.Slf4j; import javax.tools.*; import java.io.File; +import java.net.URL; +import java.net.URLClassLoader; import java.util.*; import java.util.stream.Collectors; @@ -43,6 +46,7 @@ public class AsgcCompiler { private final DiagnosticCollector collector; private final StandardJavaFileManager standardJavaFileManager; private final List options = new ArrayList<>(); + private final List defaultClassPathList = new ArrayList<>(); private final List classpathList = new ArrayList<>(); private final Collection compilationUnits = new ArrayList(); private boolean isSaveSourceCodeFile; @@ -91,9 +95,10 @@ public class AsgcCompiler { private List getOptions() { List list = Lists.newArrayList(options); - if (!CollectionUtil.isEmpty(classpathList)) { + List cp = getClasspathList(); + if (!CollectionUtil.isEmpty(cp)) { list.add("-classpath"); - list.add(classpathList.stream().collect(Collectors.joining(File.pathSeparator))); + list.add(cp.stream().collect(Collectors.joining(File.pathSeparator))); } return list; } @@ -172,11 +177,61 @@ public class AsgcCompiler { private void log() { List warnings = getWarnings(); List errors = getErrors(); - if (!CollectionUtil.isEmpty(warnings)) { - log.warn(warnings.stream().collect(Collectors.joining())); - } +// if (!CollectionUtil.isEmpty(warnings)) { +// log.warn(warnings.stream().collect(Collectors.joining())); +// } if (!CollectionUtil.isEmpty(errors)) { log.warn(errors.stream().collect(Collectors.joining())); } } + + private URLClassLoader getURLClassLoader() { + ClassLoader ret = Thread.currentThread().getContextClassLoader(); + if (null == ret) { + ret = AsgcCompiler.class.getClassLoader(); + } + return (ret instanceof URLClassLoader) ? (URLClassLoader)ret : null; + } + + private List getClasspathList() { + List classpathList = new ArrayList<>(); + List defaultClasspathList = getDefaultClasspathList(); + List customClasspathList = this.classpathList; + if (!CollectionUtil.isEmpty(defaultClasspathList)) { + classpathList.addAll(defaultClasspathList); + } + if (!CollectionUtil.isEmpty(customClasspathList)) { + classpathList.addAll(customClasspathList); + } + return classpathList; + } + + private synchronized List getDefaultClasspathList() { + if (!CollectionUtil.isEmpty(defaultClassPathList)) { + return defaultClassPathList; + } + URLClassLoader classLoader = getURLClassLoader(); + if (null == classLoader) { + return defaultClassPathList; + } + + boolean isWindows = SystemUtil.isWindows(); + for (URL url : classLoader.getURLs()) { + String path = url.getFile(); + + // 如果是 windows 系统,去除前缀字符 '/' + if (isWindows && path.startsWith("/")) { + path = path.substring(1); + } + + // 去除后缀字符 '/' + if (path.length() > 1 && (path.endsWith("/") || path.endsWith(File.separator))) { + path = path.substring(0, path.length() - 1); + } + + defaultClassPathList.add(path); + } + + return defaultClassPathList; + } }