From bf60885576e3eca598d64eb5df71afacca3ea893 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Thu, 20 Oct 2016 14:59:25 +0200 Subject: [PATCH] inline chained constructor from library class --- .../refactoring/inline/InlineMethodProcessor.java | 6 +++--- .../RespectProjectScopeSrcConstructorCall.java | 11 +++++++++++ ...spectProjectScopeSrcConstructorCall.java.after | 12 ++++++++++++ .../refactoring/inline/InlineMethodTest.java | 15 +++++++++++---- 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 java/java-tests/testData/refactoring/inlineMethod/RespectProjectScopeSrcConstructorCall.java create mode 100644 java/java-tests/testData/refactoring/inlineMethod/RespectProjectScopeSrcConstructorCall.java.after 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 bc09d0b41b6f..eee9a41d521e 100644 --- a/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java @@ -470,7 +470,6 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { GenericInlineHandler.inlineReference(usage, myMethod, myInliners); } } - myMethod.delete(); } else { List refExprList = new ArrayList<>(); @@ -513,8 +512,8 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { psiElement.delete(); } } - if (myMethod.isWritable()) myMethod.delete(); } + if (myMethod.isWritable()) myMethod.delete(); } removeAddedBracesWhenPossible(); } @@ -544,8 +543,9 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { } public static void inlineConstructorCall(PsiCall constructorCall) { - final PsiMethod oldConstructor = constructorCall.resolveMethod(); + PsiMethod oldConstructor = constructorCall.resolveMethod(); LOG.assertTrue(oldConstructor != null); + oldConstructor = (PsiMethod)oldConstructor.getNavigationElement(); PsiExpression[] instanceCreationArguments = constructorCall.getArgumentList().getExpressions(); if (oldConstructor.isVarArgs()) { //wrap with explicit array final PsiParameter[] parameters = oldConstructor.getParameterList().getParameters(); diff --git a/java/java-tests/testData/refactoring/inlineMethod/RespectProjectScopeSrcConstructorCall.java b/java/java-tests/testData/refactoring/inlineMethod/RespectProjectScopeSrcConstructorCall.java new file mode 100644 index 000000000000..e8e2b6007b3b --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/RespectProjectScopeSrcConstructorCall.java @@ -0,0 +1,11 @@ +import java.io.FileInputStream; + +class Launcher { + { + try { + FileInputStream f = new FileInputStream(""); + catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineMethod/RespectProjectScopeSrcConstructorCall.java.after b/java/java-tests/testData/refactoring/inlineMethod/RespectProjectScopeSrcConstructorCall.java.after new file mode 100644 index 000000000000..cc4458029fc4 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/RespectProjectScopeSrcConstructorCall.java.after @@ -0,0 +1,12 @@ +import java.io.File; +import java.io.FileInputStream; + +class Launcher { + { + try { + FileInputStream f = new FileInputStream("" != null ? new File("") : null); + catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java index 20e7b428f771..288f8709a0be 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java @@ -19,10 +19,7 @@ import com.intellij.JavaTestUtil; import com.intellij.codeInsight.TargetElementUtil; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.pom.java.LanguageLevel; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiMethod; -import com.intellij.psi.PsiReference; -import com.intellij.psi.PsiReferenceExpression; +import com.intellij.psi.*; import com.intellij.refactoring.BaseRefactoringProcessor; import com.intellij.refactoring.LightRefactoringTestCase; import com.intellij.refactoring.MockInlineMethodOptions; @@ -348,6 +345,10 @@ public class InlineMethodTest extends LightRefactoringTestCase { doTest(); } + public void testRespectProjectScopeSrcConstructorCall() throws Exception { + doTest(); + } + @Override protected Sdk getProjectJDK() { return getTestName(false).contains("Src") ? IdeaTestUtil.getMockJdk17() : super.getProjectJDK(); @@ -385,6 +386,12 @@ public class InlineMethodTest extends LightRefactoringTestCase { PsiElement element = TargetElementUtil .findTargetElement(myEditor, TargetElementUtil.ELEMENT_NAME_ACCEPTED | TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED); final PsiReference ref = myFile.findReferenceAt(myEditor.getCaretModel().getOffset()); + if (ref instanceof PsiJavaCodeReferenceElement) { + final PsiElement parent = ((PsiJavaCodeReferenceElement)ref).getParent(); + if (parent instanceof PsiNewExpression) { + element = ((PsiNewExpression)parent).resolveConstructor(); + } + } PsiReferenceExpression refExpr = ref instanceof PsiReferenceExpression ? (PsiReferenceExpression)ref : null; assertTrue(element instanceof PsiMethod); PsiMethod method = (PsiMethod)element.getNavigationElement();