mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:20:07 +08:00
ClassScanner 增加忽略加载错误类的扫描方法
This commit is contained in:
parent
39dee801f0
commit
d298eb5b73
@ -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)
|
||||
|
@ -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文件<br>
|
||||
* 此方法首先扫描指定包名下的资源目录,如果未扫描到,则扫描整个classpath中所有加载的类
|
||||
@ -231,19 +242,6 @@ public class ClassScanner implements Serializable {
|
||||
return scan(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描
|
||||
*
|
||||
* @param forceScanJavaClassPaths 是否强制扫描其他位于classpath关联jar中的类
|
||||
* @param ignoreLoadError 是否或略扫描时loadClass的异常
|
||||
* @return 类集合
|
||||
*
|
||||
*/
|
||||
public Set<Class<?>> scan(boolean forceScanJavaClassPaths,boolean ignoreLoadError){
|
||||
this.ignoreLoadError = ignoreLoadError;
|
||||
return scan(forceScanJavaClassPaths);
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描包路径下满足class过滤器条件的所有class文件
|
||||
*
|
||||
@ -298,7 +296,7 @@ public class ClassScanner implements Serializable {
|
||||
/**
|
||||
* 忽略加载错误扫描后,可以获得之前扫描时加载错误的类名字集合
|
||||
*/
|
||||
public Set<String> getClassesOfLoadError(){
|
||||
public Set<String> 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);
|
||||
}
|
||||
}
|
||||
|
@ -37,8 +37,9 @@ public class ClassScanerTest {
|
||||
@Test
|
||||
@Ignore
|
||||
public void scanAllPackageIgnoreLoadErrorTest(){
|
||||
ClassScanner classScanner = new ClassScanner(null, null);
|
||||
final Set<Class<?>> classes = classScanner.scan(false,true);
|
||||
final ClassScanner classScanner = new ClassScanner(null, null);
|
||||
classScanner.setIgnoreLoadError(true);
|
||||
final Set<Class<?>> classes = classScanner.scan(false);
|
||||
Console.log(classes.size());
|
||||
Console.log(classScanner.getClassesOfLoadError());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user