IDEA-151861 ExtractMethodObject should not move static methods into created inner...
authorEgor.Ushakov <egor.ushakov@jetbrains.com>
Fri, 26 Feb 2016 17:59:30 +0000 (20:59 +0300)
committerEgor.Ushakov <egor.ushakov@jetbrains.com>
Fri, 26 Feb 2016 18:00:50 +0000 (21:00 +0300)
java/java-impl/src/com/intellij/refactoring/extractMethodObject/ExtractMethodObjectProcessor.java
java/java-tests/testData/refactoring/extractMethodObject/WithPrivateStaticMethodUsed2.java [new file with mode: 0644]
java/java-tests/testData/refactoring/extractMethodObject/WithPrivateStaticMethodUsed2.java.after [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodObjectTest.java

index d13a4c7d368520b1fb63f382be90820845f9197f..c0ced7f5d7e39a0f1d48b9cbea97987da4bb1552 100644 (file)
@@ -131,8 +131,8 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
 
 
       for (PsiMethod usedMethod : usedMethods) {
-        PsiModifierList modifiers = usedMethod.getModifierList();
-        if (modifiers.hasModifierProperty(PsiModifier.PRIVATE) && !modifiers.hasModifierProperty(PsiModifier.STATIC)) {
+        if (usedMethod.hasModifierProperty(PsiModifier.PRIVATE) &&
+            (!usedMethod.hasModifierProperty(PsiModifier.STATIC) || myExtractProcessor.isStatic())) {
           PsiMethod toMove = usedMethod;
           for (PsiReference reference : ReferencesSearch.search(usedMethod)) {
             if (!PsiTreeUtil.isAncestor(getMethod(), reference.getElement(), false)) {
diff --git a/java/java-tests/testData/refactoring/extractMethodObject/WithPrivateStaticMethodUsed2.java b/java/java-tests/testData/refactoring/extractMethodObject/WithPrivateStaticMethodUsed2.java
new file mode 100644 (file)
index 0000000..73add1a
--- /dev/null
@@ -0,0 +1,9 @@
+public class XXX {
+  static void f<caret>oo() {
+    int i = 0 ;
+    bar(i);
+    System.out.println(i);
+  }
+
+  private static void bar(int i){}
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/extractMethodObject/WithPrivateStaticMethodUsed2.java.after b/java/java-tests/testData/refactoring/extractMethodObject/WithPrivateStaticMethodUsed2.java.after
new file mode 100644 (file)
index 0000000..14d9a16
--- /dev/null
@@ -0,0 +1,15 @@
+public class XXX {
+  static void foo() {
+      InnerClass.invoke();
+  }
+
+    private static class InnerClass {
+        private static void invoke() {
+            int i = 0 ;
+            bar(i);
+            System.out.println(i);
+        }
+
+        private static void bar(int i){}
+    }
+}
\ No newline at end of file
index 02b8eecfc1b34d0e2f5afb0f66889ecd68c7671e..fcfd04b125ba85f361bb9fb91178ec67ad712bec 100644 (file)
@@ -137,6 +137,10 @@ public class ExtractMethodObjectTest extends LightRefactoringTestCase {
     doTest();
   }
 
+  public void testWithPrivateStaticMethodUsed2() throws Exception {
+    doTest();
+  }
+
   public void testWithPrivateMethodWhichCantBeMoved() throws Exception {
     doTest();
   }