diff --git a/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java b/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java index bcde92bdaffe..414ab6c99855 100644 --- a/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java @@ -291,10 +291,13 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { } else { List refExprList = new ArrayList(); + final List imports2Delete = new ArrayList(); for (final UsageInfo usage : usages) { final PsiElement element = usage.getElement(); if (element instanceof PsiReferenceExpression) { refExprList.add((PsiReferenceExpression)element); + } else if (element instanceof PsiImportStaticReferenceElement) { + imports2Delete.add(PsiTreeUtil.getParentOfType(element, PsiImportStaticStatement.class)); } } PsiReferenceExpression[] refs = refExprList.toArray(new PsiReferenceExpression[refExprList.size()]); @@ -302,6 +305,11 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { for (PsiReferenceExpression ref : refs) { inlineMethodCall(ref); } + for (PsiElement psiElement : imports2Delete) { + if (psiElement != null && psiElement.isValid()) { + psiElement.delete(); + } + } myMethod.delete(); } } diff --git a/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/after/Bar.java b/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/after/Bar.java new file mode 100644 index 000000000000..2f1dfde98c09 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/after/Bar.java @@ -0,0 +1,4 @@ +public class Bar { + void bar() { + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/after/Foo.java b/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/after/Foo.java new file mode 100644 index 000000000000..0a2b05268bb6 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/after/Foo.java @@ -0,0 +1,2 @@ +public class Foo { +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/before/Bar.java b/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/before/Bar.java new file mode 100644 index 000000000000..0d111602e175 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/before/Bar.java @@ -0,0 +1,6 @@ +import static Foo.foo; +public class Bar { + void bar() { + foo(); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/before/Foo.java b/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/before/Foo.java new file mode 100644 index 000000000000..38f408a46ec2 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/multifile/removeStaticImports/before/Foo.java @@ -0,0 +1,3 @@ +public class Foo { + public static void foo(){} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodMultifileTest.java b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodMultifileTest.java new file mode 100644 index 000000000000..e157e0756a9a --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodMultifileTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2000-2010 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.refactoring.inline; + +import com.intellij.JavaTestUtil; +import com.intellij.codeInsight.CodeInsightTestCase; +import com.intellij.openapi.projectRoots.impl.JavaSdkImpl; +import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.impl.source.PostprocessReformattingAspect; +import com.intellij.psi.search.ProjectScope; +import com.intellij.refactoring.MockInlineMethodOptions; +import com.intellij.refactoring.util.InlineUtil; +import com.intellij.testFramework.IdeaTestUtil; +import com.intellij.testFramework.PsiTestUtil; + +import java.io.File; + + +public class InlineMethodMultifileTest extends CodeInsightTestCase { + + private String getRoot() { + return JavaTestUtil.getJavaTestDataPath() + "/refactoring/inlineMethod/multifile/" + getTestName(true); + } + + public void testRemoveStaticImports() throws Exception { + doTest("Foo", "foo"); + } + + private void doTest(String className, String methodName) throws Exception { + String rootBefore = getRoot() + "/before"; + PsiTestUtil.removeAllRoots(myModule, JavaSdkImpl.getMockJdk17()); + final VirtualFile rootDir = PsiTestUtil.createTestProjectStructure(myProject, myModule, rootBefore, myFilesToDelete); + PsiClass aClass = myJavaFacade.findClass(className, ProjectScope.getAllScope(myProject)); + assertTrue(aClass != null); + PsiElement element = aClass.findMethodsByName(methodName, false)[0]; + assertTrue(element instanceof PsiMethod); + PsiMethod method = (PsiMethod)element; + final boolean condition = InlineMethodProcessor.checkBadReturns(method) && !InlineUtil.allUsagesAreTailCalls(method); + assertFalse("Bad returns found", condition); + + InlineOptions options = new MockInlineMethodOptions(); + final InlineMethodProcessor processor = new InlineMethodProcessor(getProject(), method, null, myEditor, options.isInlineThisOnly()); + processor.run(); + + String rootAfter = getRoot() + "/after"; + VirtualFile rootDir2 = LocalFileSystem.getInstance().findFileByPath(rootAfter.replace(File.separatorChar, '/')); + myProject.getComponent(PostprocessReformattingAspect.class).doPostponedFormatting(); + IdeaTestUtil.assertDirectoriesEqual(rootDir2, rootDir, IdeaTestUtil.CVS_FILE_FILTER); + } +}