如何在eclipse mars中强制重命名变量名

How to force renaming of variable names in eclipse mars

如何强制 eclipse mars 重命名变量名? 当我尝试时,我得到

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

对话框只提供"Cancel"。

在旧版本的 eclipse 中可以执行此操作,我广泛使用了该功能,例如在复制和粘贴网上找到的代码片段之后。

请注意,这不是 的副本。

编辑 3(发生的事情的总结):

在代码中(如下所示)不仅存在缺少导入或未声明变量等常见错误,而且还缺少“;”,因此是 真正的语法错误。这最初隐藏在其他几个编译问题中,导致 eclipse 拒绝重构。

事实证明,这不是 mars 的特殊功能,也是旧版本的 eclipse 的特殊功能。

编辑:这是我的示例代码。它主要基于 mongodb tutorialspoint 中的示例,但很可能与 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() );
    }
 }

}

我尝试在

中将 db 重命名为 myDb
private static MongoDatabase db;

以前用过eclipse Helios,没遇到过这种"feature"。

Edit2:我找到了致命错误。在方法 "main4" 中,在

之后缺少分号
updateDocument.put("likes", "200")

仍然不明白为什么这让 eclipse 如此沮丧以至于它拒绝重构,我仍然想知道是否有一种方法可以在出现错误的情况下强制重构。

如果您的代码中存在编译问题,就会发生这种情况。 修复编译问题,然后重构 code.Yes 此功能最近在较新版本的 eclipse 中引入。

编译器会发出两种错误:语法错误和所有其他类型的错误,例如 "type mismatch" 和 "symbol not found"。 Eclipse 抱怨语法错误。您确定在以前 Eclipse 同意重构您的代码(尽管它包含错误)的情况下,您的代码包含 syntax 错误吗?你看,有的区别。

重构 java 中的符号名称比简单的文本搜索和替换要复杂得多,必须考虑代码的结构。

但是在语法错误的情况下,编译器已经放弃解析你的文件,所以它不知道你代码的结构:它不知道哪些标记是变量,哪些标记是类型,哪些标记是方法等所以它真的不能做你想要的重构。

因此,如果尽管有语法错误但您真的必须继续进行重构,那么在这种特殊情况下,恐怕文本搜索和替换是适合您的方式。

但是在尝试重构之前修复语法错误是最谨慎的做法。