diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java index 0cdac338ad2c..3eedefaec09a 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/ExtractMethodProcessor.java @@ -46,6 +46,7 @@ import com.intellij.openapi.wm.WindowManager; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.CodeStyleSettingsManager; +import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.controlFlow.ControlFlowUtil; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.search.LocalSearchScope; @@ -1047,7 +1048,7 @@ public class ExtractMethodProcessor implements MatchProvider { LOG.assertTrue(qualifierExpression != null); qualifierExpression.replace(instanceQualifier); } - return expr; + return (PsiMethodCallExpression)JavaCodeStyleManager.getInstance(myProject).shortenClassReferences(expr); } private void declareNecessaryVariablesInsideBody(PsiCodeBlock body) throws IncorrectOperationException { diff --git a/java/java-tests/testData/refactoring/extractMethod/InvalidReference.java b/java/java-tests/testData/refactoring/extractMethod/InvalidReference.java new file mode 100644 index 000000000000..b2addd97207c --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethod/InvalidReference.java @@ -0,0 +1,7 @@ +import java.io.IOException; + +class Test { + void foo(Exception targetElement) { + PsiPackage aPack = JavaDirectoryService.getInstance().getPackage((IOException)targetElement); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/extractMethod/InvalidReference_after.java b/java/java-tests/testData/refactoring/extractMethod/InvalidReference_after.java new file mode 100644 index 000000000000..ad34b03d549f --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethod/InvalidReference_after.java @@ -0,0 +1,11 @@ +import java.io.IOException; + +class Test { + void foo(Exception targetElement) { + PsiPackage aPack = newMethod((IOException) targetElement); + } + + private PsiPackage newMethod(IOException targetElement) { + return JavaDirectoryService.getInstance().getPackage((IOException)targetElement); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java b/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java index 7303de872cc6..3b6667f2c397 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java @@ -489,6 +489,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase { doTest(); } + public void testInvalidReference() throws Exception { + doTest(); + } + private void doPrepareErrorTest(final String expectedMessage) throws Exception { String expectedError = null; try {