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 15a8d9fd56a4..08bdc4cf790d 100644 --- a/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineMethodProcessor.java @@ -74,7 +74,7 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { private final CodeStyleManager myCodeStyleManager; private final JavaCodeStyleManager myJavaCodeStyle; - private PsiBlockStatement[] myAddedBraces; + private PsiCodeBlock[] myAddedBraces; private final String myDescriptiveName; private Map myAddedClassInitializers; private PsiMethod myMethodCopy; @@ -577,7 +577,9 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { } ChangeContextUtil.decodeContextInfo(anchorParent, thisClass, thisAccessExpr); - if (methodCall.getParent() instanceof PsiExpressionStatement || tailCall == InlineUtil.TailCallType.Return) { + if (methodCall.getParent() instanceof PsiLambdaExpression) { + methodCall.delete(); + } else if (methodCall.getParent() instanceof PsiExpressionStatement || tailCall == InlineUtil.TailCallType.Return) { methodCall.getParent().delete(); } else { @@ -1170,7 +1172,7 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { private PsiReferenceExpression[] addBracesWhenNeeded(PsiReferenceExpression[] refs) throws IncorrectOperationException { ArrayList refsVector = new ArrayList(); - ArrayList addedBracesVector = new ArrayList(); + ArrayList addedBracesVector = new ArrayList(); myAddedClassInitializers = new HashMap(); for (PsiReferenceExpression ref : refs) { @@ -1194,11 +1196,33 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { PsiElement newStatement = blockStatement.getCodeBlock().getStatements()[0]; addMarkedElements(refsVector, newStatement); - addedBracesVector.add(blockStatement); + addedBracesVector.add(blockStatement.getCodeBlock()); continue RefLoop; } parent = parent.getParent(); } + final PsiElement lambdaExpr = parentStatement.getParent(); + if (lambdaExpr instanceof PsiLambdaExpression) { + final PsiLambdaExpression newLambdaExpr = (PsiLambdaExpression)myFactory.createExpressionFromText( + ((PsiLambdaExpression)lambdaExpr).getParameterList().getText() + " -> " + "{\n}", lambdaExpr); + final PsiStatement statementFromText; + if (LambdaUtil.getFunctionalInterfaceReturnType((PsiLambdaExpression)lambdaExpr) == PsiType.VOID ) { + statementFromText = myFactory.createStatementFromText("a;", lambdaExpr); + ((PsiExpressionStatement)statementFromText).getExpression().replace(parentStatement); + } else { + statementFromText = myFactory.createStatementFromText("return a;", lambdaExpr); + ((PsiReturnStatement)statementFromText).getReturnValue().replace(parentStatement); + } + + newLambdaExpr.getBody().add(statementFromText); + + final PsiCodeBlock body = (PsiCodeBlock)((PsiLambdaExpression)lambdaExpr.replace(newLambdaExpr)).getBody(); + PsiElement newStatement = body.getStatements()[0]; + addMarkedElements(refsVector, newStatement); + addedBracesVector.add(body); + continue; + + } } else { final PsiField field = PsiTreeUtil.getParentOfType(ref, PsiField.class); @@ -1236,7 +1260,7 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { ref.putCopyableUserData(MARK_KEY, null); } - myAddedBraces = addedBracesVector.toArray(new PsiBlockStatement[addedBracesVector.size()]); + myAddedBraces = addedBracesVector.toArray(new PsiCodeBlock[addedBracesVector.size()]); return refsVector.toArray(new PsiReferenceExpression[refsVector.size()]); } @@ -1296,10 +1320,27 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor { private void removeAddedBracesWhenPossible() throws IncorrectOperationException { if (myAddedBraces == null) return; - for (PsiBlockStatement blockStatement : myAddedBraces) { - PsiStatement[] statements = blockStatement.getCodeBlock().getStatements(); + for (PsiCodeBlock codeBlock : myAddedBraces) { + PsiStatement[] statements = codeBlock.getStatements(); if (statements.length == 1) { - blockStatement.replace(statements[0]); + final PsiElement codeBlockParent = codeBlock.getParent(); + if (codeBlockParent instanceof PsiLambdaExpression) { + if (statements[0] instanceof PsiReturnStatement) { + final PsiExpression returnValue = ((PsiReturnStatement)statements[0]).getReturnValue(); + if (returnValue != null) { + codeBlock.replace(returnValue); + } + } else if (statements[0] instanceof PsiExpressionStatement){ + codeBlock.replace(((PsiExpressionStatement)statements[0]).getExpression()); + } + } + else { + if (codeBlockParent instanceof PsiBlockStatement) { + codeBlockParent.replace(statements[0]); + } else { + codeBlock.replace(statements[0]); + } + } } } diff --git a/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleOneLine.java b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleOneLine.java new file mode 100644 index 000000000000..4e43186908d7 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleOneLine.java @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2012 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. + */ +import java.lang.Comparable; + +class Test { + private int bar() { + return 42; + } + + public void foo() { + Comparable c = (o) -> bar(); + } +} diff --git a/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleOneLine.java.after b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleOneLine.java.after new file mode 100644 index 000000000000..a39e95079e99 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleOneLine.java.after @@ -0,0 +1,26 @@ +/* + * Copyright 2000-2012 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. + */ +import java.lang.Comparable; + +class Test { + private int bar() { + return 42; + } + + public void foo() { + Comparable c = (o) -> 42; + } +} diff --git a/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleToBlock.java b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleToBlock.java new file mode 100644 index 000000000000..abe9d7499111 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleToBlock.java @@ -0,0 +1,27 @@ +import java.lang.Comparable; + +/* + * Copyright 2000-2012 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. + */ +class Test { + private int bar() { + System.out.println(""); + return 42; + } + + public void foo() { + Comparable c = (o) -> bar(); + } +} diff --git a/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleToBlock.java.after b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleToBlock.java.after new file mode 100644 index 000000000000..c44932d2d404 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaValueCompatibleToBlock.java.after @@ -0,0 +1,30 @@ +import java.lang.Comparable; + +/* + * Copyright 2000-2012 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. + */ +class Test { + private int bar() { + System.out.println(""); + return 42; + } + + public void foo() { + Comparable c = (o) -> { + System.out.println(""); + return 42; + }; + } +} diff --git a/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleOneLine.java b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleOneLine.java new file mode 100644 index 000000000000..5199637766c9 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleOneLine.java @@ -0,0 +1,24 @@ +/* + * Copyright 2000-2012 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. + */ +class Test { + private void bar() { + System.out.println(""); + } + + public void foo() { + Runnable r = () -> bar(); + } +} diff --git a/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleOneLine.java.after b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleOneLine.java.after new file mode 100644 index 000000000000..c69cb20c874b --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleOneLine.java.after @@ -0,0 +1,24 @@ +/* + * Copyright 2000-2012 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. + */ +class Test { + private void bar() { + System.out.println(""); + } + + public void foo() { + Runnable r = () -> System.out.println(""); + } +} diff --git a/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleToBlock.java b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleToBlock.java new file mode 100644 index 000000000000..059ce334dd89 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleToBlock.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2012 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. + */ +class Test { + private void bar() { + System.out.println(""); + System.out.println(""); + } + + public void foo() { + Runnable r = () -> bar(); + } +} diff --git a/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleToBlock.java.after b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleToBlock.java.after new file mode 100644 index 000000000000..3827120139f6 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/OneLineLambdaVoidCompatibleToBlock.java.after @@ -0,0 +1,28 @@ +/* + * Copyright 2000-2012 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. + */ +class Test { + private void bar() { + System.out.println(""); + System.out.println(""); + } + + public void foo() { + Runnable r = () -> { + System.out.println(""); + System.out.println(""); + }; + } +} 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 0b4a346eda55..b1fad6e5e2f7 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java @@ -209,6 +209,22 @@ public class InlineMethodTest extends LightRefactoringTestCase { public void testInlineRunnableRun() throws Exception { doTestInlineThisOnly(); } + + public void testOneLineLambdaVoidCompatibleToBlock() throws Exception { + doTestInlineThisOnly(); + } + + public void testOneLineLambdaValueCompatibleToBlock() throws Exception { + doTestInlineThisOnly(); + } + + public void testOneLineLambdaVoidCompatibleOneLine() throws Exception { + doTestInlineThisOnly(); + } + + public void testOneLineLambdaValueCompatibleOneLine() throws Exception { + doTestInlineThisOnly(); + } private void doTestInlineThisOnly() { @NonNls String fileName = "/refactoring/inlineMethod/" + getTestName(false) + ".java";