Search code examples
javaeclipserefactoringeclipse-mars

How to force renaming of variable names in eclipse mars


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.


Solution

  • 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.