2

我正在重构一些 Java 代码,通过将一些静态方法调用更改为非静态调用来更加解耦,例如:

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

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

我的问题是,在大型项目中,很难找到进行静态方法调用的位置。有没有一种简单的方法可以从命令行或在 Eclipse 中执行此操作?

这样的工具需要让我忽略诸如此类的“良性”静态方法调用(要么首先不找到它们,要么允许它们从搜索结果中轻松删除):

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

一些澄清:

  • 我对查找通过反射进行的方法调用不感兴趣
  • 我不知道这个项目中目前存在哪些静态方法,所以它不像使用 Eclipse 的“Open Call Hierarchy”命令(Ctrl-Alt-H)搜索调用者那么简单,尽管搜索非私有的简单方法静态方法会让我使用这种方法
  • 我也有兴趣找到对位于我的项目之外的静态方法的调用,例如 javax.mail.Transport#send
  • 我正在寻找免费(如啤酒)解决方案
4

6 回答 6

3

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.

于 2009-10-06T01:48:15.877 回答
1

我会使用grep(-R在 Linux 上) 来搜索初始 caps-dot-camel case-open (我使用它不足以为您提供完整的命令行)。然后grep -v处理掉一些垃圾。

好吧,我真正要做的就是逐步重构。改变一个方法,看看什么中断(如果没有中断,删除代码)。

Theoretically you could search through the class files looking for invokestatic. The FindBugs infrastructure would probably help out here (there may be better starting points).

于 2009-10-06T01:47:04.667 回答
1

Some IDEs provide support for refactoring. 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. 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).

于 2009-10-06T01:49:34.740 回答
1

We have a product called nWire for Java which might just help. nWire analyzes your code and builds a database of your code components and associations. 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 uses the H2 database engine which is open-source and free.

I can assist in accessing the database. Drop me a line to support [at] nwiresoftware.com.

于 2009-10-06T07:05:21.513 回答
1

I've written a small Java program that uses the excellent ASM library. It lets you exclude packages like java.lang, and produces output that looks like this:

+ 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.

于 2009-10-08T03:45:54.570 回答
0

A possible solution could be a custom CheckSyle or PMD or ... warning. Currently I have the same challenge and trying it with CheckStyle. It seems to be right easy to write such an extention.

于 2016-05-15T12:08:42.993 回答