bytekit icon indicating copy to clipboard operation
bytekit copied to clipboard

ReflectionUtils中的doWithMethods()方法处理逻辑不那么优雅..

Open zwm325715 opened this issue 2 years ago • 2 comments

背景:

  1. ~~我们是个代码洁癖爱好者~~ 2.该方法既然是个通用的工具方法,那么当我们的clazz是个接口时,此时实现逻辑会有那么一点尴尬(同一个方法会重复callback)~~

//步骤1.getDeclaredMethods(clazz)方法功能其实是已经去clazz继承的接口中拿了一下default以及static类型的方法。 Method[] methods = getDeclaredMethods(clazz); for (Method method : methods) { XXX }if (clazz.getSuperclass() != null) { doWithMethods(clazz.getSuperclass(), mc, mf); } //此时由于clazz是个接口,会走到这里 else if (clazz.isInterface()) { //此时又去拿clazz接口继承的接口了,然后递归doWithMethods()时一定又会拿到上面"步骤1中继承的接口中已经拿到过的default和static方法",也就是说重复callback了 for (Class<?> superIfc : clazz.getInterfaces()) { doWithMethods(superIfc, mc, mf); } }

zwm325715 avatar Apr 19 '23 06:04 zwm325715

这个代码来自spring,可以去官方提改进issue。

hengyunabc avatar Apr 19 '23 06:04 hengyunabc

MethodProcessor类中的 public void loadThis(final InsnList instructions) {} 方法逻辑感觉可优化.

1.该方法的本质其实是围绕着是否需要把当前方法的对象this加载到operand stack上. 那么此时对于构造方法和普通方法来讲都是一样的逻辑,都需要从Thread Stack Frame中的local variables[0]处加载this到operand stack上,所以感觉方法中对于构造方法的判断多余了,只是需要判断是否静态方法即可.

2.实际业务中我们大部分增强的方法都不是构造方法,判断构造方法的逻辑大部分是不满足的(白判断).

zwm325715 avatar Apr 24 '23 09:04 zwm325715