IDEA-CR-14593: MethodRefCanBeReplacedWithLambdaInspection#isWithSideEffects -> LambdaRefactoringUtil#canConvertToLambda; mapToFlatMap extracted

This commit is contained in:
Tagir Valeev
2016-10-19 16:25:20 +07:00
parent cf80f2738e
commit 19af166e5d
3 changed files with 38 additions and 23 deletions
@@ -31,8 +31,8 @@ import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.util.LambdaRefactoringUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ig.style.MethodRefCanBeReplacedWithLambdaInspection;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -87,8 +87,7 @@ public class InlineStreamMapAction extends PsiElementBaseIntentionAction {
return lambdaExpression.getParameterList().getParametersCount() == 1 &&
(!requireExpressionLambda || LambdaUtil.extractSingleExpressionFromBody(lambdaExpression.getBody()) != null);
} else if(expression instanceof PsiMethodReferenceExpression) {
PsiMethodReferenceExpression methodReference = (PsiMethodReferenceExpression)expression;
return !MethodRefCanBeReplacedWithLambdaInspection.isWithSideEffects(methodReference);
return LambdaRefactoringUtil.canConvertToLambda((PsiMethodReferenceExpression)expression);
}
return false;
}
@@ -161,12 +160,29 @@ public class InlineStreamMapAction extends PsiElementBaseIntentionAction {
}
}
if(nextName.equals("flatMap") && prevClassName.equals(CommonClassNames.JAVA_UTIL_STREAM_STREAM)) {
String mapMethod = translateMap(prevName);
return "flatM"+mapMethod.substring(1);
return mapToFlatMap(prevName);
}
return null;
}
@Contract(pure = true)
@Nullable
private static String mapToFlatMap(String mapMethod) {
switch (mapMethod) {
case "map":
return "flatMap";
case "mapToInt":
return "flatMapToInt";
case "mapToLong":
return "flatMapToLong";
case "mapToDouble":
return "flatMapToDouble";
}
// Something unsupported passed: ignore
return null;
}
@Contract(pure = true)
@NotNull
private static String translateMap(String nextMethod) {
switch (nextMethod) {
@@ -77,7 +77,6 @@ public class LambdaRefactoringUtil {
final PsiParameter[] psiParameters = resolve instanceof PsiMethod ? ((PsiMethod)resolve).getParameterList().getParameters() : null;
final StringBuilder buf = new StringBuilder("(");
LOG.assertTrue(functionalInterfaceType != null);
buf.append(GenericsUtil.getVariableTypeByExpressionType(functionalInterfaceType).getCanonicalText()).append(")(");
final PsiParameterList parameterList = interfaceMethod.getParameterList();
final PsiParameter[] parameters = parameterList.getParameters();
@@ -103,6 +102,7 @@ public class LambdaRefactoringUtil {
else {
initialName = parameter.getName();
}
LOG.assertTrue(initialName != null);
baseName = codeStyleManager.variableNameToPropertyName(initialName, VariableKind.PARAMETER);
}
@@ -265,4 +265,16 @@ public class LambdaRefactoringUtil {
}
}
}
/**
* Checks whether method reference can be converted to lambda without significant semantics change
* (i.e. method reference qualifier has no side effects)
*
* @param methodReferenceExpression method reference to check
* @return true if method reference can be converted to lambda
*/
public static boolean canConvertToLambda(PsiMethodReferenceExpression methodReferenceExpression) {
final PsiExpression qualifierExpression = methodReferenceExpression.getQualifierExpression();
return qualifierExpression != null && !SideEffectChecker.mayHaveSideEffects(qualifierExpression);
}
}