move members: show conflict if non-final field is moved to interface
authorAnna Kozlova <Anna.Kozlova@jetbrains.com>
Thu, 17 Sep 2009 17:10:31 +0000 (21:10 +0400)
committerAnna Kozlova <Anna.Kozlova@jetbrains.com>
Thu, 17 Sep 2009 17:10:31 +0000 (21:10 +0400)
java/java-impl/src/com/intellij/refactoring/move/moveMembers/MoveMembersProcessor.java
java/java-tests/testData/refactoring/moveMembers/writableField/after/A.java [new file with mode: 0644]
java/java-tests/testData/refactoring/moveMembers/writableField/after/B.java [new file with mode: 0644]
java/java-tests/testData/refactoring/moveMembers/writableField/before/A.java [new file with mode: 0644]
java/java-tests/testData/refactoring/moveMembers/writableField/before/B.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/MoveMembersTest.java

index cd99907a10e4a51728d8fa9d862bff1488c4bee9..e045d80e93a27009ce7e885fd6a5ae2c910cf462 100644 (file)
@@ -4,6 +4,7 @@
  */
 package com.intellij.refactoring.move.moveMembers;
 
+import com.intellij.codeInsight.highlighting.ReadWriteAccessDetector;
 import com.intellij.lang.LanguageExtension;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
@@ -271,6 +272,16 @@ public class MoveMembersProcessor extends BaseRefactoringProcessor {
               conflicts.put(member, message);
             }
           }
+
+          if (member instanceof PsiField && myTargetClass.isInterface()) {
+            final ReadWriteAccessDetector accessDetector = ReadWriteAccessDetector.findDetector(member);
+            if (accessDetector != null) {
+              final ReadWriteAccessDetector.Access access = accessDetector.getExpressionAccess(element);
+              if (access != ReadWriteAccessDetector.Access.Read) {
+                conflicts.put(element, CommonRefactoringUtil.capitalize(RefactoringUIUtil.getDescription(member, true)) + " has write access but is moved to an interface");
+              }
+            }
+          }
         }
       }
     }
diff --git a/java/java-tests/testData/refactoring/moveMembers/writableField/after/A.java b/java/java-tests/testData/refactoring/moveMembers/writableField/after/A.java
new file mode 100644 (file)
index 0000000..9772fb1
--- /dev/null
@@ -0,0 +1,4 @@
+public interface A {
+
+    String ONE = "";
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/moveMembers/writableField/after/B.java b/java/java-tests/testData/refactoring/moveMembers/writableField/after/B.java
new file mode 100644 (file)
index 0000000..0a70a25
--- /dev/null
@@ -0,0 +1,6 @@
+public class B {
+
+    void foo() {
+    A.ONE = "foo";
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/moveMembers/writableField/before/A.java b/java/java-tests/testData/refactoring/moveMembers/writableField/before/A.java
new file mode 100644 (file)
index 0000000..39abc02
--- /dev/null
@@ -0,0 +1,3 @@
+public interface A {
+    
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/moveMembers/writableField/before/B.java b/java/java-tests/testData/refactoring/moveMembers/writableField/before/B.java
new file mode 100644 (file)
index 0000000..c5933dc
--- /dev/null
@@ -0,0 +1,7 @@
+public class B {
+  public static String ONE = "";
+
+  void foo() {
+    ONE = "foo";
+  }
+}
\ No newline at end of file
index 03caa23eb4271727c455e655f01b8995850db254..12fed4fd23a969c9ed75bdcf3218595ed750b1aa 100644 (file)
@@ -13,6 +13,7 @@ import com.intellij.JavaTestUtil;
 
 import java.util.ArrayList;
 import java.util.LinkedHashSet;
+import java.util.Map;
 
 public class MoveMembersTest extends MultiFileTestCase {
   @Override
@@ -84,6 +85,16 @@ public class MoveMembersTest extends MultiFileTestCase {
     doTest("B", "A", 0);
   }
 
+  public void testWritableField() throws Exception {
+    try {
+      doTest("B", "A", 0);
+      fail("conflict expected");
+    }
+    catch (Exception e) {
+      assertEquals(e.getMessage(), "Found conflicts: Field <b><code>B.ONE</code></b> has write access but is moved to an interface");
+    }
+  }
+
   protected String getTestRoot() {
     return "/refactoring/moveMembers/";
   }
@@ -124,7 +135,15 @@ public class MoveMembersTest extends MultiFileTestCase {
 
     MockMoveMembersOptions options = new MockMoveMembersOptions(targetClass.getQualifiedName(), memberSet);
     options.setMemberVisibility(null);
-    new MoveMembersProcessor(myProject, null, options).run();
+    new MoveMembersProcessor(myProject, null, options){
+      @Override
+      protected boolean showConflicts(Map<PsiElement, String> conflicts) {
+        if (!conflicts.isEmpty()) {
+          throw new RuntimeException("Found conflicts: " + conflicts.values().iterator().next());
+        }
+        return super.showConflicts(conflicts);
+      }
+    }.run();
     FileDocumentManager.getInstance().saveAllDocuments();
   }
 }