-1

I'm trying to make a program that will read in a class file and if you give it a variable for example you give it "i" in the following example:

public class Example {
  public static void main( String[] args) {

    int i = 1;
    int j = 5;
    int k = 2; 

    i = i + k;

    System.out.println(i);
  }
}

Then my program will return

 int i = 1;
 int k = 2; 

 i = i + k;
 System.out.println(i);

Since these are variables that affect i.

I'm not sure how to do this. So far I've tried using javaparser which takes in the file and finds all the VariableDeclarationExpr using a visitor pattern. However, this won't print out the bottom two cases in the code above.

Can anyone give me any hints to how to find them?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Steven
  • 93
  • 1
  • 11

1 Answers1

0

The VariableDeclarationExpr represents only declarations (like in your case int i = 1;). But the other two statements in your code are not declarations. They are assignments (probably AssignmentExpr) and a method call (probably MethodCallExpr). So I would first think about where the variable i can appear and then cover all the cases individually. I hope that helps

BluesSolo
  • 608
  • 9
  • 28