assume that lambda on method references place won't require additional cast (IDEA-165335; IDEA-165039)

This commit is contained in:
Anna.Kozlova
2016-12-20 18:10:04 +01:00
parent 1651508c87
commit 38cb2b8c29
8 changed files with 110 additions and 5 deletions

View File

@@ -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);

View File

@@ -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<? extends Descriptor> findEmpty() {
Set<? extends Descriptor> children = new HashSet<>();
return children
.stream()
.filter(where(InlineRef::get<caret>Name, isEqual("")))
.findAny();
}
static <T, V> Predicate<T> where(Function<T, V> function, Predicate<? super V> predicate) {
return input -> predicate.test(function.apply(input));
}
static String getName(Descriptor desc) {
return "name";
}
}
class Descriptor { }

View File

@@ -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<? extends Descriptor> findEmpty() {
Set<? extends Descriptor> children = new HashSet<>();
return children
.stream()
.filter(where(desc -> "name", isEqual("")))
.findAny();
}
static <T, V> Predicate<T> where(Function<T, V> function, Predicate<? super V> predicate) {
return input -> predicate.test(function.apply(input));
}
}
class Descriptor { }

View File

@@ -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><code>B</code></b>");
}

View File

@@ -7,7 +7,7 @@ import java.util.stream.Collectors;
class Collectors2 {
static <T> Map<T, Integer> combine(Collection<Map<? extends T, Integer>> pMaps) {
return pMaps.stream()
.map((java.util.function.Function<Map<? extends T, Integer>, java.util.Set<? extends Map.Entry<? extends T, Integer>>>) integerMap -> integerMap.entrySet())
.map(integerMap -> integerMap.entrySet())
.flatMap(Collection::stream)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(pEntry1, pEntry2) -> pEntry1 + pEntry2,

View File

@@ -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<S extends Index> {
List<S> asList();
}
public static OptionalInt min(IndexSet<?> set) {
return set.asList()
.stream()
.mapToInt(o -> o.asInteger())
.min();
}
}

View File

@@ -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<S extends Index> {
List<S> asList();
}
public static OptionalInt min(IndexSet<?> set) {
return set.asList()
.stream()
.mapToInt(Index::asInte<caret>ger)
.min();
}
}

View File

@@ -134,4 +134,8 @@ public class MethodRefCanBeReplacedWithLambdaFixTest extends IGQuickFixesTestCas
public void testNoUnderscoreInLambdaParameterName() throws Exception {
doTest();
}
public void testNoCastWhereCaptureArgIsExpected() throws Exception {
doTest();
}
}