mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
inline method to one line lambda: expand to code block when needed (IDEA-91126)
This commit is contained in:
@@ -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<PsiField, PsiClassInitializer> 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<PsiReferenceExpression> refsVector = new ArrayList<PsiReferenceExpression>();
|
||||
ArrayList<PsiBlockStatement> addedBracesVector = new ArrayList<PsiBlockStatement>();
|
||||
ArrayList<PsiCodeBlock> addedBracesVector = new ArrayList<PsiCodeBlock>();
|
||||
myAddedClassInitializers = new HashMap<PsiField, PsiClassInitializer>();
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+26
@@ -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<String> c = (o) -> ba<caret>r();
|
||||
}
|
||||
}
|
||||
+26
@@ -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<String> c = (o) -> 42;
|
||||
}
|
||||
}
|
||||
+27
@@ -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<String> c = (o) -> ba<caret>r();
|
||||
}
|
||||
}
|
||||
+30
@@ -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<String> c = (o) -> {
|
||||
System.out.println("");
|
||||
return 42;
|
||||
};
|
||||
}
|
||||
}
|
||||
+24
@@ -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 = () -> ba<caret>r();
|
||||
}
|
||||
}
|
||||
+24
@@ -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("");
|
||||
}
|
||||
}
|
||||
+25
@@ -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 = () -> ba<caret>r();
|
||||
}
|
||||
}
|
||||
+28
@@ -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("");
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user