-
Notifications
You must be signed in to change notification settings - Fork 0
<test>[testlib]: cjlib improve #4162
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
base: 4.8.38
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -69,6 +69,33 @@ class TProxy { | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| TProxy(Object adaptee, Class[] argumentTypes, Object[] arguments) { | ||||||||||||||||||||||||||||||
| assert adaptee != null : "call TProxy(Class adapteeClass) instead" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Enhancer enhancer = new Enhancer() | ||||||||||||||||||||||||||||||
| enhancer.setSuperclass(adaptee.getClass()) | ||||||||||||||||||||||||||||||
| enhancer.setCallback(new ProxyHandler()) | ||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||
| proxyedObject = enhancer.create(argumentTypes, arguments) | ||||||||||||||||||||||||||||||
| if (adaptee != null) { | ||||||||||||||||||||||||||||||
| FieldUtils.getAllFields(adaptee.getClass()).each { f -> | ||||||||||||||||||||||||||||||
| if (Modifier.isStatic(f.modifiers) || Modifier.isFinal(f.modifiers)) { | ||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| f.setAccessible(true) | ||||||||||||||||||||||||||||||
| f.set(proxyedObject, f.get(adaptee)) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } catch (IllegalArgumentException e) { | ||||||||||||||||||||||||||||||
| if (e.getMessage().contains("Superclass has no null constructors" )) { | ||||||||||||||||||||||||||||||
| throw new Exception("the class ${adaptee.getClass()} has no non-argument constructor", e) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| throw e | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+90
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修正异常消息以匹配参数化构造的上下文。 第 92 行的错误消息"has no non-argument constructor"在此上下文中不正确。当调用带参数的构造函数时,该消息具有误导性——实际问题应该是找不到匹配的参数化构造函数,而非无参构造函数缺失。 🐛 建议修正错误消息 } catch (IllegalArgumentException e) {
if (e.getMessage().contains("Superclass has no null constructors" )) {
- throw new Exception("the class ${adaptee.getClass()} has no non-argument constructor", e)
+ throw new Exception("the class ${adaptee.getClass()} has no matching constructor for the given argument types", e)
}
throw e📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+72
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win 消除与现有构造函数的代码重复。 新构造函数与 建议提取公共逻辑到私有辅助方法中,以提高可维护性并减少未来出现不一致的风险。 ♻️ 建议重构以消除重复+ private void copyFields(Object source, Object target) {
+ FieldUtils.getAllFields(source.getClass()).each { f ->
+ if (Modifier.isStatic(f.modifiers) || Modifier.isFinal(f.modifiers)) {
+ return
+ }
+ f.setAccessible(true)
+ f.set(target, f.get(source))
+ }
+ }
+
+ private Object createProxy(Object adaptee, Closure createEnhancer) {
+ Enhancer enhancer = new Enhancer()
+ enhancer.setSuperclass(adaptee.getClass())
+ enhancer.setCallback(new ProxyHandler())
+ try {
+ def proxy = createEnhancer(enhancer)
+ if (adaptee != null) {
+ copyFields(adaptee, proxy)
+ }
+ return proxy
+ } catch (IllegalArgumentException e) {
+ if (e.getMessage().contains("Superclass has no null constructors")) {
+ throw new Exception("the class ${adaptee.getClass()} has no matching constructor", e)
+ }
+ throw e
+ }
+ }
+
TProxy(Object adaptee) {
assert adaptee != null : "call TProxy(Class adapteeClass) instead"
- Enhancer enhancer = new Enhancer()
- enhancer.setSuperclass(adaptee.getClass())
- enhancer.setCallback(new ProxyHandler())
- try {
- proxyedObject = enhancer.create()
- if (adaptee != null) {
- FieldUtils.getAllFields(adaptee.getClass()).each { f ->
- if (Modifier.isStatic(f.modifiers) || Modifier.isFinal(f.modifiers)) {
- return
- }
-
- f.setAccessible(true)
- f.set(proxyedObject, f.get(adaptee))
- }
- }
- } catch (IllegalArgumentException e) {
- if (e.getMessage().contains("Superclass has no null constructors" )) {
- throw new Exception("the class ${adaptee.getClass()} has no non-argument constructor", e)
- }
-
- throw e
- }
+ proxyedObject = createProxy(adaptee) { enhancer -> enhancer.create() }
}
TProxy(Object adaptee, Class[] argumentTypes, Object[] arguments) {
assert adaptee != null : "call TProxy(Class adapteeClass) instead"
- Enhancer enhancer = new Enhancer()
- enhancer.setSuperclass(adaptee.getClass())
- enhancer.setCallback(new ProxyHandler())
- try {
- proxyedObject = enhancer.create(argumentTypes, arguments)
- if (adaptee != null) {
- FieldUtils.getAllFields(adaptee.getClass()).each { f ->
- if (Modifier.isStatic(f.modifiers) || Modifier.isFinal(f.modifiers)) {
- return
- }
-
- f.setAccessible(true)
- f.set(proxyedObject, f.get(adaptee))
- }
- }
- } catch (IllegalArgumentException e) {
- if (e.getMessage().contains("Superclass has no null constructors" )) {
- throw new Exception("the class ${adaptee.getClass()} has no non-argument constructor", e)
- }
-
- throw e
- }
+ proxyedObject = createProxy(adaptee) { enhancer -> enhancer.create(argumentTypes, arguments) }
}🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||
| * @param name: 要替换函数的名称 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
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.
添加参数验证以防止运行时错误。
构造函数缺少对
argumentTypes和arguments的验证:argumentTypes.length == arguments.length,类型与参数数量不匹配会在运行时失败🛡️ 建议添加参数验证
TProxy(Object adaptee, Class[] argumentTypes, Object[] arguments) { assert adaptee != null : "call TProxy(Class adapteeClass) instead" + assert argumentTypes != null : "argumentTypes cannot be null" + assert arguments != null : "arguments cannot be null" + assert argumentTypes.length == arguments.length : "argumentTypes and arguments must have the same length"🤖 Prompt for AI Agents