refactor(utils): 加强 getPriority 方法的优先值判断.

加强优先级判断, 有利于后续使用时防止出现意外情况的问题.
顺便补充一手单元测试.
This commit is contained in:
2022-05-05 16:13:48 +08:00
parent 8be0978783
commit 830f05c90a
2 changed files with 45 additions and 4 deletions

View File

@ -56,9 +56,14 @@ internal fun File.deepListFiles(
* @return 获取 Finder 的优先级.
* @throws NoSuchFieldException 如果 Finder 没有添加 [FinderRules] 注解时抛出该异常.
*/
internal fun ExtensionPackageFinder.getPriority() =
this::class.java.getDeclaredAnnotation(FinderRules::class.java)?.priority
internal fun ExtensionPackageFinder.getPriority(): Int {
val value = this::class.java.getDeclaredAnnotation(FinderRules::class.java)?.priority
?: throw NoSuchFieldException("Finder did not add `FinderRules` annotation")
if (value < 0) {
throw IllegalArgumentException("Priority cannot be lower than 0. (Class: ${this::class.java})")
}
return value
}
/**
* 为 [AutoCloseable] 对象注册 Jvm Shutdown 钩子.