如何在调用图中找到实例变量对象的所有用法?

时间:2013-08-01 04:10:04

标签: java parsing callstack

来自现有的代码库。 A类的实例变量A.toBeCalled为“ToBeCalled”类型。 如何找到它的所有用法/参考A.toBeCall来自代码库的整个调用图? e.g。

  • B类调用A.getToBeCalled()并存储在其本地var中,然后通过调用其方法来使用它。
  • B类调用A.getToBeCalled()并存储在C类的实例var中,然后D类使用C类的实例var。

要求:

  • 调用A.toBeCalled对象的方法在B类,C类,D类中完成... e.t.c
  • 上面的被调用者图

代码示例

Class A {
   ToBeCalled toBeCalled = new ToBeCalled();
   getToBecalled() { return toBeCalled;}
}

Class B {      
  main() {
    A a = new A();
    ToBeCalled toBeCalledInB = a.getToBecalled();
    toBeCalledInB.doXXX();
    C c = new C();
    c.setToBeCalled(toBeCalledInB);
    D d = new D();
    d.start(c);
  }
}

Class C {
  ToBeCalled toBeCalledInC;
  setToBeCalled(ToBeCalled theToBeCalled){
    toBeCalledInC = theToBeCalled;
  }
  getToBeCalled() {
    return toBeCalledInC;
  }
}

Class D {
  start(C c) {
    ToBeCalled toBeCalledInD = c.getToBeCalled();
    toBeCalledInD.doYYY();
  }
}

调用A.toBeCalled的列表方法

  • ToBeCalled.doXXX()
  • ToBeCalled.doYYY()

我调查了几个工具作为起点,然而,它们有点粗糙

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,那你就是在谈论interfaces。 假设你有几个方法接口MyItf

public interface MyItf {
public ToBeCalled getToBeCalled();
public void doXXX();
public void doYYY();
}

使用interface ToBeCalled类必须覆盖所有方法。

public class ToBeCalled implements MyItf{

@Override
public ToBeCalled getToBeCalled() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void doXXX() {
    // TODO Auto-generated method stub
}

@Override
public void doYYY() {
    // TODO Auto-generated method stub
    }
}

其他课程:

public class ToBeCalled2 implements MyItf{

@Override
public ToBeCalled getToBeCalled() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void doXXX() {
    // TODO Auto-generated method stub
}

@Override
public void doYYY() {
    // TODO Auto-generated method stub
    }
}

最后:

public class Launcher {


public static void main(String[] args) {
    MyItf my = new ToBeCalled();        
    my.getToBeCalled();     
    my.doXXX();     
    my.doYYY();

            my = new ToBeCalled2();     
    my.getToBeCalled();     
    my.doXXX();     
    my.doYYY();
}
}