From 97b9fbee9af3b799a7e80951fe5590baddbbbf89 Mon Sep 17 00:00:00 2001 From: anna Date: Tue, 25 Sep 2012 17:02:30 +0200 Subject: [PATCH] method ref: inline method ref in qualifier place; do not treat cast as redundant there --- .../intellij/refactoring/util/InlineUtil.java | 28 +++++++++++-------- .../intellij/psi/PsiMethodReferenceType.java | 4 +++ .../lambda/MethodRefContext/expected.xml | 2 ++ .../lambda/MethodRefContext/src/Test.java | 13 +++++++++ .../inlineLocal/MethodRefAsRefQualifier.java | 14 ++++++++++ .../MethodRefAsRefQualifier.java.after | 13 +++++++++ .../codeInspection/RedundantCast18Test.java | 4 +-- .../refactoring/inline/InlineLocalTest.java | 4 +++ .../intellij/psi/util/RedundantCastUtil.java | 2 +- 9 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext/expected.xml create mode 100644 java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext/src/Test.java create mode 100644 java/java-tests/testData/refactoring/inlineLocal/MethodRefAsRefQualifier.java create mode 100644 java/java-tests/testData/refactoring/inlineLocal/MethodRefAsRefQualifier.java.after diff --git a/java/java-impl/src/com/intellij/refactoring/util/InlineUtil.java b/java/java-impl/src/com/intellij/refactoring/util/InlineUtil.java index 8ee73bb0017f..8b9aaa59dd8b 100644 --- a/java/java-impl/src/com/intellij/refactoring/util/InlineUtil.java +++ b/java/java-impl/src/com/intellij/refactoring/util/InlineUtil.java @@ -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)) diff --git a/java/java-psi-api/src/com/intellij/psi/PsiMethodReferenceType.java b/java/java-psi-api/src/com/intellij/psi/PsiMethodReferenceType.java index 2901f7735919..d64ff6788138 100644 --- a/java/java-psi-api/src/com/intellij/psi/PsiMethodReferenceType.java +++ b/java/java-psi-api/src/com/intellij/psi/PsiMethodReferenceType.java @@ -70,4 +70,8 @@ public class PsiMethodReferenceType extends PsiType { public PsiType[] getSuperTypes() { return PsiType.EMPTY_ARRAY; } + + public PsiMethodReferenceExpression getExpression() { + return myReference; + } } diff --git a/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext/expected.xml b/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext/expected.xml new file mode 100644 index 000000000000..4704d91e891d --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext/expected.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext/src/Test.java b/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext/src/Test.java new file mode 100644 index 000000000000..b82f2ca2538a --- /dev/null +++ b/java/java-tests/testData/inspection/redundantCast/lambda/MethodRefContext/src/Test.java @@ -0,0 +1,13 @@ +class Test { + { + ((Bar) Test::length)._(""); + } + + public static Integer length(String s) { + return s.length(); + } + + interface Bar { + Integer _(String s); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineLocal/MethodRefAsRefQualifier.java b/java/java-tests/testData/refactoring/inlineLocal/MethodRefAsRefQualifier.java new file mode 100644 index 000000000000..165286233917 --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineLocal/MethodRefAsRefQualifier.java @@ -0,0 +1,14 @@ +class Test { + { + Bar bar = Test::length; + bar._(""); + } + + public static Integer length(String s) { + return s.length(); + } + + interface Bar { + Integer _(String s); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineLocal/MethodRefAsRefQualifier.java.after b/java/java-tests/testData/refactoring/inlineLocal/MethodRefAsRefQualifier.java.after new file mode 100644 index 000000000000..b82f2ca2538a --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineLocal/MethodRefAsRefQualifier.java.after @@ -0,0 +1,13 @@ +class Test { + { + ((Bar) Test::length)._(""); + } + + public static Integer length(String s) { + return s.length(); + } + + interface Bar { + Integer _(String s); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast18Test.java b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast18Test.java index 93638485e216..f347d3b35aa4 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast18Test.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/RedundantCast18Test.java @@ -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() { diff --git a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java index e312ee81defa..7c4558026a0b 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineLocalTest.java @@ -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); } diff --git a/java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java b/java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java index 601796eb62ce..ee390db92977 100644 --- a/java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java +++ b/java/openapi/src/com/intellij/psi/util/RedundantCastUtil.java @@ -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; }