-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimize: make EnhancedServiceLoader more readable #4622
optimize: make EnhancedServiceLoader more readable #4622
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我认为这不算代码的重构,只是去除了部分代码警告,而增加了ClassCastException 的catch增加了应用启动时异常被吞,导致运行在错误环境下,这应该直接抛出而不是捕获,将问题阻挡在最前沿
关于此次pr我主要基于以下几点考虑: |
我认为这算是个代码优化,而非重构,你认为呢?
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/home/runner/work/seata/seata/common/src/main/java/io/seata/common/loader/EnhancedServiceLoader.java:24:8: Unused import - java.util.Collections. [UnusedImports]
I've removed it |
Codecov Report
@@ Coverage Diff @@
## develop #4622 +/- ##
=============================================
+ Coverage 48.90% 49.04% +0.13%
- Complexity 4042 4058 +16
=============================================
Files 734 734
Lines 25577 25579 +2
Branches 3156 3157 +1
=============================================
+ Hits 12509 12545 +36
+ Misses 11732 11699 -33
+ Partials 1336 1335 -1
|
throws ClassNotFoundException { | ||
//Check whether the definition has been loaded | ||
if (!isDefinitionContainsClazz(className, loader)) { | ||
Class<?> clazz = Class.forName(className, true, loader); | ||
if (!type.isAssignableFrom(clazz)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该抛出异常来处理,而不是返回个null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我考虑了一下,能运行到此处的异常情况有一种是SPI中指定的实现类并没有实现SPI文件指定的接口,此处应该抛出ClassCastException,并且在日志中提醒用户配置有问题。我已经在最新提交中处理,并且添加上诉场景的单元测试。
当SPI中实现类指定有问题时,日志将输出如下内容
[main] ERROR io.seata.common.loader.EnhancedServiceLoader$InnerEnhancedServiceLoader - can't cast io.seata.common.loader.FrenchHello to io.seata.common.loader.Hello1
[main] ERROR io.seata.common.loader.EnhancedServiceLoader$InnerEnhancedServiceLoader - Load [io.seata.common.loader.FrenchHello] class fail, please make sure the extension config in META-INF/seata/io.seata.common.loader.Hello1 implements io.seata.common.loader.Hello1.
…the implementation class specified therein
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Ⅰ. Describe what this PR did
refactor EnhancedServiceLoader.java to make codes more readable
Ⅱ. Does this pull request fix one issue?
no
Ⅲ. Why don't you add test cases (unit test/integration test)?
the test cases EnhancedServiceLoaderTest.java already exists
Ⅳ. Describe how to verify it
Just run the test cases EnhancedServiceLoaderTest.java to verify it
Ⅴ. Special notes for reviews