Java 静态分析:入门
嗨,我想开始尝试 java 源代码的静态分析。首先,直接解析我的源代码树似乎是最简单的,但是假设有可用的 API。特别是,如果有一个允许这样的代码的 API 那就太好了:
for (MetaClass m : mySourcePackage.getClasses())
{
System.out.println(m.getMethods().size());
...
}
当然,对于任何给定的类,您可以使用反射来完成此操作 - 但我更感兴趣的是从头开始静态分析整个源代码包 -并逐一迭代类(例如,评估测试覆盖率、最大行数等......)。
是否有任何高质量的开源框架可以进行此类元分析(或者,是否可以使用特定的类路径启动 JVM 并在 JVM 内部进行此类分析)?
请记住,我并不特别担心 DI 和反射等问题(至少目前不是)。
Hi I wanted to start playing around with static analysis of java source code. As a start , it seems simplest to directly parse my source tree, however assume that there are API's out there for this. In particular it would be nice to have an API which allowed code such as this :
for (MetaClass m : mySourcePackage.getClasses())
{
System.out.println(m.getMethods().size());
...
}
Of course, for any given class, you can do this using reflection - but I'm more interested in statically analyzing a whole source code package, from scratch - and iterating through classes one by one (for example, to evaluate things like test coverage, maximum lines, etc....).
Are there any high quality open source frameworks for doing such meta-analyses (or, maybe, is it possible to launch the JVM with a certain class path and do such an analysis inside the JVM)?
Please keep in mind that I'm NOT particularly worried about gotchas such as DI and reflection (at least, not at this point).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Findbugs 可以被视为静态分析的框架。您可以实施具有特定目标的新检测器。 “操作方法”教程
此外,这里还有一些涵盖您提到的目标的库:
更多工具/库
Findbugs can be consider as a framework for static analysis. You could implement new detectors that have their specific objectives. "How To" tutorial
Also, here are some libraries that cover the objectives you mention :
More tools/libraries
error prone 是另一个值得关注的好工具,它由 google 使用,误报率非常低,
https ://github.com/google/error-prone
代码结构非常好,
例如您可以看到 这里数组相等检查是如何实现的
error prone is another good tool to look at, it is used by google and has very low ratio of false positive,
https://github.com/google/error-prone
the code is very well structured,
e.g. you can see here how the array equal check is implemented
Javalib / Sawja 是一个用于在 Caml。
它提供了静态分析所需的许多构建块,包括用于以下目的的原语:
文档非常好,如果我正确回忆一下,该发行版包含几个帮助您入门的示例。
鉴于这一切,实现您所描述的源代码指标应该是快速而简单的。例如,下面是一个简短的、未经测试的代码片段,旨在计算给定类中的方法数量:
Javalib / Sawja is an library for writing static analyzers for Java in Caml.
It provides many of the building blocks needed for static analysis, including primitives to:
Documentation is quite good, and if I recall correctly the distribution includes several examples to get you started.
Given all this, implementing the kind of source code metrics that you describe should be quick and easy. For instance, below is a short, untested snippet that aims at counting the number of methods in a given class:
如果您对代码测试覆盖率的静态分析感兴趣,您可以使用 Cobertura - 和 eCobertura Eclipse IDE 插件。
If you are interested in static analysis for test coverage of your code you can use Cobertura - and eCobertura plug-in for Eclipse IDE.