From d298eb5b73860109c3826a7ebd92bc24da25fa6b Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 8 Nov 2022 19:20:09 +0800 Subject: [PATCH] =?UTF-8?q?ClassScanner=20=E5=A2=9E=E5=8A=A0=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E5=8A=A0=E8=BD=BD=E9=94=99=E8=AF=AF=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../cn/hutool/core/lang/ClassScanner.java | 32 +++++++++---------- .../cn/hutool/core/lang/ClassScanerTest.java | 5 +-- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df0afb0fe..eb3fef749 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * 【core 】 FileUtil.copy,当来源为文件时,返回文件而非目录(issue#I5YCVL@Gitee) * 【db 】 DialectFactory增加identifyDriver重载(issue#I5YWI6@Gitee) * 【core 】 去除ClassloaderUtil的Cache(issue#I5YWI6@Gitee) +* 【core 】 ClassScanner 增加忽略加载错误类的扫描方法(pr#855@Gitee) ### 🐞Bug修复 * 【db 】 修复分页时order by截断问题(issue#I5X6FM@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/ClassScanner.java b/hutool-core/src/main/java/cn/hutool/core/lang/ClassScanner.java index 06bca8ec3..8642d2a08 100755 --- a/hutool-core/src/main/java/cn/hutool/core/lang/ClassScanner.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/ClassScanner.java @@ -221,6 +221,17 @@ public class ClassScanner implements Serializable { this.charset = charset; } + /** + * 设置是否忽略loadClass时的错误 + * + * @param ignoreLoadError 忽略loadClass时的错误 + * @return this + */ + public ClassScanner setIgnoreLoadError(boolean ignoreLoadError) { + this.ignoreLoadError = ignoreLoadError; + return this; + } + /** * 扫描包路径下满足class过滤器条件的所有class文件
* 此方法首先扫描指定包名下的资源目录,如果未扫描到,则扫描整个classpath中所有加载的类 @@ -231,19 +242,6 @@ public class ClassScanner implements Serializable { return scan(false); } - /** - * 扫描 - * - * @param forceScanJavaClassPaths 是否强制扫描其他位于classpath关联jar中的类 - * @param ignoreLoadError 是否或略扫描时loadClass的异常 - * @return 类集合 - * - */ - public Set> scan(boolean forceScanJavaClassPaths,boolean ignoreLoadError){ - this.ignoreLoadError = ignoreLoadError; - return scan(forceScanJavaClassPaths); - } - /** * 扫描包路径下满足class过滤器条件的所有class文件 * @@ -298,7 +296,7 @@ public class ClassScanner implements Serializable { /** * 忽略加载错误扫描后,可以获得之前扫描时加载错误的类名字集合 */ - public Set getClassesOfLoadError(){ + public Set getClassesOfLoadError() { return Collections.unmodifiableSet(this.classesOfLoadError); } @@ -397,10 +395,10 @@ public class ClassScanner implements Serializable { } catch (UnsupportedClassVersionError e) { // 版本导致的不兼容的类,跳过 classesOfLoadError.add(className); - } catch (Throwable e){ - if(!this.ignoreLoadError) { + } catch (Throwable e) { + if (false == this.ignoreLoadError) { throw new RuntimeException(e); - }else{ + } else { classesOfLoadError.add(className); } } diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/ClassScanerTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/ClassScanerTest.java index 2c2d243df..b812e4c7d 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/ClassScanerTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/ClassScanerTest.java @@ -37,8 +37,9 @@ public class ClassScanerTest { @Test @Ignore public void scanAllPackageIgnoreLoadErrorTest(){ - ClassScanner classScanner = new ClassScanner(null, null); - final Set> classes = classScanner.scan(false,true); + final ClassScanner classScanner = new ClassScanner(null, null); + classScanner.setIgnoreLoadError(true); + final Set> classes = classScanner.scan(false); Console.log(classes.size()); Console.log(classScanner.getClassesOfLoadError()); }