1

I want to know how a particular variable in Java class is used. Are there any static code analysis tools which help me trace out the variable.

For example:

Class A {
     int trackMe;
     function usedHere(trackMe);
     B bobject = new B(trackMe); 
     ...

    }

Class B {
      B (int var) {
         copyOfTrackMe = var;
      }
    }

In the above example if I want to track the variable "trackMe". I should be notified by the tool that trackMe is passed to function usedHere and also to constructor of B where a copy is made.

Any inputs are appreciated.

******* EDIT *******
I do not want to run the Code or alter the code in order to achieve this. So I guess none of the IDE debuggers won't get the job done.

Kara
  • 6,115
  • 16
  • 50
  • 57
Adi GuN
  • 1,244
  • 3
  • 16
  • 38

2 Answers2

0

Most IDEs can do this via search. For example, in eclipse, highlighting the field trackMe and then pressing control+H will bring up the Java search dialog. Clicking on "search" will then show all the places in your workspace where that field is directly referenced.

dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • Actually, what that will do is search for that string. Clicking anywhere in a variable and preseing control-shift-G will show all the places where that variable is referenced, and leave out any places where it merely appears in a comment, or the string appears within some other variable. – arcy Oct 09 '13 at 00:44
  • @rcook Actually that is the same thing as the control+H "Java search" tab. which is what I was referring too. – dkatzel Oct 09 '13 at 00:52
  • Consider adding a screenshot. – Thorbjørn Ravn Andersen Oct 09 '13 at 00:59
  • Actually what you said was right. The Java search is something similar to what I want. Thank you :) – Adi GuN Oct 09 '13 at 22:14
0

maybe i'm not understanding you correctly, but this sounds strange. perhaps if you say why you want to do, it could be clearer. However, if you haven't tried using the standard debugger found in any decent IDE, you should try that and see if it that's what you want. You can watch a variable, and see how it's content's are changing.

However, if this is something you want to do in code, you can try wrapping the variable in an object and writing a special getter method which would output some information whenever it is called such as:

class MyVarWrapper {
  private int myVar;
  public void MyVarWrapper(int n) {
    myVar = n;
  }

  public int getMyVar(String info) {
    System.out.println(info);
    return myVar;
  }
}

May look a bit convoluted, but info is limited after all :)

SamAko
  • 3,485
  • 7
  • 41
  • 77
  • I don't want to run the code or alter the code. I believe I need to run the code in order to try what you are saying. – Adi GuN Oct 09 '13 at 22:07