java - 查找 Java 类的所有传递依赖项,包括仅通过其接口(interface)使用的实现类

标签 java dependencies static-code-analysis jdeps

我的目标是列出我项目的公共(public) API 类的所有传递依赖项,并使用它来集中测试工作,以防对这些依赖项进行任何代码更改。

例如:

class MyApi {
    MyDao md;
    public void methodA() {
        //do something with md;
    }
}

interface MyDao { }

class MyDaoImpl implements MyDao { }

因此,如果我知道 MyDaoImpl 已被修改(比如从提交历史记录)并且我知道 MyApi.methodA 使用 MyDaoImpl,那么我的测试应该侧重于检查它。我需要 MyApi.methodA() 的依赖项列表,包括 MyDao 和 MyDaoImpl。

到目前为止,我已经尝试了两种工具 - https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jdeps.htmlhttp://depfind.sourceforge.net/ - 他们很有前途,但似乎并没有完全解决问题。对于这两种工具,似乎如果一个类依赖于一个接口(interface),则没有内置的方法将该接口(interface)的实现作为传递依赖项包含在内。

有没有办法在不进行大量定制的情况下从任何工具中提取这些信息?

最佳答案

您可以使用 JArchitect为您的需要。 右键单击 UI 中任意位置的方法,然后选择菜单:选择方法... > ...正在使用我(直接或间接) 会导致如下代码查询:

from m in Methods 
let depth0 = m.DepthOfIsUsing("myNamespace.MyClass.MyMethod()")
where depth0  >= 0 orderby depth0
select new { m, depth0 }

问题在于此类查询提供了间接用法,但不会查找通过接口(interface)(或在基类中声明的重写方法)发生的调用。

希望可以通过此查询获得您所要求的内容:

// Retrieve the target method by name
let methodTarget = Methods.WithFullName(""myNamespace.MyClass.MyMethod()"").Single()

// Build a ICodeMetric<IMethod,ushort> representing the depth of indirect
// call of the target method.
let indirectCallDepth = 
   methodTarget.ToEnumerable()
   .FillIterative(
       methods => methods.SelectMany(
          m => m.MethodsCallingMe.Union(m.OverriddensBase)))

from m in indirectCallDepth.DefinitionDomain
select new { m, callDepth = indirectCallDepth[m]  }

这个查询的两个基石是:

  • 调用 FillIterative() 以递归选择间接调用。
  • 如其名称所示,调用属性 IMethod.OverriddensBase。对于 M 方法,它返回在基类或接口(interface)中声明的所有方法的可枚举,由 M 覆盖。

关于java - 查找 Java 类的所有传递依赖项,包括仅通过其接口(interface)使用的实现类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45636579/

相关文章:

java - 在Java中解析Java日期格式

maven-2 - 您知道 mvnrepository.com 的 Maven 配置文件吗?

visual-studio-2010 - C# 和 C++ 项目之间的项目依赖关系,构建顺序

android - 使用 Gradle 将应用程序模块依赖项添加到 Android Studio 中的另一个应用程序模块中

c# - Code Contracts 是否未能发现 Nullable<T>.HasValue 和 null 之间的明显关系?

Java2D 和图像创建

java - mac 机器上有不同的 java 版本吗?

python - 检查python程序是否有错误

java - SonarQube Eclipse 插件 3.4 由于连接超时而无法到达 Sonar 服务器

java - 在java中生成序列优惠券代码