grep 用于特定的 jvm 字节码模式
我正在开发一个遗留的 java 项目,该项目存在许多设计问题。因此,代码的某些部分不会按预期运行。考虑下面的代码:
public enum Parent{
PARENT1(CHILD1, CHILD2), PARENT2(CHILD3, CHILD1),
PARENT3(CHILD4, CHILD2)
private Child [] children;
Parent(Child ...children) { this.children = children; }
public Child [] getChildren() { return this.children; }
}
public enum Child{ CHILD1, CHILD2, CHILD3, CHILD4 }
现在从上面的代码中,您可以清楚地看到 getChildren() 返回对数组的引用。因此,可以像这样更改数组的元素:
Child [] childrenOfParent3 = Parent3.getChildren();
childrenOfParent3[0] = null; //badcode
某些代码正在执行类似于上面提到的操作,并且我很难找到它(grep 没有帮助)。
有没有任何工具可以帮助我使用 jvm 字节码分析来定位这个模式(坏代码),或者你会如何做?
注意:由于某些限制,我无法更改父级实现。
谢谢!
I am working on a legacy java project which has a number of design issues. As a result, some parts of the code don't behave as expected. Consider the following piece of code:
public enum Parent{
PARENT1(CHILD1, CHILD2), PARENT2(CHILD3, CHILD1),
PARENT3(CHILD4, CHILD2)
private Child [] children;
Parent(Child ...children) { this.children = children; }
public Child [] getChildren() { return this.children; }
}
public enum Child{ CHILD1, CHILD2, CHILD3, CHILD4 }
Now from the above code, you can clearly see that getChildren() returns reference to the array. Hence, it is possible to change the elements of the array like this:
Child [] childrenOfParent3 = Parent3.getChildren();
childrenOfParent3[0] = null; //badcode
Some piece of code is doing something like the above mentioned and, I am having a hard time trying to locate this(grep does not help).
Is there any tool out there which can help me locate this pattern(badcode) using jvm bytecode analysis or how would you do it?
NOTE: Due to some constraints, I cannot change Parent implementation.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Findbugs 能够检测 这个。
Findbugs is capable of detecting this.
我们在这里讨论了多少个实例? Eclipse 调试器等工具允许数据观察点(例如:对象修改)。这将导致调试器立即停止在正在修改相关对象的代码行上。唯一的技巧是您需要在对象创建时放置一个断点,以便您可以识别要观看的对象。
How many instances are we talking about here? Tools like the Eclipse debugger allow for data watchpoints (eg: object modified). That will cause the debugger to stop immediately on the exact line of code that is modifying the object in question. The only trick is you need to put a breakpoint in at object creation so that you can identify the object you want to watch.
您可以使用 Java Decompiler 将所有字节代码转换为源代码,然后您应该能够出现使用正则表达式来匹配有问题的代码(或使用 FindBugs?)。
You can use a Java Decompiler turn all the byte code into source, then you should be able to come up with a regex to match the code in question (or use FindBugs?).