move member: restore qualifier in switch labels for non-enum constants (IDEA-147539)
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 6 Nov 2015 14:09:47 +0000 (15:09 +0100)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Fri, 6 Nov 2015 15:21:29 +0000 (16:21 +0100)
java/java-impl/src/com/intellij/refactoring/move/moveMembers/MoveJavaMemberHandler.java
java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/after/A.java [new file with mode: 0644]
java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/after/B.java [new file with mode: 0644]
java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/before/A.java [new file with mode: 0644]
java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/before/B.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/MoveMembersTest.java

index 59dbd1ab271f43059871fa480e05c0d02c746ced..742f955d6dbfcbfbad534b954e9556a53302a70c 100644 (file)
@@ -179,14 +179,15 @@ public class MoveJavaMemberHandler implements MoveMemberHandler {
           changeQualifier(refExpr, usage.qualifierClass, usage.member);
         }
         else {
-          final PsiReferenceParameterList parameterList = refExpr.getParameterList();
-          if (parameterList != null && parameterList.getTypeArguments().length == 0 && !(refExpr instanceof PsiMethodReferenceExpression)){
-            refExpr.setQualifierExpression(null);
-          } else {
-            final Project project = element.getProject();
-            final PsiClass targetClass =
-              JavaPsiFacade.getInstance(project).findClass(options.getTargetClassName(), GlobalSearchScope.projectScope(project));
-            if (targetClass != null) {
+          final Project project = element.getProject();
+          final PsiClass targetClass =
+            JavaPsiFacade.getInstance(project).findClass(options.getTargetClassName(), GlobalSearchScope.projectScope(project));
+          if (targetClass != null) {
+            final PsiReferenceParameterList parameterList = refExpr.getParameterList();
+            if ((targetClass.isEnum() || PsiTreeUtil.isAncestor(targetClass, element, true)) && parameterList != null && parameterList.getTypeArguments().length == 0 && !(refExpr instanceof PsiMethodReferenceExpression)) {
+              refExpr.setQualifierExpression(null);
+            }
+            else {
               changeQualifier(refExpr, targetClass, usage.member);
             }
           }
diff --git a/java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/after/A.java b/java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/after/A.java
new file mode 100644 (file)
index 0000000..8a58812
--- /dev/null
@@ -0,0 +1,2 @@
+public interface A {
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/after/B.java b/java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/after/B.java
new file mode 100644 (file)
index 0000000..cc08541
--- /dev/null
@@ -0,0 +1,13 @@
+public class B {
+
+    public static final String FOO = "FOO";
+}
+
+class U {
+  public void example(String foo) {
+    switch (foo) {
+      case B.FOO:
+        break;
+    }
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/before/A.java b/java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/before/A.java
new file mode 100644 (file)
index 0000000..0451878
--- /dev/null
@@ -0,0 +1,3 @@
+public interface A {
+  public static final String FOO = "FOO";
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/before/B.java b/java/java-tests/testData/refactoring/moveMembers/stringConstantInSwitchLabelExpression/before/B.java
new file mode 100644 (file)
index 0000000..309cd8c
--- /dev/null
@@ -0,0 +1,12 @@
+public class B {
+  
+}
+
+class U {
+  public void example(String foo) {
+    switch (foo) {
+      case A.FOO:
+        break;
+    }
+  }
+}
\ No newline at end of file
index e1e66c483fc47d9d78872f043f0c818fa2bf0dfb..79435401350a2299391ecd4d3a3d5836d81500fd 100644 (file)
@@ -18,7 +18,6 @@ package com.intellij.refactoring;
 import com.intellij.JavaTestUtil;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.roots.LanguageLevelProjectExtension;
-import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.pom.java.LanguageLevel;
 import com.intellij.psi.PsiClass;
 import com.intellij.psi.PsiElement;
@@ -174,7 +173,11 @@ public class MoveMembersTest extends MultiFileTestCase {
   public void testEscalateVisibility1() throws Exception {
     doTest("A", "B", true, VisibilityUtil.ESCALATE_VISIBILITY, 0);
   }
-  
+
+  public void testStringConstantInSwitchLabelExpression() throws Exception {
+    doTest("A", "B", true, VisibilityUtil.ESCALATE_VISIBILITY, 0);
+  }
+
   public void testMultipleWithDependencies() throws Exception {
     doTest("A", "B", true, VisibilityUtil.ESCALATE_VISIBILITY, 0, 1);
   }