Java 静态分析:入门

发布于 2025-01-06 18:12:28 字数 435 浏览 3 评论 0原文

嗨,我想开始尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〃安静 2025-01-13 18:12:28

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

¢蛋碎的人ぎ生 2025-01-13 18:12:28

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

巾帼英雄 2025-01-13 18:12:28

Javalib / Sawja 是一个用于在 Caml

它提供了静态分析所需的许多构建块,包括用于以下目的的原语:

  • 解析 Java 字节码
  • 创建和操作中间表示
  • 操作单个指令、方法、类和整个程序
  • 处理类层次结构和控制流算法

文档非常好,如果我正确回忆一下,该发行版包含几个帮助您入门的示例。

鉴于这一切,实现您所描述的源代码指标应该是快速而简单的。例如,下面是一个简短的、未经测试的代码片段,旨在计算给定类中的方法数量:

open Sawja_pack
open Javalib_pack

let size methodmap =
  JBasics.MethodMap.fold (fun _ _ count -> count+1) methodmap 0

let main classname classpath =
  let interface_or_class  = Javalib.get_class classpath (JBasics.make_cn classname) in
  let all_concrete_methods= Javalib.get_concrete_methods interface_or_class in
  print_int (size all_concrete_methods);
  exit 0

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:

  • parse Java bytecode
  • create and manipulate intermediate representations
  • manipulate individual instructions, methods, classes, and whole programs
  • handle class hierarchies and control flow algorithms

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:

open Sawja_pack
open Javalib_pack

let size methodmap =
  JBasics.MethodMap.fold (fun _ _ count -> count+1) methodmap 0

let main classname classpath =
  let interface_or_class  = Javalib.get_class classpath (JBasics.make_cn classname) in
  let all_concrete_methods= Javalib.get_concrete_methods interface_or_class in
  print_int (size all_concrete_methods);
  exit 0
猥︴琐丶欲为 2025-01-13 18:12:28

如果您对代码测试覆盖率的静态分析感兴趣,您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文