使用 AspectJ 查找 Java 中未使用的代码
我有一个在大型 Java 项目中查找未使用(“死”)方法的想法,但我需要帮助导出实现。
- 使用 AspectJ 向项目包中的所有方法添加“之前”方面。该方面将简单地记录(?)该方法已被执行。
- 我编译项目包中所有类/方法的列表(可能使用服务定位器/反射)。
- 建议的代码经过完整的回归测试。理想情况下, 我也想将其投入生产一段时间(如果合适的话) 可以找到高性能的解决方案)。
- 已执行方法(步骤 1)和可用方法(步骤 2) 进行比较,生成所有方法的综合列表 从未被调用(即死代码)。
由于步骤 2 和 4 可以离线执行,因此我实际上只是在寻求有关步骤 1 的帮助。
具体来说,如何记录方法的执行时间?我想如果我尝试任何类型的内存存储,我很快就会遇到 OutOfMemoryErrors。同样,如果我将数据存储在数据库/文件系统中,调用量可能会导致重大性能问题。有人做过类似的事情吗?任何意见/建议表示赞赏。
I have an idea for finding unused ('dead') methods in a large Java project but I need help deriving an implementation.
- Use AspectJ to add a 'before' aspect to ALL methods in project packages. The aspect will simply record (?) that the method has been executed.
- I compile a list of all classes/methods in project packages (probably using a service locator/reflection).
- The advised code is subjected to a full regression test. Ideally,
I'd like to put this into production for a while too (if a suitably
performant solution can be found). - The lists of executed methods (Step 1) and available methods (Step
2) are compared, yielding a comprehensive list of all methods that
were never called (i.e. dead code).
Since steps 2 and 4 can be conducted offline, I'm really only looking for help with Step 1.
Specifically, how can I record when a method is executed? I figure I'm going to encounter OutOfMemoryErrors pretty soon if I attempt any kind of in-memory storage. Likewise, if I store the data in a database/on the file-system, the volume of calls is likely to cause major performance issues. Has anyone ever done something similar? Any advice/suggestions appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试查看流行的测试覆盖率库,例如 Cobertura 或 艾玛。他们确实做了你所说的事情,还有一些,尽管不是使用 AspectJ。至少,Cobertura 将调用信息存储到内存中似乎没有问题。
Try checking out popular test coverage libraries like Cobertura or EMMA. They do exactly what you're talking about and then some, though not with AspectJ. Cobertura, at least, seems to have no problem storing invocation information down to the line in memory.
那么您需要打电话前的建议。根据该建议,您需要记录正在调用的方法。您可能希望保留一组被调用的方法,以免出现重复。您可以从 thisJointPoint 获取当前方法。我可以给出它的 AspectJ 代码,但我认为这不是重点。
我认为你最好只使用一个工具来解析二进制 .class 文件,使用 BECL 或 ASM,从您知道的方法开始< /em> 被调用,并构建调用图。这类似于 JVM 进行垃圾收集的方式。
但是,你真的应该问问自己,你到底是什么?是性能吗?因为影响应该是没有的。如果你想减小 .class 文件的大小,最好使用 ProGuard 之类的东西。这将解决这个问题和其他问题,而且已经解决了。
也就是说,我认为如果您确实想这样做,最好的方法是使用 Cobertura 来检测您的代码,并对您的应用程序进行运行,然后查看您的覆盖率报告。不需要很长时间,90% 的已用代码就会被绘制出来。任何可以删除而不会导致编译错误的未调用方法都是死代码。
Well you want a before call advice. On that advice you'd want to log the method being called. Probably you'd want to keep a set of the methods called so that you don't get duplicates. You could get the current method off of the thisJointPoint. I could give the AspectJ code for it but I think it's beside the point.
I think you'd be better off just using a tool to parse the binary .class files using BECL or ASM, starting with methods you know get called, and build a call graph. This is similar to how the JVM does garbage collection.
But, really you should ask yourself for what you are this. Is it performance? Because the impact should be none. If you want to reduce the size of the .class files you'd be better off using something like ProGuard. That will take care of that and other issues and its already made.
That said I think the best approach if you really want to do this is to instrument your code using Cobertura, and do a run-through of your application and then look at your coverage reports. It shouldn't take long for 90% of your used code to be painted. Any uncalled methods that you can delete without causing a compile error are dead code.