rationalize static method calls after pull up
authoranna <Anna.Kozlova@jetbrains.com>
Fri, 2 Sep 2011 17:25:03 +0000 (19:25 +0200)
committeranna <Anna.Kozlova@jetbrains.com>
Fri, 2 Sep 2011 17:25:58 +0000 (19:25 +0200)
java/java-impl/src/com/intellij/refactoring/memberPullUp/PullUpHelper.java
java/java-tests/testData/refactoring/pullUp/UpdateStaticRefs.java [new file with mode: 0644]
java/java-tests/testData/refactoring/pullUp/UpdateStaticRefs_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/PullUpTest.java

index dc1ad6118bde56e5440bfcd3c6294b1d76b8dd40..d110d6e1943abe91fae9d9cc49b16e82475c1af2 100644 (file)
@@ -93,12 +93,30 @@ public class PullUpHelper extends BaseRefactoringProcessor{
 
   @NotNull
   protected UsageInfo[] findUsages() {
-    return new UsageInfo[0];
+    final List<UsageInfo> result = new ArrayList<UsageInfo>();
+    for (MemberInfo memberInfo : myMembersToMove) {
+      final PsiMember member = memberInfo.getMember();
+      if (member.hasModifierProperty(PsiModifier.STATIC)) {
+        for (PsiReference reference : ReferencesSearch.search(member)) {
+          result.add(new UsageInfo(reference));
+        }
+      }
+    }
+    return result.isEmpty() ? UsageInfo.EMPTY_ARRAY : result.toArray(new UsageInfo[result.size()]);
   }
 
   protected void performRefactoring(UsageInfo[] usages) {
     moveMembersToBase();
     moveFieldInitializations();
+    for (UsageInfo usage : usages) {
+      PsiElement element = usage.getElement();
+      if (element instanceof PsiReferenceExpression) {
+        PsiExpression qualifierExpression = ((PsiReferenceExpression)element).getQualifierExpression();
+        if (qualifierExpression instanceof PsiReferenceExpression && ((PsiReferenceExpression)qualifierExpression).resolve() == mySourceClass) {
+          ((PsiReferenceExpression)qualifierExpression).bindToElement(myTargetSuperClass);
+        }
+      }
+    }
     processMethodsDuplicates();
   }
 
diff --git a/java/java-tests/testData/refactoring/pullUp/UpdateStaticRefs.java b/java/java-tests/testData/refactoring/pullUp/UpdateStaticRefs.java
new file mode 100644 (file)
index 0000000..0d15177
--- /dev/null
@@ -0,0 +1,10 @@
+class Foo {}
+class FooImpl extends Foo {
+  public static void foo(){}
+  <caret>
+}
+class U {
+  public static void main(String[] args) {
+    FooImpl.foo();
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/pullUp/UpdateStaticRefs_after.java b/java/java-tests/testData/refactoring/pullUp/UpdateStaticRefs_after.java
new file mode 100644 (file)
index 0000000..a267d99
--- /dev/null
@@ -0,0 +1,11 @@
+class Foo {
+    public static void foo(){}
+}
+class FooImpl extends Foo {
+
+}
+class U {
+  public static void main(String[] args) {
+    Foo.foo();
+  }
+}
\ No newline at end of file
index fb6ec06ec21d6e49c6000bd42fd43cf98b6a92c4..7c637436c796a662e3c130e622b298f5ce49297e 100644 (file)
@@ -35,7 +35,6 @@ public class PullUpTest extends LightCodeInsightTestCase {
     doTest(new RefactoringTestUtil.MemberDescriptor("x", PsiField.class),
            new RefactoringTestUtil.MemberDescriptor("getX", PsiMethod.class),
            new RefactoringTestUtil.MemberDescriptor("setX", PsiMethod.class));
-
   }
 
   public void testPullUpInheritedStaticClasses() throws Exception {
@@ -46,7 +45,7 @@ public class PullUpTest extends LightCodeInsightTestCase {
   public void testPullUpPrivateInnerClassWithPrivateConstructor() throws Exception {
     doTest(new RefactoringTestUtil.MemberDescriptor("C", PsiClass.class));
   }
-  
+
   public void testPullUpAndAbstractize() throws Exception {
     doTest(new RefactoringTestUtil.MemberDescriptor("a", PsiMethod.class),
            new RefactoringTestUtil.MemberDescriptor("b", PsiMethod.class, true));
@@ -102,6 +101,10 @@ public class PullUpTest extends LightCodeInsightTestCase {
     doTest(false, new RefactoringTestUtil.MemberDescriptor("I", PsiClass.class));
   }
 
+  public void testUpdateStaticRefs() throws Exception {
+    doTest(false, new RefactoringTestUtil.MemberDescriptor("foo", PsiMethod.class));
+  }
+
   private void doTest(RefactoringTestUtil.MemberDescriptor... membersToFind) throws Exception {
     doTest(true, membersToFind);
   }
@@ -121,7 +124,7 @@ public class PullUpTest extends LightCodeInsightTestCase {
     }
     MemberInfo[] infos = RefactoringTestUtil.findMembers(sourceClass, membersToFind);
 
-    final int[] countMoved = new int[] {0};
+    final int[] countMoved = new int[]{0};
     final MoveMemberListener listener = new MoveMemberListener() {
       @Override
       public void memberMoved(PsiClass aClass, PsiMember member) {
@@ -143,5 +146,4 @@ public class PullUpTest extends LightCodeInsightTestCase {
   protected String getTestDataPath() {
     return JavaTestUtil.getJavaTestDataPath();
   }
-
 }