修复ClassScanner自定义classload无效问题

This commit is contained in:
Looly 2023-01-17 17:30:45 +08:00
parent 9183a9100e
commit 9994083fe1
3 changed files with 23 additions and 3 deletions

View File

@ -20,6 +20,7 @@
* 【core 】 修复FileTypeUtil判断wav后缀的录音文件类型不能匹配问题pr#2834@Github
* 【core 】 修复FileUtil的rename在newName与原文件夹名称一样时文件夹会被删除问题issue#2845@Github
* 【core 】 修复IoUtil.readBytes使用SocketInputStream读取不完整问题issue#I6AT49@Gitee
* 【core 】 修复ClassScanner自定义classload无效问题issue#I68TV2@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -7,6 +7,7 @@ import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
@ -173,10 +174,28 @@ public class ResourceUtil {
* @since 4.1.5
*/
public static EnumerationIter<URL> getResourceIter(String resource) {
return getResourceIter(resource, null);
}
/**
* 获取指定路径下的资源Iterator<br>
* 路径格式必须为目录格式,/分隔例如:
*
* <pre>
* config/a
* spring/xml
* </pre>
*
* @param resource 资源路径
* @param classLoader {@link ClassLoader}
* @return 资源列表
* @since 4.1.5
*/
public static EnumerationIter<URL> getResourceIter(String resource, ClassLoader classLoader) {
final Enumeration<URL> resources;
try {
resources = ClassLoaderUtil.getClassLoader().getResources(resource);
} catch (IOException e) {
resources = ObjUtil.defaultIfNull(classLoader, ClassLoaderUtil::getClassLoader).getResources(resource);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return new EnumerationIter<>(resources);

View File

@ -255,7 +255,7 @@ public class ClassScanner implements Serializable {
this.classes.clear();
this.classesOfLoadError.clear();
for (URL url : ResourceUtil.getResourceIter(this.packagePath)) {
for (URL url : ResourceUtil.getResourceIter(this.packagePath, this.classLoader)) {
switch (url.getProtocol()) {
case "file":
scanFile(new File(URLUtil.decode(url.getFile(), this.charset.name())), null);