-1

I have an existing java source code. I am modifying this .java file using java parser (jar name - javaparser-core-2.5.1.jar). I need to add an else if statement block e.g:

    if(condition1){
    //then statement
}else if(condition2) {
    //then statement
} else if(condition3) {
    //then statement
}else {
   //else statement
}

the else if statement with condition3 needs to be added. How can I do that?

1 Answers1

1

You will need to...

  1. parse the file
  2. locate the IfStmt you want. (For example: you can use a visitor to find IfStmt's and check if their elseStmt is not an IfStmt - that should be the last "if" in a chain. But it's hard to say without clear requirements.)
  3. create a new IfStmt with one of its constructors. Set the elseStmt to the one you found in 2.
  4. set the elseStmt of the IfStmt from 2 to the IfStmt from 3.
Danny
  • 161
  • 1
  • 6