简体   繁体   English

检查 Java 中是否使用了类或方法

[英]Check if class or method is used in Java

I want to check if a class or method is used in a jar file to delete the ones that are not used.我想检查 jar 文件中是否使用了类或方法来删除未使用的类或方法。 I don't know how to approach it.我不知道如何接近它。

You can use an IDE to do what you are looking for (Netbeans IDE or IntelliJ Jetbrains)您可以使用 IDE 来执行所需的操作(Netbeans IDE 或 IntelliJ Jetbrains)

The process is simple, import the jar file, and check for the definition/declaration of a particular class.该过程很简单,导入 jar 文件,并检查特定类的定义/声明。

The most used way to identify if a method is used is by building a call graph.确定某个方法是否被使用的最常用方法是构建调用图。 If a method is not called, then it can be removed.如果一个方法没有被调用,那么它可以被删除。 However, call graphs are not always sound because of the usages of reflection, unsafe, and polymorphism.然而,由于反射、不安全和多态的使用,调用图并不总是健全的。 So if you want to remove a method, you have to be sure that this method is not used by reflection or the other categories of unsoundness.所以如果你想删除一个方法,你必须确保这个方法不被反射或其他类别的不健全所使用。

In order to remove classes, you have not only to ensure that the methods are not used but also the fields of the classes.为了删除类,您不仅要确保不使用方法,还要确保不使用类的字段。 Therefore, you need to identify all methods that might reference a field.因此,您需要确定可能引用某个字段的所有方法。

All the described analyses you can approach using the tools Soot, WALA, Javassist, or OPAL.您可以使用工具 Soot、WALA、Javassist 或 OPAL 进行所有描述的分析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM