代码之家  ›  专栏  ›  技术社区  ›  ab2000

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

  •  2
  • ab2000  · 技术社区  · 8 年前

    例如:

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

    https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jdeps.html http://depfind.sourceforge.net/

    有没有一种方法可以在没有大量定制的情况下从任何工具中提取这些信息?

    1 回复  |  直到 8 年前
        1
  •  1
  •   James from CppDepend Team    8 年前

    您可以使用 JArchitect 满足您的需求。 右键单击UI中的任意方法,然后选择菜单: 选择方法…>。。。(直接或间接)利用我的人 导致如下代码查询:

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

    问题是,这种查询提供了间接用法,但不查找发生的调用 (或在基类中声明的重写方法)。

    // 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]  }
    

    这个问题的两个关键点是:

    • 调用属性IMethod。顾名思义,覆盖了数据库。对于方法 这将返回基类或接口中声明的所有方法的可枚举性,由重写 M .