简体   繁体   English

如何在大型Java项目中查找静态方法调用?

[英]How to find static method calls in large Java project?

I'm refactoring some Java code to be more decoupled by changing some static method calls to non-static calls, for example: 我通过将一些静态方法调用更改为非静态调用来重构一些Java代码,以使其更加分离,例如:

// Before:
DAO.doSomething(dataSource, arg1, ..., argN)

// After:
dao.doSomething(arg1, ..., argN)

My problem is that in a large project, it can be hard to find where static method calls are being made. 我的问题是,在大型项目中,很难找到在哪里进行静态方法调用。 Is there an easy way to do this, either from the command line or in Eclipse? 从命令行还是在Eclipse中,有没有简便的方法可以做到这一点?

Such a tool would need to let me ignore "benign" static method calls such as these (either by not finding them in the first place, or by allowing them to be easily deleted from the search results): 这样的工具将需要让我忽略诸如此类的“良性”静态方法调用(要么不首先查找它们,要么使它们易于从搜索结果中删除):

String.valueOf(...)
Integer.parseInt(...)
MyClass.someBenignStaticMethod(...)

Some clarifications: 一些说明:

  • I'm not interested in finding method calls made via reflection 我对查找通过反射进行的方法调用不感兴趣
  • I don't know what static methods currently exist in this project, so it's not as simple as searching for their callers using Eclipse's "Open Call Hierarchy" command (Ctrl-Alt-H), although an easy way to search for non-private static methods would let me use this approach 我不知道该项目中目前存在哪些静态方法,因此它不是使用Eclipse的“ Open Call Hierarchy”命令(Ctrl-Alt-H)搜索其调用者的简单方法,尽管这是一种搜索非私有方法的简便方法静态方法会让我使用这种方法
  • I'm also interested in finding calls to static methods located outside my project, eg javax.mail.Transport#send 我还对查找位于项目外部的静态方法的调用感兴趣,例如javax.mail.Transport#send
  • I'm looking for a free (as in beer) solution 我正在寻找免费的(如啤酒)解决方案

Do you really need to search? 您真的需要搜索吗? Why not comment out the static method calls one by one? 为什么不注释掉静态方法一个接一个地调用呢? When you compile it then it will flush out the references. 编译时,它将刷新出引用。

I'd use grep ( -R on Linux) to search for initial caps-dot-camel case-open (I don't use it enough to give you the full command line). 我将使用grep (在Linux上为-R )来搜索初始caps-dot-camel case-open(我没有足够使用它来提供完整的命令行)。 And then grep -v to get rid of some of the rubbish. 然后使用grep -v除去一些垃圾。

Well, really what I'd do is refactor incrementally. 好吧,实际上我要做的是逐步重构。 Changes a method, and see what breaks (if nothing breaks, delete the code). 更改方法,然后查看中断(如果没有中断,请删除代码)。

Theoretically you could search through the class files looking for invokestatic . 从理论上讲,您可以搜索类文件以查找invokestatic The FindBugs infrastructure would probably help out here (there may be better starting points). FindBugs基础结构可能会在这里有所帮助(可能会有更好的起点)。

Some IDEs provide support for refactoring. 一些IDE提供了对重构的支持。 You can refactor every static method one-by-one. 您可以一对一重构每个静态方法。

In Eclipse, you can view the call hierarchy to see all the callers of such method. 在Eclipse中,您可以查看调用层次结构以查看该方法的所有调用方。 To view the call hierarchy you can select the method name and press Command-Alt-H, or Right-Click on symbol and choose 'Open Call Hierarchy). 要查看呼叫层次,可以选择方法名称,然后按Command-Alt-H或右键单击符号并选择“打开呼叫层次”。

We have a product called nWire for Java which might just help. 我们有一个名为nWire for Java的产品可能会有所帮助。 nWire analyzes your code and builds a database of your code components and associations. nWire将分析您的代码,并建立一个包含代码组件和关联的数据库。 You can see a brief demo on our web site. 您可以在我们的网站上看到一个简短的演示。

We plan to have reporting capabilities added in the future. 我们计划在将来增加报告功能。 In the mean while, if you have some basic experience with databases, you can tap into the nWire repository and, with a simple SQL query, get a list of all your static methods (you can also see the invocations there). 同时,如果您对数据库有一些基本的经验,则可以进入nWire存储库,并通过简单的SQL查询获得所有静态方法的列表(您也可以在此处看到调用)。 nWire uses the H2 database engine which is open-source and free. nWire使用开放源代码和免费的H2数据库引擎。

I can assist in accessing the database. 我可以协助您访问数据库。 Drop me a line to support [at] nwiresoftware.com. 请给我热线电话,以支持nwiresoftware.com。

I've written a small Java program that uses the excellent ASM library. 我编写了一个使用出色的ASM库的小型Java程序。 It lets you exclude packages like java.lang, and produces output that looks like this: 它使您可以排除java.lang之类的包,并生成如下所示的输出:

+ java
  + io
    - File
      # createTempFile(java.lang.String, java.lang.String)
+ javax
  + imageio
    - ImageIO
      # read(java.io.InputStream)
      # write(java.awt.image.RenderedImage, java.lang.String, java.io.File)
  + mail
    - Transport
      # send(javax.mail.Message)
    + internet
      - InternetAddress
        # parse(java.lang.String, boolean)
  + xml
    + parsers
      - DocumentBuilderFactory
        # newInstance()

I'd prefer something that's more easily built into my existing build process, which uses CheckStyle, but this is the best solution I've come up with so far. 我希望使用CheckStyle可以更轻松地将其构建到现有构建过程中,但这是迄今为止我提出的最佳解决方案。

A possible solution could be a custom CheckSyle or PMD or ... warning. 可能的解决方案可以是自定义CheckSyle或PMD或...警告。 Currently I have the same challenge and trying it with CheckStyle. 目前,我遇到了同样的挑战,并尝试使用CheckStyle。 It seems to be right easy to write such an extention. 写这样的扩展似乎很容易。

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

相关问题 如何以编程方式查找项目中的所有Java方法调用和给定参数? - How to find all Java method calls and given parameters in a project programmatically? 如何找到Java项目中使用方法的位置 - How to find where a method is used in Java project 如何对调用不同类的 static 方法的 Java 方法进行单元测试 - How to unit test a Java method which calls a different class's static method 如何抑制和验证私有静态方法调用? - How to suppress and verify private static method calls? 在java类中查找对方法的所有调用 - Find all calls to method in java classes 我如何实现一个泛型类,该类在java中调用指定接口的静态方法 - How do i implement a generic class that calls a static method of specified interface in java 使用方法内部的静态调用设计Java单元测试 - Designing Java unit test with static calls inside method 在Java中限制为最多N次并发调用静态方法 - Limiting to at max N concurrent calls to a static method in Java Java:Self用于同一类中的静态方法调用 - Java: Self for static method calls within the same class 如何在java项目中找到哪个数据类没有覆盖toString()方法 - How to find which data class not overriden toString() method in java project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM