surround lambda/method refs with cast when inline in expression list (IDEA-92742)

This commit is contained in:
anna
2012-10-10 17:19:44 +02:00
parent a927ecf5d1
commit 7d4be5d223
6 changed files with 59 additions and 1 deletions
@@ -150,7 +150,8 @@ public class InlineUtil {
}
private static PsiExpression surroundWithCast(PsiVariable variable, PsiExpression expr, PsiExpression expression) {
if (expression.getParent() instanceof PsiReferenceExpression) {
final PsiElement parent = expression.getParent();
if (parent instanceof PsiReferenceExpression || parent instanceof PsiExpressionList) {
PsiTypeCastExpression cast = (PsiTypeCastExpression)JavaPsiFacade.getElementFactory(expr.getProject()).createExpressionFromText("(t)a", null);
PsiTypeElement castTypeElement = cast.getCastType();
assert castTypeElement != null;
@@ -159,6 +160,9 @@ public class InlineUtil {
assert operand != null;
operand.replace(expr);
expr = (PsiTypeCastExpression)expr.replace(cast);
if (RedundantCastUtil.isCastRedundant((PsiTypeCastExpression)expr)) {
expr = (PsiExpression)expr.replace(((PsiTypeCastExpression)expr).getOperand());
}
}
return expr;
}
@@ -0,0 +1,12 @@
class Test {
void test() {
I b1 = System::exit;
a(b<caret>1);
}
void a(Object b) {}
interface I {
void i(int i);
}
}
@@ -0,0 +1,11 @@
class Test {
void test() {
a((I) System::exit);
}
void a(Object b) {}
interface I {
void i(int i);
}
}
@@ -0,0 +1,12 @@
class Test {
void test() {
I b1 = System::exit;
a(b<caret>1);
}
void a(I b) {}
interface I {
void i(int i);
}
}
@@ -0,0 +1,11 @@
class Test {
void test() {
a(System::exit);
}
void a(I b) {}
interface I {
void i(int i);
}
}
@@ -184,6 +184,14 @@ public class InlineLocalTest extends LightCodeInsightTestCase {
doTest(true);
}
public void testCastAroundLambda() throws Exception {
doTest(true);
}
public void testNoCastAroundLambda() throws Exception {
doTest(true);
}
public void testLocalVarInsideLambdaBodyWriteUsage() throws Exception {
doTest(true, "Cannot perform refactoring.\n" +
"Variable 'hello' is accessed for writing.");