mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
calculate void/value lambda compatibility based on valid, non-modified lambda expression
This commit is contained in:
@@ -870,32 +870,38 @@ public class RefactoringUtil {
|
||||
JavaCodeStyleManager.getInstance(declaration.getProject()).shortenClassReferences(declaration);
|
||||
if (loopBodyCopy != null) codeBlock.add(loopBodyCopy);
|
||||
} else if (container instanceof PsiLambdaExpression) {
|
||||
final PsiLambdaExpression lambdaExpression = (PsiLambdaExpression)container;
|
||||
final PsiElement lambdaExpressionBody = lambdaExpression.getBody();
|
||||
LOG.assertTrue(lambdaExpressionBody != null);
|
||||
PsiLambdaExpression lambdaExpression = (PsiLambdaExpression)container;
|
||||
final PsiElement invalidBody = lambdaExpression.getBody();
|
||||
if (invalidBody == null) return declaration;
|
||||
|
||||
final PsiLambdaExpression expressionFromText = (PsiLambdaExpression)elementFactory
|
||||
.createExpressionFromText(lambdaExpression.getParameterList().getText() + " -> {}", lambdaExpression);
|
||||
.createExpressionFromText(lambdaExpression.getParameterList().getText() + " -> {}", lambdaExpression.getParent());
|
||||
PsiCodeBlock newBody = (PsiCodeBlock)expressionFromText.getBody();
|
||||
LOG.assertTrue(newBody != null);
|
||||
newBody.add(declaration);
|
||||
|
||||
PsiStatement lastBodyStatement = elementFactory.createStatementFromText("a;", lambdaExpression);
|
||||
((PsiExpressionStatement)lastBodyStatement).getExpression().replace(lambdaExpressionBody);
|
||||
lambdaExpression =
|
||||
(PsiLambdaExpression)lambdaExpression.replace(elementFactory.createExpressionFromText(
|
||||
lambdaExpression.getParameterList().getText() + " -> " + invalidBody.getText(), lambdaExpression));
|
||||
|
||||
final PsiElement lambdaExpressionBody = lambdaExpression.getBody();
|
||||
LOG.assertTrue(lambdaExpressionBody != null);
|
||||
final PsiStatement lastBodyStatement;
|
||||
if (LambdaUtil.getFunctionalInterfaceReturnType(lambdaExpression) == PsiType.VOID) {
|
||||
lastBodyStatement = elementFactory.createStatementFromText("a;", lambdaExpression);
|
||||
((PsiExpressionStatement)lastBodyStatement).getExpression().replace(lambdaExpressionBody);
|
||||
}
|
||||
else {
|
||||
lastBodyStatement = elementFactory.createStatementFromText("return a;", lambdaExpression);
|
||||
final PsiExpression returnValue = ((PsiReturnStatement)lastBodyStatement).getReturnValue();
|
||||
LOG.assertTrue(returnValue != null);
|
||||
returnValue.replace(lambdaExpressionBody);
|
||||
}
|
||||
newBody.add(lastBodyStatement);
|
||||
|
||||
PsiLambdaExpression copy = (PsiLambdaExpression)lambdaExpression.replace(expressionFromText);
|
||||
final PsiLambdaExpression copy = (PsiLambdaExpression)lambdaExpression.replace(expressionFromText);
|
||||
newBody = (PsiCodeBlock)copy.getBody();
|
||||
LOG.assertTrue(newBody != null);
|
||||
|
||||
if (LambdaUtil.getFunctionalInterfaceReturnType(copy) != PsiType.VOID) {
|
||||
PsiExpressionStatement lastStatement = (PsiExpressionStatement)newBody.getStatements()[1];
|
||||
PsiReturnStatement returnStatement = (PsiReturnStatement)elementFactory.createStatementFromText("return a;", copy);
|
||||
final PsiExpression returnValue = returnStatement.getReturnValue();
|
||||
LOG.assertTrue(returnValue != null);
|
||||
returnValue.replace(lastStatement.getExpression());
|
||||
lastStatement.replace(returnStatement);
|
||||
}
|
||||
declaration = newBody.getStatements()[0];
|
||||
declaration = (PsiStatement)JavaCodeStyleManager.getInstance(declaration.getProject()).shortenClassReferences(declaration);
|
||||
}
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
class Foo {
|
||||
interface I<T> {
|
||||
T m(Object op);
|
||||
}
|
||||
|
||||
interface J<T> {
|
||||
void m(T o);
|
||||
}
|
||||
|
||||
|
||||
void f(J r) {}
|
||||
void f(I<String> r) {}
|
||||
|
||||
{
|
||||
f((a) -> {
|
||||
int c = 1;
|
||||
return c;
|
||||
});
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
class Foo {
|
||||
interface I<T> {
|
||||
T m(Object op);
|
||||
}
|
||||
|
||||
interface J<T> {
|
||||
void m(T o);
|
||||
}
|
||||
|
||||
|
||||
void f(J r) {}
|
||||
void f(I<String> r) {}
|
||||
|
||||
{
|
||||
f((a) -> <selection>1</selection>);
|
||||
}
|
||||
}
|
||||
@@ -441,6 +441,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
|
||||
doTest(new MockIntroduceVariableHandler("c", false, false, false, "int"));
|
||||
}
|
||||
|
||||
public void testPutInLambdaBodyVoidValueConflict() {
|
||||
doTest(new MockIntroduceVariableHandler("c", false, false, false, "int"));
|
||||
}
|
||||
|
||||
public void testNormalizeDeclarations() {
|
||||
doTest(new MockIntroduceVariableHandler("i3", false, false, false, "int"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user