How do I find calls to static methods in a large Java project?

I am refactoring some Java code to be more decoupled, changing some of the 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 is difficult to find where static method calls are made. Is there an easy way to do this, either from the command line or from Eclipse?

A tool like this should allow me to ignore "benign" static method calls like these (either not finding them in the first place, or allowing them to be easily removed from search results):

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

      

Some clarifications:

  • I'm not interested in finding method calls made with reflection
  • I do not know what static methods currently exist in this project, so it is not as easy as finding their callers using the Eclipse "Open Call Hierarchy" command (Ctrl-Alt-H), although this is an easy way to find non-personal static methods will allow me to use this approach
  • I am also interested in finding calls to static methods located outside of my project, for example. javax.mail.Transport # send
  • I'm looking for a free (beer-like) solution.
+2


source to share


6 answers


I have 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 would prefer something that is more easily built into my existing build process that uses CheckStyle, but this is the best solution I have come up with so far.

+1


source


Do you really need to search? Why not comment out the static method calls one by one? When you compile it, it will clear the links.



+3


source


I would use grep

( -R

on Linux) to search for the original open-body cap-point-camel (I don't use it enough to give you a full command line). And then grep -v

to get rid of some rubbish.

Well, really, I would refactor gradually. Modifies the method and sees what breaks (if nothing breaks, remove the code).

In theory, you could search for class files looking for invokestatic

. The FindBugs framework will probably help here (there may be better starting points).

+1


source


Several IDEs provide support for refactoring. You can refactor each static method one by one.

In Eclipse you can view the call hierarchy to see all the callers of such a method. To view the call hierarchy, you can select the method name and press Command-Alt-H, or right-click the symbol and select Open Call Hierarchy.

+1


source


We have a product called nWire for Java that might just help. nWire analyzes your code and creates a database of your code components and associations. You can see a short demo on our website.

We plan to add reporting capabilities in the future. In the meantime, if you have basic database experience, you can go to the nWire repository and use a simple SQL query to get a list of all your static methods (you can also see the calls there). nWire uses the H2 database file, which is open source and free.

I can help with database access. Drop me a line for support [at] nwiresoftware.com.

+1


source


A possible solution could be a custom CheckSyle or PMD or ... a warning. I am currently having the same problem as CheckStyle. It seems like it would be easy to write such an extension.

0


source







All Articles