mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
IDEA-CR-14593: MethodRefCanBeReplacedWithLambdaInspection#isWithSideEffects -> LambdaRefactoringUtil#canConvertToLambda; mapToFlatMap extracted
This commit is contained in:
+21
-5
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user