method ref: inline method ref in qualifier place; do not treat cast as redundant there

This commit is contained in:
anna
2012-09-25 19:47:02 +02:00
parent 99ae9e1382
commit 97b9fbee9a
9 changed files with 69 additions and 15 deletions
@@ -137,17 +137,9 @@ public class InlineUtil {
}
}
} else if (exprType instanceof PsiLambdaExpressionType) {
final PsiLambdaExpression expression = ((PsiLambdaExpressionType)exprType).getExpression();
if (expression.getParent() instanceof PsiReferenceExpression) {
PsiTypeCastExpression cast = (PsiTypeCastExpression)JavaPsiFacade.getElementFactory(expr.getProject()).createExpressionFromText("(t)a", null);
PsiTypeElement castTypeElement = cast.getCastType();
assert castTypeElement != null;
castTypeElement.replace(variable.getTypeElement());
final PsiExpression operand = cast.getOperand();
assert operand != null;
operand.replace(expr);
expr = (PsiTypeCastExpression)expr.replace(cast);
}
expr = surroundWithCast(variable, expr, ((PsiLambdaExpressionType)exprType).getExpression());
} else if (exprType instanceof PsiMethodReferenceType) {
expr = surroundWithCast(variable, expr, ((PsiMethodReferenceType)exprType).getExpression());
}
ChangeContextUtil.clearContextInfo(initializer);
@@ -157,6 +149,20 @@ public class InlineUtil {
return (PsiExpression)ChangeContextUtil.decodeContextInfo(expr, thisClass, thisAccessExpr);
}
private static PsiExpression surroundWithCast(PsiVariable variable, PsiExpression expr, PsiExpression expression) {
if (expression.getParent() instanceof PsiReferenceExpression) {
PsiTypeCastExpression cast = (PsiTypeCastExpression)JavaPsiFacade.getElementFactory(expr.getProject()).createExpressionFromText("(t)a", null);
PsiTypeElement castTypeElement = cast.getCastType();
assert castTypeElement != null;
castTypeElement.replace(variable.getTypeElement());
final PsiExpression operand = cast.getOperand();
assert operand != null;
operand.replace(expr);
expr = (PsiTypeCastExpression)expr.replace(cast);
}
return expr;
}
private static PsiThisExpression createThisExpression(PsiManager manager, PsiClass thisClass, PsiClass refParent) {
PsiThisExpression thisAccessExpr = null;
if (Comparing.equal(thisClass, refParent))
@@ -70,4 +70,8 @@ public class PsiMethodReferenceType extends PsiType {
public PsiType[] getSuperTypes() {
return PsiType.EMPTY_ARRAY;
}
public PsiMethodReferenceExpression getExpression() {
return myReference;
}
}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems/>
@@ -0,0 +1,13 @@
class Test {
{
((Bar) Test::length)._("");
}
public static Integer length(String s) {
return s.length();
}
interface Bar {
Integer _(String s);
}
}
@@ -0,0 +1,14 @@
class Test {
{
Bar bar = Test::length;
b<caret>ar._("");
}
public static Integer length(String s) {
return s.length();
}
interface Bar {
Integer _(String s);
}
}
@@ -0,0 +1,13 @@
class Test {
{
((Bar) Test::length)._("");
}
public static Integer length(String s) {
return s.length();
}
interface Bar {
Integer _(String s);
}
}
@@ -17,9 +17,6 @@ package com.intellij.codeInspection;
import com.intellij.codeInspection.ex.LocalInspectionToolWrapper;
import com.intellij.codeInspection.redundantCast.RedundantCastInspection;
import com.intellij.openapi.projectRoots.JavaSdkVersion;
import com.intellij.openapi.projectRoots.JavaVersionService;
import com.intellij.openapi.projectRoots.JavaVersionServiceImpl;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
@@ -33,6 +30,7 @@ public class RedundantCast18Test extends InspectionTestCase {
}
public void testLambdaContext() throws Exception { doTest(); }
public void testMethodRefContext() throws Exception { doTest(); }
public void testExpectedSupertype() throws Exception { doTest(); }
protected Sdk getTestProjectSdk() {
@@ -168,6 +168,10 @@ public class InlineLocalTest extends LightCodeInsightTestCase {
doTest(true);
}
public void testMethodRefAsRefQualifier() throws Exception {
doTest(true);
}
public void testLocalVarInsideLambdaBody() throws Exception {
doTest(true);
}
@@ -389,7 +389,7 @@ public class RedundantCastUtil {
}
} else if (parent instanceof PsiSynchronizedStatement && (expr instanceof PsiExpression && ((PsiExpression)expr).getType() instanceof PsiPrimitiveType)) {
return;
} else if (expr instanceof PsiLambdaExpression) {
} else if (expr instanceof PsiLambdaExpression || expr instanceof PsiMethodReferenceExpression) {
if (parent instanceof PsiParenthesizedExpression && parent.getParent() instanceof PsiReferenceExpression) {
return;
}