diff --git a/java/java-impl/src/com/intellij/refactoring/util/LambdaRefactoringUtil.java b/java/java-impl/src/com/intellij/refactoring/util/LambdaRefactoringUtil.java index 1721264cff2a..98f43b8be70b 100644 --- a/java/java-impl/src/com/intellij/refactoring/util/LambdaRefactoringUtil.java +++ b/java/java-impl/src/com/intellij/refactoring/util/LambdaRefactoringUtil.java @@ -28,7 +28,6 @@ import com.intellij.psi.codeStyle.VariableKind; import com.intellij.psi.util.MethodSignature; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; -import com.intellij.psi.util.RedundantCastUtil; import com.intellij.refactoring.introduceField.ElementToWorkOn; import com.intellij.refactoring.introduceVariable.IntroduceVariableHandler; import com.intellij.util.Function; @@ -77,8 +76,7 @@ public class LambdaRefactoringUtil { } final PsiParameter[] psiParameters = resolve instanceof PsiMethod ? ((PsiMethod)resolve).getParameterList().getParameters() : null; - final StringBuilder buf = new StringBuilder("("); - buf.append(GenericsUtil.getVariableTypeByExpressionType(functionalInterfaceType).getCanonicalText()).append(")"); + final StringBuilder buf = new StringBuilder(); final PsiParameterList parameterList = interfaceMethod.getParameterList(); final PsiParameter[] parameters = parameterList.getParameters(); @@ -224,14 +222,17 @@ public class LambdaRefactoringUtil { } + PsiLambdaExpression lambdaExpression = (PsiLambdaExpression)referenceExpression.replace(elementFactory.createExpressionFromText(buf.toString(), referenceExpression)); + /* final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)referenceExpression.replace(elementFactory.createExpressionFromText(buf.toString(), referenceExpression)); - PsiLambdaExpression lambdaExpression = (PsiLambdaExpression)typeCastExpression.getOperand(); + LOG.assertTrue(lambdaExpression != null, buf.toString()); if (RedundantCastUtil.isCastRedundant(typeCastExpression) || ignoreCast) { final PsiExpression operand = typeCastExpression.getOperand(); LOG.assertTrue(operand != null); lambdaExpression = (PsiLambdaExpression)typeCastExpression.replace(operand); } + */ if (simplifyToExpressionLambda) { simplifyToExpressionLambda(lambdaExpression); diff --git a/java/java-tests/testData/refactoring/inlineMethod/RedundantCastOnMethodReferenceToLambda.java b/java/java-tests/testData/refactoring/inlineMethod/RedundantCastOnMethodReferenceToLambda.java new file mode 100644 index 000000000000..45a8efdf7b2f --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/RedundantCastOnMethodReferenceToLambda.java @@ -0,0 +1,28 @@ + +import java.util.*; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collector; + +import static java.util.function.Predicate.isEqual; + +class InlineRef { + Optional findEmpty() { + Set children = new HashSet<>(); + return children + .stream() + .filter(where(InlineRef::getName, isEqual(""))) + .findAny(); + } + + + static Predicate where(Function function, Predicate predicate) { + return input -> predicate.test(function.apply(input)); + } + + static String getName(Descriptor desc) { + return "name"; + } +} + +class Descriptor { } \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/inlineMethod/RedundantCastOnMethodReferenceToLambda.java.after b/java/java-tests/testData/refactoring/inlineMethod/RedundantCastOnMethodReferenceToLambda.java.after new file mode 100644 index 000000000000..a5cde05e488d --- /dev/null +++ b/java/java-tests/testData/refactoring/inlineMethod/RedundantCastOnMethodReferenceToLambda.java.after @@ -0,0 +1,24 @@ + +import java.util.*; +import java.util.function.Function; +import java.util.function.Predicate; + +import static java.util.function.Predicate.isEqual; + +class InlineRef { + Optional findEmpty() { + Set children = new HashSet<>(); + return children + .stream() + .filter(where(desc -> "name", isEqual(""))) + .findAny(); + } + + + static Predicate where(Function function, Predicate predicate) { + return input -> predicate.test(function.apply(input)); + } + +} + +class Descriptor { } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java index 288f8709a0be..e781c410034e 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/inline/InlineMethodTest.java @@ -309,6 +309,10 @@ public class InlineMethodTest extends LightRefactoringTestCase { doTestConflict("Inlined method is used in method reference with side effects in qualifier"); } + public void testRedundantCastOnMethodReferenceToLambda() throws Exception { + doTest(); + } + public void testInaccessibleSuperCallWhenQualifiedInline() throws Exception { doTestConflict("Inlined method calls super.bar() which won't be accessed in class B"); } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/CollapseToExpressionLambdaWhenCast.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/CollapseToExpressionLambdaWhenCast.after.java index 6e9cfc0a4e5a..705ae34aa902 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/CollapseToExpressionLambdaWhenCast.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/CollapseToExpressionLambdaWhenCast.after.java @@ -7,7 +7,7 @@ import java.util.stream.Collectors; class Collectors2 { static Map combine(Collection> pMaps) { return pMaps.stream() - .map((java.util.function.Function, java.util.Set>>) integerMap -> integerMap.entrySet()) + .map(integerMap -> integerMap.entrySet()) .flatMap(Collection::stream) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (pEntry1, pEntry2) -> pEntry1 + pEntry2, diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/NoCastWhereCaptureArgIsExpected.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/NoCastWhereCaptureArgIsExpected.after.java new file mode 100644 index 000000000000..3a789a17d15f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/NoCastWhereCaptureArgIsExpected.after.java @@ -0,0 +1,22 @@ +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.OptionalInt; +import java.util.function.Function; +import java.util.stream.Collector; + +public class Main { + interface Index { + int asInteger(); + } + interface IndexSet { + List asList(); + } + + public static OptionalInt min(IndexSet set) { + return set.asList() + .stream() + .mapToInt(o -> o.asInteger()) + .min(); + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/NoCastWhereCaptureArgIsExpected.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/NoCastWhereCaptureArgIsExpected.java new file mode 100644 index 000000000000..89e21eed27dd --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/style/methodRefs2lambda/NoCastWhereCaptureArgIsExpected.java @@ -0,0 +1,22 @@ +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.OptionalInt; +import java.util.function.Function; +import java.util.stream.Collector; + +public class Main { + interface Index { + int asInteger(); + } + interface IndexSet { + List asList(); + } + + public static OptionalInt min(IndexSet set) { + return set.asList() + .stream() + .mapToInt(Index::asInteger) + .min(); + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/style/MethodRefCanBeReplacedWithLambdaFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/style/MethodRefCanBeReplacedWithLambdaFixTest.java index cd3ccf9a0bcd..a6e0fd8f4d39 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/style/MethodRefCanBeReplacedWithLambdaFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/style/MethodRefCanBeReplacedWithLambdaFixTest.java @@ -134,4 +134,8 @@ public class MethodRefCanBeReplacedWithLambdaFixTest extends IGQuickFixesTestCas public void testNoUnderscoreInLambdaParameterName() throws Exception { doTest(); } + + public void testNoCastWhereCaptureArgIsExpected() throws Exception { + doTest(); + } }