Andrew Swan
Andrew Swan

Reputation: 13627

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:

// 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?

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:

Upvotes: 2

Views: 2854

Answers (6)

iuzuz
iuzuz

Reputation: 97

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.

Upvotes: 0

Andrew Swan
Andrew Swan

Reputation: 13627

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.

Upvotes: 1

zvikico
zvikico

Reputation: 9825

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.

Upvotes: 1

notnoop
notnoop

Reputation: 59299

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

Upvotes: 1

Brian Lyttle
Brian Lyttle

Reputation: 14579

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.

Upvotes: 3

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147124

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). And then grep -v to get rid of some of the rubbish.

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. The FindBugs infrastructure would probably help out here (there may be better starting points).

Upvotes: 1

Related Questions