extract method: shorten qualified names for new return type (IDEA-155518)
authorAnna.Kozlova <anna.kozlova@jetbrains.com>
Wed, 4 May 2016 10:53:35 +0000 (12:53 +0200)
committerAnna.Kozlova <anna.kozlova@jetbrains.com>
Wed, 4 May 2016 11:47:03 +0000 (13:47 +0200)
java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java
java/java-tests/testData/refactoring/extractMethod/ShortenClassRefsInNewReturnType.java [new file with mode: 0644]
java/java-tests/testData/refactoring/extractMethod/ShortenClassRefsInNewReturnType_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java

index 45fd55ff4ac0d1ac927e706113974498e64926a9..fbeb184239ec1d4d7a80aa490a15eae16f59afba 100644 (file)
@@ -1140,7 +1140,8 @@ public class ExtractMethodProcessor implements MatchProvider {
       }
     }
     PsiDeclarationStatement statement = myElementFactory.createVariableDeclarationStatement(name, type, myMethodCall);
-    statement = (PsiDeclarationStatement)addToMethodCallLocation(statement);
+    statement =
+      (PsiDeclarationStatement)JavaCodeStyleManager.getInstance(myProject).shortenClassReferences(addToMethodCallLocation(statement));
     PsiVariable var = (PsiVariable)statement.getDeclaredElements()[0];
     myMethodCall = (PsiMethodCallExpression)var.getInitializer();
     if (myOutputVariable != null) {
diff --git a/java/java-tests/testData/refactoring/extractMethod/ShortenClassRefsInNewReturnType.java b/java/java-tests/testData/refactoring/extractMethod/ShortenClassRefsInNewReturnType.java
new file mode 100644 (file)
index 0000000..80fc8ee
--- /dev/null
@@ -0,0 +1,8 @@
+import java.util.ArrayList;
+
+class Test {
+  Object foo() {
+    Object list = <selection>new ArrayList<String>();</selection>
+    return list;
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/extractMethod/ShortenClassRefsInNewReturnType_after.java b/java/java-tests/testData/refactoring/extractMethod/ShortenClassRefsInNewReturnType_after.java
new file mode 100644 (file)
index 0000000..9dd120a
--- /dev/null
@@ -0,0 +1,16 @@
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+class Test {
+  Object foo() {
+    Object list = newMethod();
+    return list;
+  }
+
+    @NotNull
+    private Collection newMethod() {
+        return new ArrayList<String>();
+    }
+}
\ No newline at end of file
index 35946793c3a8694fc114065b1e19578cbf04492c..eb8a1492e3665ccab9d1f09ffaa0e2a936b629f1 100644 (file)
@@ -744,6 +744,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
     doTestReturnTypeChanged(PsiType.INT);
   }
 
+  public void testShortenClassRefsInNewReturnType() throws Exception {
+    doTestReturnTypeChanged(PsiType.getTypeByName(CommonClassNames.JAVA_UTIL_COLLECTION, getProject(), GlobalSearchScope.allScope(getProject())));
+  }
+
   public void testPassFieldAsParameterAndMakeStatic() throws Exception {
     doTestPassFieldsAsParams();
   }