简体   繁体   English

Java查找在代码中使用类的位置-以编程方式

[英]Java find where a class is used in the code - programmatically

I have a List of classes which I can iterate through. 我有一个可以迭代的类列表。 Using Java is there a way of finding out where these classes are used so that I can write it out to a report? 使用Java可以找出使用这些类的位置,以便可以将其写到报告中?

I know that I can find out using 'References' in Eclipse but there are too many to be able to do this manually. 我知道我可以在Eclipse中使用“参考”来查找,但是有太多的内容无法手动完成。 So I need to be able to do this programmatically. 因此,我需要能够以编程方式执行此操作。 Can anyone give me any pointers please? 有人可以给我指点吗?

Edit: This is static analysis and part of creating a bigger traceability report for non-technical people. 编辑:这是静态分析,是为非技术人员创建更大的可追溯性报告的一部分。 I have comprehensive Javadocs but they are not 'friendly' and also work in the opposite direction to how I need the report. 我有全面的Javadocs,但它们不是“友好的”,而且工作方式与我需要报告的方向相反。 Javadocs start from package and work downwards, whereas I need to start a variable level and work upwards. Javadocs从程序包开始,向下进行工作,而我需要从变量级别开始,向上进行工作。 If that makes any sense. 如果那有意义的话。

You could try to add a stacktrace dump somewhere in the class that isolates the specific case you are looking for. 您可以尝试在类中的某个位置添加stacktrace转储,以隔离您要查找的特定情况。

public void someMethodInMyClass()
{
    if (conditions_are_met_to_identify)
    {
        Thread.dumpStack();
    }
    // ... original code here
}

You may have to scan all the sources, and check the import statements. 您可能必须扫描所有源,并检查导入语句。 (Taking care of the * imports.. having to setup your scanner for both the fully Qualified class name and its packagename.*) (注意*导入。必须为完全合格的类名称及其包名称设置扫描仪。)

EDIT: It would be great to use the eclipse search engine for this. 编辑:最好使用Eclipse搜索引擎。 Perhaps here is the answer 也许这就是答案

Still another approach (probably not complete): 还有另一种方法(可能不完整):

Search Google for 'java recursively list directories and files' and get source code that will recursively list all the *.java file path/names in a project. 在Google上搜索“ java递归列出目录和文件”,并获取将递归列出项目中所有* .java文件路径/名称的源代码。 For each file in the list: 对于列表中的每个文件:

1: See if the file path/name is in the list of fully qualified file names you are interested in. If so, record is path/name as a match. 1:查看文件路径/名称是否在您感兴趣的标准文件名列表中。如果是,则记录路径/名称作为匹配项。

2: Regardless if its a match or not, open the file and copy its content to a List collection. 2:无论是否匹配,都打开文件并将其内容复制到List集合。 Iterate through the content list and see if the class name is present. 遍历内容列表,查看类名称是否存在。 If found, determine its path by seeing if its in the same package as the current file you are examining. 如果找到,请确定其路径是否与您正在检查的当前文件位于同一软件包中。 If so, you have a match. 如果是这样,您将有一场比赛。 If not, you need to extract the paths from the *.import statements, add it to the class name, and see if it exists in your recursive list of file path/names. 如果不是,则需要从* .import语句中提取路径,将其添加到类名称中,然后查看它是否存在于文件路径/名称的递归列表中。 If still not found, add it to a 'not found' list (including what line number it was found on) so you can manually see why it was not identified. 如果仍然找不到,请将其添加到“未找到”列表(包括找到的行号)中,以便您手动查看为什么未找到它。

3: Add all matches to a 'found match' list. 3:将所有匹配项添加到“找到的匹配项”列表中。 Examine the list to ensure it looks correct. 检查列表以确保它看起来正确。

Not sure what you are trying to do, but in case you want to analyse code during runtime, I would use an out-of-the box profiler that shows you what is loaded and what allocated. 不确定您要做什么,但是如果您想在运行时分析代码,我将使用现成的探查器,该探查器向您显示加载的内容和分配的内容。

@Open source profilers: Open Source Java Profilers @Open Source Profilers: 开源Java Profilers

On the other hand, if you want to do this yourself (During runtime) you can write your own custom profiler: 另一方面,如果要自己执行此操作(在运行时),则可以编写自己的自定义探查器:

How to write a profiler? 如何编写分析器?

You might also find this one useful (Although not exactly what you want): 您可能还会发现这很有用(尽管并非您想要的那样):

How can I list all classes loaded in a specific class loader http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html 如何列出特定类加载器中加载的所有类 http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html

If what you are looking is just to examine your code base, there are really good tools out there as well. 如果您正在寻找的只是检查您的代码库,那么那里也确实有很好的工具。 @see http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis @请参阅http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis

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

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