[英]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: 一些说明:
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.