From 260c6317c84462177800ec2d7c9ca66c35975d49 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Fri, 21 Oct 2016 14:50:42 +0200 Subject: [PATCH] inline local: go through lambda/inner class hierarchy evenly (IDEA-162909) --- .../intellij/refactoring/inline/InlineLocalHandler.java | 8 +++++--- .../inlineLocal/InlineVariableIntoNestedLambda.java | 6 ++++++ .../inlineLocal/InlineVariableIntoNestedLambda.java.after | 5 +++++ .../com/intellij/refactoring/inline/InlineLocalTest.java | 6 +++++- 4 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 java/java-tests/testData/refactoring/inlineLocal/InlineVariableIntoNestedLambda.java create mode 100644 java/java-tests/testData/refactoring/inlineLocal/InlineVariableIntoNestedLambda.java.after diff --git a/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java b/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java index e00821d3f98a..52f193742000 100644 --- a/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -45,7 +45,9 @@ import com.intellij.refactoring.listeners.RefactoringEventData; import com.intellij.refactoring.listeners.RefactoringEventListener; import com.intellij.refactoring.util.CommonRefactoringUtil; import com.intellij.refactoring.util.InlineUtil; -import com.intellij.util.*; +import com.intellij.util.ArrayUtil; +import com.intellij.util.IncorrectOperationException; +import com.intellij.util.Query; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -96,7 +98,7 @@ public class InlineLocalHandler extends JavaInlineActionHandler { final PsiElement element = psiReference.getElement(); PsiElement innerClass = PsiTreeUtil.getParentOfType(element, PsiClass.class, PsiLambdaExpression.class); while (innerClass != containingClass && innerClass != null) { - final PsiClass parentPsiClass = PsiTreeUtil.getParentOfType(innerClass, PsiClass.class, true); + final PsiElement parentPsiClass = PsiTreeUtil.getParentOfType(innerClass.getParent(), PsiClass.class, PsiLambdaExpression.class); if (parentPsiClass == containingClass) { if (innerClass instanceof PsiLambdaExpression) { if (PsiTreeUtil.isAncestor(innerClass, local, false)) { diff --git a/java/java-tests/testData/refactoring/inlineLocal/InlineVariableIntoNestedLambda.java b/java/java-tests/testData/refactoring/inlineLocal/InlineVariableIntoNestedLambda.java new file mode 100644 index 000000000000..1ffb8ed7afae --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineLocal/InlineVariableIntoNestedLambda.java @@ -0,0 +1,6 @@ +class Test { + { + String s = "hello"; + Runnable r = () -> {Runnable rr = () -> System.out.println(s);}; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineLocal/InlineVariableIntoNestedLambda.java.after b/java/java-tests/testData/refactoring/inlineLocal/InlineVariableIntoNestedLambda.java.after new file mode 100644 index 000000000000..bf51c648d5b8 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineLocal/InlineVariableIntoNestedLambda.java.after @@ -0,0 +1,5 @@ +class Test { + { + Runnable r = () -> {Runnable rr = () -> System.out.println("hello");}; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java index 6559b6bb6447..1bf326ee32c5 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -292,6 +292,10 @@ public class InlineLocalTest extends LightCodeInsightTestCase { "Variable 'hello' is accessed for writing"); } + public void testInlineVariableIntoNestedLambda() throws Exception { + doTest(false); + } + public void testAvoidTypeSpecificationWhenPossibleToAvoid() throws Exception { doTest(false); }