put new variable inside lambda body: check lambda-related types only after lambda body is fixed (EA-62133 - IOE: PsiJavaParserFacadeImpl.createStatementFromText)

This commit is contained in:
Anna Kozlova
2014-10-24 18:36:22 +02:00
parent 04d5460d03
commit a56e52d868
4 changed files with 53 additions and 12 deletions
@@ -880,22 +880,22 @@ public class RefactoringUtil {
LOG.assertTrue(newBody != null);
newBody.add(declaration);
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);
}
PsiStatement lastBodyStatement = elementFactory.createStatementFromText("a;", lambdaExpression);
((PsiExpressionStatement)lastBodyStatement).getExpression().replace(lambdaExpressionBody);
newBody.add(lastBodyStatement);
final PsiLambdaExpression copy = (PsiLambdaExpression)lambdaExpression.replace(expressionFromText);
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);
}
@@ -0,0 +1,20 @@
class Foo {
interface I<T> {
T m(Object op);
}
interface J<T> {
int m(T o);
}
void f(J r) {}
void f(I<String> r) {}
{
f((Object a) -> {
int c = 1;
return c;
});
}
}
@@ -0,0 +1,17 @@
class Foo {
interface I<T> {
T m(Object op);
}
interface J<T> {
int m(T o);
}
void f(J r) {}
void f(I<String> r) {}
{
f((Object a) -> <selection>1</selection>);
}
}
@@ -437,6 +437,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
doTest(new MockIntroduceVariableHandler("c", false, false, false, "int"));
}
public void testPutInLambdaBody() {
doTest(new MockIntroduceVariableHandler("c", false, false, false, "int"));
}
public void testNormalizeDeclarations() {
doTest(new MockIntroduceVariableHandler("i3", false, false, false, "int"));
}