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

查看:82
本文介绍了如何确定 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 驱动程序名称)加载;
  • 动态添加注释;
  • 由于外部输入(例如用户输入、来自数据库的数据或远程过程调用)而加载;

所以仅仅查看源代码是不够的.话虽如此,任何合理的 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.

不过,像这样的工具确实有一些价值.您可能想要查看类似 Emma 的内容.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天全站免登陆