如何确定Java程序使用哪些类? [英] How to determine which classes are used by a Java program?

查看:105
本文介绍了如何确定Java程序使用哪些类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何工具列出了某些应用程序有效使用某些类别的工具,甚至更好地自动修剪JAR库仅提供引用和使用的类?

Is there any tool that lists which and when some classes are effectively used by an app or, even-better, automatically trims JAR libraries to only provide classes that are both referenced and used?

推荐答案

请记住,如停止问题,你不能肯定说一个特定的类是或不被使用。至少在任何适度复杂的应用程序。这是因为类不仅仅是在编译时绑定,而是可以加载:

Bear in mind that, as proven by the halting problem, you can't definitely say that a particular class is or isn't used. At least on any moderately complex application. That's because classes aren't just bound at compile-time but can be loaded:


  • 基于XML配置(例如Spring)从属性文件(例如JDBC驱动程序名称)加载$ li
  • ;

  • 动态添加注释;

  • 加载为外部输入的结果(例如用户输入,数据库或远程程序调用的数据);

  • 等。

  • based on XML config (eg Spring);
  • loaded from properties files (eg JDBC driver name);
  • added dynamically with annotations;
  • loaded as a result of external input (eg user input, data from a database or remote procedure call);
  • etc.

所以只看源代码是不够的。话虽如此,任何合理的IDE都将为您提供依赖关系分析工具。 IntelliJ当然可以。

So just looking at source code isn't enough. That being said, any reasonable IDE will provide you with dependency analysis tools. IntelliJ certainly does.

您真正需要的是运行时测试您的应用程序正在做什么,但即使这不能保证。毕竟,由于输入的奇怪组合,特定的代码路径可能会出现在1000万次,因此您无法保证被覆盖。

What you really need is runtime instrumentation on what your application is doing but even that isn't guaranteed. After all, a particular code path might come up one in 10 million runs due to a weird combination of inputs so you can't be guaranteed that you're covered.

这样的工具确实有一些价值。您可能需要查看艾玛等内容。剖析器像 Yourkit 可以为您提供代码转储,您也可以进行分析(尽管不会选择暂时的对象非常好)。

Tools like this do have some value though. You might want to look at something like Emma. Profilers like Yourkit can give you a code dump that you can do an analysis on too (although that won't pick up transient objects terribly well).

个人来说,我发现,除了IDE将会告诉你的几点价值:删除未使用的JAR。比这更细微,只是要求麻烦,一点也没有收获。

Personally I find little value beyond what the IDE will tell you: removing unused JARs. Going more granular than that is just asking for trouble for little to no gain.

这篇关于如何确定Java程序使用哪些类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆