1

How can I force eclipse mars to rename variable names? When I try, I get

This refactoring cannot be performed correctly due to syntax errors in the compilation unit.

The dialog only offers "Cancel".

It was possible to do this in older versions of eclipse, and I used the feature extensively, for example after copy&paste of code snippets found on the net.

Note this is not a duplicate of Refactoring variable names in Eclipse .

Edit 3 (summary of what happened):

In the code (shown below) were not only those common errors like missing imports or undeclared variables, but also a missing ";", thus a true syntax error. This, at first hidden among several other compiling issues, caused eclipse to refuse the refactoring.

As it turned out, this is not a special feature of mars but also of older versions of eclipse.

Edit: here comes my example code. It is mainly based on the examples from tutorialspoint for mongodb but very probably doesn't have anything to do with mongo.

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoDatabase;

public class MongoDBJDBC2 {

  private static String myUserName;
  private static String myPassword;
  private static String myHost = "localhost";
  private static String myDatabaseName = "mydb";
  private static MongoDatabase db;

  public MongoDBJDBC2() {
    initDb();
    // TODO Auto-generated constructor stub
  }

  public static void main(String args[]) {
    MongoDBJDBC2 mo = new MongoDBJDBC2();
  }

  private static void initDb() {
    MongoClientURI uri = new MongoClientURI(
        "mongodb://" + myUserName + ":" + myPassword + "@" + myHost + "/?authSource=db1");
    try (MongoClient mongoClient = new MongoClient(uri);) {

      db = mongoClient.getDatabase(myDatabaseName);
      System.out.println("Connect to database successfully");
      //  boolean auth = db.authenticate(myUserName, myPassword);

    } catch (Exception e) {
      System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
  }

  public static void main4( String args[] ) {

    try{   

       // To connect to mongodb server
       MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

       // Now connect to your databases
       DB db = mongoClient.getDB( "test" );
       System.out.println("Connect to database successfully");

       boolean auth = db.authenticate(myUserName, myPassword);
       System.out.println("Authentication: "+auth);   

       DBCollection coll = db.getCollection("mycol");
       System.out.println("Collection mycol selected successfully");

       DBCursor cursor = coll.find();

       while (cursor.hasNext()) { 
          DBObject updateDocument = cursor.next();
          updateDocument.put("likes","200")
          col1.update(updateDocument); 
       }

       System.out.println("Document updated successfully");
       cursor = coll.find();

       int i = 1;

       while (cursor.hasNext()) { 
          System.out.println("Updated Document: "+i); 
          System.out.println(cursor.next()); 
          i++;
       }

    }catch(Exception e){
       System.err.println( e.getClass().getName() + ": " + e.getMessage() );
    }
 }

}

I try to rename db to myDb in

private static MongoDatabase db;

Previously I used eclipse Helios and never encountered this kind of "feature".

Edit2: I have located the fatal error. In method "main4" a semicolon is missing after

updateDocument.put("likes", "200")

Still don't understand why this upsets eclipse so much that it refuses to refactor, and I still would like to know if there is a way to force refactoring despite of errors.

Community
  • 1
  • 1
Gyro Gearloose
  • 1,056
  • 1
  • 9
  • 26
  • Solve the compilation error and try again. – Marcinek Nov 22 '15 at 14:21
  • 1
    The variable to replace was static. When I removed the static modifier and tried again, the "... refactoring cannot..." was reduced to a warning and next to the "Cancel" button "Continue" appeared. So I guess there are two levels of errors, one that allow continuation, and the other s that are fatal. – Gyro Gearloose Nov 22 '15 at 14:25
  • Can you confirm which older version it was possible with, and if possible a [MCVE] that worked in the older version and not the newer version. If you can put that together, then you have a perfect bug report for Eclipse and you can submit it to make Eclipse better at https://bugs.eclipse.org/bugs/ – Jonah Graham Nov 22 '15 at 14:25
  • @JonahGraham will have a look and reduce my example, this will take some time. – Gyro Gearloose Nov 22 '15 at 14:27
  • @GyroGearloose The Eclipse community would be very grateful indeed if you file such a bug report, this sounds like one of the cases where it may have regressed with no one noticing. – Jonah Graham Nov 22 '15 at 14:32
  • PS I tried a few examples in Kepler and Mars and got the same behaviour in both. For syntax errors I had no continue in either, for semantic errors I did have continue in both. – Jonah Graham Nov 22 '15 at 14:33
  • @JonahGraham You are right, with a missing semicolon even Helios has this feature. Can't remember when I last had an issue with a true syntax error, so I put the blame on mars. – Gyro Gearloose Nov 22 '15 at 15:02

2 Answers2

1

Compilers issue two kinds of errors: syntax errors and all other kinds of errors, like "type mismatch" and "symbol not found". Eclipse complains about a syntax error. Are you sure that in previous occasions when Eclipse agreed to refactor your code despite the fact that it contained errors, it was syntax errors that your code contained? You see, there is a big difference.

Refactoring symbol names in java is far more involved than a simple text search and replace, the structure of your code has to be taken into account.

But in the case of a syntax error, the compiler has given up parsing your file, so it does not know the structure of your code: it does not know which tokens are variables, which tokens are types, which tokens are methods, etc. so it really cannot do the refactoring that you want.

So, if you must really proceed with your refactoring despite having syntax errors, then I am afraid that text search and replace is the way to go for you in this particular case.

But fixing the syntax errors before attempting to refactor would be the most prudent thing to do.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • I think the majority of this answer is good, but the last paragraph it a bit extreme. It is not uncommon to paste a bit of code that causes some errors and then refactor. In fact, Eclipse does not even warn you perform a rename on *Duplicate local variable* error, but which occurrences it renames may not be the ones you intended. – Jonah Graham Nov 22 '15 at 14:37
  • 1
    @JonahGraham you are right. I have even been taking advantage of this, precisely when pasting code. I will change that last paragraph. – Mike Nakis Nov 22 '15 at 14:43
  • @MikeNakis Thank you for pointing out the effect of different kinds of error to eclipse. What looks a bit crazy to me is that eclipse not only marks the error but also notes that a ";" is missing but still gets fatally disturbed. – Gyro Gearloose Nov 22 '15 at 15:23
  • Yes, syntax errors tend to do this. Semicolons in particular are a very sensitive subject, because parsers rely heavily on semicolons in order to divide the code into statements, and then try to make sense out of the statements. So, quite often, something wrong with the syntax of a statement will be reported as a problem with a semicolon. And java is not nearly as weird in this regard as other languages are. Take C++ for example: quite often errors given by C++ compilers do not point anywhere near the location where the programmer actually made the mistake. – Mike Nakis Nov 22 '15 at 15:38
  • (When it comes to checking syntactical correctness, java is a perfect dream, C/C++ is standard and the real horror is perl ) – Gyro Gearloose Nov 22 '15 at 17:58
0

This happens when there is compilation issue in your code. Fix the compilation issue than you can refactor your code.Yes this feature is recently introduced in newer version of eclipse.

Naruto
  • 4,221
  • 1
  • 21
  • 32