mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-CR-13639 LambdaCanBeMethodReferenceInspection refactored, more detailed option messages
This commit is contained in:
+5
-3
@@ -84,7 +84,9 @@ public class AnonymousCanBeMethodReferenceInspection extends BaseJavaBatchLocalI
|
||||
final PsiMethod method = aClass.getMethods()[0];
|
||||
final PsiCodeBlock body = method.getBody();
|
||||
final PsiExpression methodRefCandidate =
|
||||
LambdaCanBeMethodReferenceInspection.canBeMethodReferenceProblem(body, method.getParameterList().getParameters(), aClass.getBaseClassType(), aClass.getParent());
|
||||
new LambdaCanBeMethodReferenceInspection()
|
||||
.canBeMethodReferenceProblem(body, method.getParameterList().getParameters(), aClass.getBaseClassType(),
|
||||
aClass.getParent());
|
||||
if (methodRefCandidate instanceof PsiCallExpression) {
|
||||
final PsiCallExpression callExpression = (PsiCallExpression)methodRefCandidate;
|
||||
final PsiMethod resolveMethod = callExpression.resolveMethod();
|
||||
@@ -133,8 +135,8 @@ public class AnonymousCanBeMethodReferenceInspection extends BaseJavaBatchLocalI
|
||||
if (methods.length != 1) return;
|
||||
|
||||
final PsiParameter[] parameters = methods[0].getParameterList().getParameters();
|
||||
final String methodRefText =
|
||||
LambdaCanBeMethodReferenceInspection.convertToMethodReference(methods[0].getBody(), parameters, anonymousClass.getBaseClassType(), anonymousClass.getParent());
|
||||
final String methodRefText = LambdaCanBeMethodReferenceInspection
|
||||
.convertToMethodReference(methods[0].getBody(), parameters, anonymousClass.getBaseClassType(), anonymousClass.getParent());
|
||||
|
||||
replaceWithMethodReference(project, methodRefText, anonymousClass.getBaseClassType(), anonymousClass.getParent());
|
||||
}
|
||||
|
||||
+1
-5
@@ -183,11 +183,7 @@ public class ComparatorCombinatorsInspection extends BaseJavaBatchLocalInspectio
|
||||
PsiParameter[] parameters = lambda.getParameterList().getParameters();
|
||||
PsiElement body = lambda.getBody();
|
||||
if(body == null) return;
|
||||
String methodReference =
|
||||
LambdaCanBeMethodReferenceInspection.convertToMethodReference(body, parameters, lambda.getFunctionalInterfaceType(), lambda);
|
||||
if(methodReference != null) {
|
||||
lambda.replace(factory.createExpressionFromText(methodReference, lambda));
|
||||
} else {
|
||||
if(LambdaCanBeMethodReferenceInspection.replaceLambdaWithMethodReference(factory, lambda) == lambda) {
|
||||
PsiParameter parameter = parameters[0];
|
||||
String name = parameter.getName();
|
||||
String nameCandidate = null;
|
||||
|
||||
+64
-51
@@ -23,7 +23,6 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.openapi.util.Conditions;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.impl.source.resolve.graphInference.FunctionalInterfaceParameterizationUtil;
|
||||
@@ -34,6 +33,7 @@ import com.intellij.refactoring.util.RefactoringChangeUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -46,8 +46,6 @@ import java.util.Map;
|
||||
* User: anna
|
||||
*/
|
||||
public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
private static final String SHORT_NAME = "Convert2MethodRef";
|
||||
|
||||
public static final Logger LOG = Logger.getInstance("#" + LambdaCanBeMethodReferenceInspection.class.getName());
|
||||
|
||||
public boolean REPLACE_INSTANCEOF;
|
||||
@@ -58,18 +56,12 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
|
||||
@Override
|
||||
public JComponent createOptionsPanel() {
|
||||
final MultipleCheckboxOptionsPanel panel = new MultipleCheckboxOptionsPanel(this);
|
||||
panel.addCheckbox("Replace instanceof", "REPLACE_INSTANCEOF");
|
||||
panel.addCheckbox("Replace cast", "REPLACE_CAST");
|
||||
panel.addCheckbox("Replace null-check", "REPLACE_NULL_CHECK");
|
||||
panel.addCheckbox("Replace instanceof with MyClass.class::isInstance", "REPLACE_INSTANCEOF");
|
||||
panel.addCheckbox("Replace cast with MyClass.class::cast", "REPLACE_CAST");
|
||||
panel.addCheckbox("Replace null-check with Objects::nonNull or Objects::isNull", "REPLACE_NULL_CHECK");
|
||||
return panel;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
static LambdaCanBeMethodReferenceInspection getInstance(@NotNull PsiElement element) {
|
||||
final InspectionProfile inspectionProfile = InspectionProjectProfileManager.getInstance(element.getProject()).getCurrentProfile();
|
||||
return (LambdaCanBeMethodReferenceInspection)inspectionProfile.getUnwrappedTool(SHORT_NAME, element);
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -106,7 +98,8 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
|
||||
final PsiElement body = expression.getBody();
|
||||
final PsiType functionalInterfaceType = expression.getFunctionalInterfaceType();
|
||||
if (functionalInterfaceType != null) {
|
||||
final PsiExpression candidate = canBeMethodReferenceProblem(body, expression.getParameterList().getParameters(), functionalInterfaceType);
|
||||
final PsiExpression candidate =
|
||||
canBeMethodReferenceProblem(body, expression.getParameterList().getParameters(), functionalInterfaceType, null);
|
||||
if (candidate != null) {
|
||||
holder.registerProblem(candidate,
|
||||
"Can be replaced with method reference",
|
||||
@@ -123,23 +116,17 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
|
||||
final PsiVariable[] parameters,
|
||||
final PsiType functionalInterfaceType,
|
||||
@Nullable PsiElement context) {
|
||||
final PsiExpression candidate = canBeMethodReferenceProblem(body, parameters, functionalInterfaceType, context);
|
||||
final PsiExpression candidate = new LambdaCanBeMethodReferenceInspection()
|
||||
.canBeMethodReferenceProblem(body, parameters, functionalInterfaceType, context);
|
||||
return createMethodReferenceText(candidate, functionalInterfaceType, parameters);
|
||||
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiExpression canBeMethodReferenceProblem(@Nullable final PsiElement body,
|
||||
final PsiVariable[] parameters,
|
||||
final PsiType functionalInterfaceType) {
|
||||
return canBeMethodReferenceProblem(body, parameters, functionalInterfaceType, null);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiExpression canBeMethodReferenceProblem(@Nullable final PsiElement body,
|
||||
final PsiVariable[] parameters,
|
||||
PsiType functionalInterfaceType,
|
||||
@Nullable PsiElement context) {
|
||||
public PsiExpression canBeMethodReferenceProblem(@Nullable final PsiElement body,
|
||||
final PsiVariable[] parameters,
|
||||
PsiType functionalInterfaceType,
|
||||
@Nullable PsiElement context) {
|
||||
final PsiExpression methodRefCandidate = extractMethodReferenceCandidateExpression(body);
|
||||
if (methodRefCandidate instanceof PsiNewExpression) {
|
||||
final PsiNewExpression newExpression = (PsiNewExpression)methodRefCandidate;
|
||||
@@ -268,15 +255,18 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
|
||||
return resolvesToParameter(qualifier, parameters[0]);
|
||||
}
|
||||
|
||||
@Contract("null, _ -> false")
|
||||
private static boolean resolvesToParameter(PsiExpression expression, PsiVariable parameter) {
|
||||
return expression instanceof PsiReferenceExpression && ((PsiReferenceExpression)expression).resolve() == parameter;
|
||||
}
|
||||
|
||||
@Contract("null -> false")
|
||||
static boolean isNull(PsiElement element) {
|
||||
return element instanceof PsiLiteralExpression && ((PsiLiteralExpression)element).getValue() == null;
|
||||
}
|
||||
|
||||
public static PsiExpression extractMethodReferenceCandidateExpression(PsiElement body) {
|
||||
@Nullable
|
||||
private PsiExpression extractMethodReferenceCandidateExpression(PsiElement body) {
|
||||
final PsiExpression expression = LambdaUtil.extractSingleExpressionFromBody(body);
|
||||
if (expression == null) {
|
||||
return null;
|
||||
@@ -291,37 +281,60 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
LambdaCanBeMethodReferenceInspection instance = getInstance(expression);
|
||||
if(instance != null) {
|
||||
if (expression instanceof PsiInstanceOfExpression && instance.REPLACE_INSTANCEOF) {
|
||||
return expression;
|
||||
}
|
||||
else if (expression instanceof PsiBinaryExpression && instance.REPLACE_NULL_CHECK) {
|
||||
IElementType tokenType = ((PsiBinaryExpression)expression).getOperationTokenType();
|
||||
if (JavaTokenType.EQEQ.equals(tokenType) || JavaTokenType.NE.equals(tokenType)) {
|
||||
if (isNull(((PsiBinaryExpression)expression).getLOperand()) ||
|
||||
isNull(((PsiBinaryExpression)expression).getROperand())) {
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (expression instanceof PsiTypeCastExpression && instance.REPLACE_CAST) {
|
||||
PsiTypeElement typeElement = ((PsiTypeCastExpression)expression).getCastType();
|
||||
if (typeElement != null) {
|
||||
PsiJavaCodeReferenceElement refs = typeElement.getInnermostComponentReferenceElement();
|
||||
if (refs != null && refs.getParameterList() != null && refs.getParameterList().getTypeParameterElements().length != 0) {
|
||||
return null;
|
||||
}
|
||||
PsiType type = typeElement.getType();
|
||||
if (type instanceof PsiPrimitiveType)
|
||||
return null;
|
||||
if (expression instanceof PsiInstanceOfExpression && REPLACE_INSTANCEOF) {
|
||||
return expression;
|
||||
}
|
||||
else if (expression instanceof PsiBinaryExpression && REPLACE_NULL_CHECK) {
|
||||
IElementType tokenType = ((PsiBinaryExpression)expression).getOperationTokenType();
|
||||
if (JavaTokenType.EQEQ.equals(tokenType) || JavaTokenType.NE.equals(tokenType)) {
|
||||
if (isNull(((PsiBinaryExpression)expression).getLOperand()) ||
|
||||
isNull(((PsiBinaryExpression)expression).getROperand())) {
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (expression instanceof PsiTypeCastExpression && REPLACE_CAST) {
|
||||
PsiTypeElement typeElement = ((PsiTypeCastExpression)expression).getCastType();
|
||||
if (typeElement != null) {
|
||||
PsiJavaCodeReferenceElement refs = typeElement.getInnermostComponentReferenceElement();
|
||||
if (refs != null && refs.getParameterList() != null && refs.getParameterList().getTypeParameterElements().length != 0) {
|
||||
return null;
|
||||
}
|
||||
PsiType type = typeElement.getType();
|
||||
if (type instanceof PsiPrimitiveType)
|
||||
return null;
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void replaceAllLambdasWithMethodReferences(PsiElement root) {
|
||||
Collection<PsiLambdaExpression> lambdas = PsiTreeUtil.findChildrenOfType(root, PsiLambdaExpression.class);
|
||||
if(!lambdas.isEmpty()) {
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(root.getProject());
|
||||
for(PsiLambdaExpression lambda : lambdas) {
|
||||
replaceLambdaWithMethodReference(factory, lambda);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static PsiExpression replaceLambdaWithMethodReference(PsiElementFactory factory, PsiLambdaExpression lambda) {
|
||||
PsiType type = lambda.getFunctionalInterfaceType();
|
||||
if(type == null) return lambda;
|
||||
String methodReference =
|
||||
convertToMethodReference(lambda.getBody(), lambda.getParameterList().getParameters(), type, lambda);
|
||||
if(methodReference == null) return lambda;
|
||||
PsiTypeCastExpression replacement = (PsiTypeCastExpression)lambda.replace(
|
||||
factory.createExpressionFromText("(" + lambda.getFunctionalInterfaceType().getCanonicalText() + ")" + methodReference, lambda));
|
||||
if (RedundantCastUtil.isCastRedundant(replacement)) {
|
||||
final PsiExpression operand = replacement.getOperand();
|
||||
LOG.assertTrue(operand != null);
|
||||
return (PsiExpression)replacement.replace(operand);
|
||||
}
|
||||
return replacement;
|
||||
}
|
||||
|
||||
private static boolean checkQualifier(PsiElement qualifier) {
|
||||
if (qualifier == null) {
|
||||
return true;
|
||||
@@ -383,7 +396,7 @@ public class LambdaCanBeMethodReferenceInspection extends BaseJavaBatchLocalInsp
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String createMethodReferenceText(final PsiElement element,
|
||||
static String createMethodReferenceText(final PsiElement element,
|
||||
final PsiType functionalInterfaceType,
|
||||
final PsiVariable[] parameters) {
|
||||
if (element instanceof PsiMethodCallExpression) {
|
||||
|
||||
+18
-89
@@ -37,7 +37,10 @@ import com.intellij.psi.controlFlow.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.IntArrayList;
|
||||
@@ -496,10 +499,9 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
|
||||
private static boolean isTrivial(PsiStatement body, PsiParameter parameter) {
|
||||
//method reference
|
||||
final PsiExpression candidate = LambdaCanBeMethodReferenceInspection
|
||||
final PsiExpression candidate = new LambdaCanBeMethodReferenceInspection()
|
||||
.canBeMethodReferenceProblem(body instanceof PsiBlockStatement ? ((PsiBlockStatement)body).getCodeBlock() : body,
|
||||
new PsiParameter[]{parameter},
|
||||
createDefaultConsumerType(parameter.getProject(), parameter));
|
||||
new PsiParameter[]{parameter}, createDefaultConsumerType(parameter.getProject(), parameter), null);
|
||||
if (!(candidate instanceof PsiCallExpression)) {
|
||||
return true;
|
||||
}
|
||||
@@ -515,29 +517,8 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
return mapperCall instanceof PsiReferenceExpression && ((PsiReferenceExpression)mapperCall).resolve() == variable;
|
||||
}
|
||||
|
||||
static String compoundLambdaOrMethodReference(PsiVariable variable,
|
||||
PsiExpression expression,
|
||||
String samQualifiedName,
|
||||
PsiType[] samParamTypes) {
|
||||
String result = "";
|
||||
final Project project = variable.getProject();
|
||||
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
|
||||
final PsiClass functionClass = psiFacade.findClass(samQualifiedName, expression.getResolveScope());
|
||||
for (int i = 0; i < samParamTypes.length; i++) {
|
||||
if (samParamTypes[i] instanceof PsiPrimitiveType) {
|
||||
samParamTypes[i] = ((PsiPrimitiveType)samParamTypes[i]).getBoxedType(expression);
|
||||
}
|
||||
}
|
||||
final PsiClassType functionalInterfaceType = functionClass != null ? psiFacade.getElementFactory().createType(functionClass, samParamTypes) : null;
|
||||
final PsiVariable[] parameters = {variable};
|
||||
String methodReferenceText = LambdaCanBeMethodReferenceInspection.convertToMethodReference(expression, parameters, functionalInterfaceType, null);
|
||||
if (methodReferenceText != null) {
|
||||
LOG.assertTrue(functionalInterfaceType != null);
|
||||
result += "(" + functionalInterfaceType.getCanonicalText() + ")" + methodReferenceText;
|
||||
} else {
|
||||
result += variable.getName() + " -> " + expression.getText();
|
||||
}
|
||||
return result;
|
||||
static String createLambda(PsiVariable variable, PsiExpression expression) {
|
||||
return variable.getName() + " -> " + expression.getText();
|
||||
}
|
||||
|
||||
private static abstract class MigrateToStreamFix implements LocalQuickFix {
|
||||
@@ -598,24 +579,10 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
|
||||
static void simplifyAndFormat(@NotNull Project project, PsiElement result) {
|
||||
if(result == null) return;
|
||||
simplifyRedundantCast(result);
|
||||
LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result);
|
||||
CodeStyleManager.getInstance(project).reformat(JavaCodeStyleManager.getInstance(project).shortenClassReferences(result));
|
||||
}
|
||||
|
||||
static void simplifyRedundantCast(PsiElement result) {
|
||||
for (PsiMethodReferenceExpression methodReferenceExpression : PsiTreeUtil
|
||||
.findChildrenOfType(result, PsiMethodReferenceExpression.class)) {
|
||||
final PsiElement parent = methodReferenceExpression.getParent();
|
||||
if (parent instanceof PsiTypeCastExpression) {
|
||||
if (RedundantCastUtil.isCastRedundant((PsiTypeCastExpression)parent)) {
|
||||
final PsiExpression operand = ((PsiTypeCastExpression)parent).getOperand();
|
||||
LOG.assertTrue(operand != null);
|
||||
parent.replace(operand);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void restoreComments(PsiForeachStatement foreachStatement, PsiStatement body) {
|
||||
final PsiElement parent = foreachStatement.getParent();
|
||||
for (PsiElement comment : PsiTreeUtil.findChildrenOfType(body, PsiComment.class)) {
|
||||
@@ -678,8 +645,9 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
|
||||
buffer.append(".").append(myForEachMethodName).append("(");
|
||||
|
||||
final String functionalExpressionText = createForEachFunctionalExpressionText(project, block, tb.getVariable());
|
||||
PsiExpressionStatement callStatement = (PsiExpressionStatement)elementFactory.createStatementFromText(buffer.toString() + functionalExpressionText + ");", foreachStatement);
|
||||
final String functionalExpressionText = tb.getVariable().getName() + " -> " + wrapInBlock(block);
|
||||
PsiExpressionStatement callStatement = (PsiExpressionStatement)elementFactory
|
||||
.createStatementFromText(buffer.toString() + functionalExpressionText + ");", foreachStatement);
|
||||
callStatement = (PsiExpressionStatement)foreachStatement.replace(callStatement);
|
||||
|
||||
final PsiExpressionList argumentList = ((PsiCallExpression)callStatement.getExpression()).getArgumentList();
|
||||
@@ -696,20 +664,6 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
simplifyAndFormat(project, callStatement);
|
||||
}
|
||||
|
||||
private static String createForEachFunctionalExpressionText(Project project, PsiElement block, PsiVariable variable) {
|
||||
final PsiExpression methodRefCandidate = LambdaCanBeMethodReferenceInspection.extractMethodReferenceCandidateExpression(block);
|
||||
if (methodRefCandidate != null) {
|
||||
final PsiClassType functionalType = createDefaultConsumerType(project, variable);
|
||||
final PsiVariable[] parameters = {variable};
|
||||
String methodReferenceText =
|
||||
LambdaCanBeMethodReferenceInspection.convertToMethodReference(block, parameters, functionalType, null);
|
||||
if (methodReferenceText != null) {
|
||||
return methodReferenceText;
|
||||
}
|
||||
}
|
||||
return variable.getName() + " -> " + wrapInBlock(block);
|
||||
}
|
||||
|
||||
private static String wrapInBlock(PsiElement block) {
|
||||
if(block instanceof PsiExpressionStatement) {
|
||||
return ((PsiExpressionStatement)block).getExpression().getText();
|
||||
@@ -813,15 +767,6 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
final String callText =
|
||||
builder.append(".forEach(").append(varName).append("->").append(forEachBody.getText()).append(");").toString();
|
||||
PsiElement result = foreachStatement.replace(elementFactory.createStatementFromText(callText, foreachStatement));
|
||||
PsiLambdaExpression lambda =
|
||||
(PsiLambdaExpression)((PsiMethodCallExpression)((PsiExpressionStatement)result).getExpression()).getArgumentList()
|
||||
.getExpressions()[0];
|
||||
String methodReference =
|
||||
LambdaCanBeMethodReferenceInspection.convertToMethodReference(lambda.getBody(), lambda.getParameterList().getParameters(),
|
||||
lambda.getFunctionalInterfaceType(), lambda);
|
||||
if(methodReference != null) {
|
||||
lambda.replace(elementFactory.createExpressionFromText(methodReference, lambda));
|
||||
}
|
||||
simplifyAndFormat(project, result);
|
||||
}
|
||||
|
||||
@@ -927,13 +872,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
if(lambdaBody instanceof PsiExpression && BoolUtils.isNegation((PsiExpression)lambdaBody)) {
|
||||
PsiExpression negated = BoolUtils.getNegated((PsiExpression)lambdaBody);
|
||||
LOG.assertTrue(negated != null, lambdaBody.getText());
|
||||
String methodReferenceText = LambdaCanBeMethodReferenceInspection
|
||||
.convertToMethodReference(negated, lambda.getParameterList().getParameters(), lambda.getFunctionalInterfaceType(), lambda);
|
||||
if(methodReferenceText != null) {
|
||||
lambda.replace(elementFactory.createExpressionFromText(methodReferenceText, lambda));
|
||||
} else {
|
||||
lambdaBody.replace(negated);
|
||||
}
|
||||
lambdaBody.replace(negated);
|
||||
methodName = "allMatch";
|
||||
}
|
||||
}
|
||||
@@ -989,9 +928,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
else {
|
||||
typeName = "Int";
|
||||
}
|
||||
intermediateOps.add(".mapTo" + typeName + "(" +
|
||||
compoundLambdaOrMethodReference(tb.getVariable(), addend, "java.util.function.To" + typeName + "Function",
|
||||
new PsiType[]{tb.getVariable().getType()})+")");
|
||||
intermediateOps.add(".mapTo" + typeName + "(" + createLambda(tb.getVariable(), addend) + ")");
|
||||
final StringBuilder builder = generateStream(iteratedValue, intermediateOps);
|
||||
builder.append(".sum()");
|
||||
replaceWithNumericAddition(project, foreachStatement, var, builder, typeName.toLowerCase(Locale.ENGLISH));
|
||||
@@ -1041,9 +978,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
public String createReplacement(PsiElementFactory factory) {
|
||||
PsiExpression expression =
|
||||
myNegated ? factory.createExpressionFromText(BoolUtils.getNegatedExpressionText(myExpression), myExpression) : myExpression;
|
||||
return ".filter(" + compoundLambdaOrMethodReference(myVariable, expression,
|
||||
"java.util.function.Predicate",
|
||||
new PsiType[] {myVariable.getType()}) + ")";
|
||||
return ".filter(" + createLambda(myVariable, expression) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1054,9 +989,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
|
||||
@Override
|
||||
public String createReplacement(PsiElementFactory factory) {
|
||||
return ".map(" + compoundLambdaOrMethodReference(myVariable, myExpression,
|
||||
"java.util.function.Function",
|
||||
new PsiType[] {myVariable.getType(), myExpression.getType()}) + ")";
|
||||
return ".map(" + createLambda(myVariable, myExpression) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1068,9 +1001,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
@Override
|
||||
public String createReplacement(PsiElementFactory factory) {
|
||||
PsiExpression replacement = factory.createExpressionFromText(myExpression.getText() + ".stream()", myExpression);
|
||||
return ".flatMap(" + compoundLambdaOrMethodReference(myVariable, replacement,
|
||||
"java.util.function.Function",
|
||||
new PsiType[] {myVariable.getType(), replacement.getType()}) + ")";
|
||||
return ".flatMap(" + createLambda(myVariable, replacement) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1082,9 +1013,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
|
||||
@Override
|
||||
public String createReplacement(PsiElementFactory factory) {
|
||||
PsiExpression replacement = factory.createExpressionFromText("java.util.Arrays.stream("+myExpression.getText() + ")", myExpression);
|
||||
return ".flatMap(" + compoundLambdaOrMethodReference(myVariable, replacement,
|
||||
"java.util.function.Function",
|
||||
new PsiType[] {myVariable.getType(), replacement.getType()}) + ")";
|
||||
return ".flatMap(" + createLambda(myVariable, replacement) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-14
@@ -121,8 +121,9 @@ public class IntroduceParameterDialog extends RefactoringDialog {
|
||||
|
||||
|
||||
|
||||
@NotNull
|
||||
private String getParameterName() {
|
||||
return myParameterNameField.getEnteredName().trim();
|
||||
return myParameterNameField.getEnteredName().trim();
|
||||
}
|
||||
|
||||
public JComponent getPreferredFocusedComponent() {
|
||||
@@ -187,11 +188,7 @@ public class IntroduceParameterDialog extends RefactoringDialog {
|
||||
gbConstraints.weightx = 1;
|
||||
gbConstraints.fill = GridBagConstraints.BOTH;
|
||||
panel.add(myParameterNameField.getComponent(), gbConstraints);
|
||||
myParameterNameChangedListener = new NameSuggestionsField.DataChanged() {
|
||||
public void dataChanged() {
|
||||
validateButtons();
|
||||
}
|
||||
};
|
||||
myParameterNameChangedListener = () -> validateButtons();
|
||||
myParameterNameField.addDataChangedListener(myParameterNameChangedListener);
|
||||
|
||||
myNameSuggestionsManager =
|
||||
@@ -288,12 +285,8 @@ public class IntroduceParameterDialog extends RefactoringDialog {
|
||||
if (myCbCollapseToLambda.isVisible() && myCbCollapseToLambda.isSelected() && parameterInitializer != null) {
|
||||
PsiExpression lambda = AnonymousCanBeLambdaInspection.replaceAnonymousWithLambda(parameterInitializer, selectedType);
|
||||
if (lambda != null) {
|
||||
final PsiParameter[] lambdaParameters = ((PsiLambdaExpression)lambda).getParameterList().getParameters();
|
||||
final String methodReferenceText = LambdaCanBeMethodReferenceInspection.convertToMethodReference(((PsiLambdaExpression)lambda).getBody(), lambdaParameters, selectedType, null);
|
||||
if (methodReferenceText != null) {
|
||||
lambda = JavaPsiFacade.getElementFactory(getProject()).createExpressionFromText(methodReferenceText, lambda);
|
||||
}
|
||||
|
||||
lambda = LambdaCanBeMethodReferenceInspection.replaceLambdaWithMethodReference(JavaPsiFacade.getElementFactory(getProject()),
|
||||
(PsiLambdaExpression)lambda);
|
||||
processor.setParameterInitializer(lambda);
|
||||
}
|
||||
}
|
||||
@@ -313,8 +306,8 @@ public class IntroduceParameterDialog extends RefactoringDialog {
|
||||
@Override
|
||||
protected void canRun() throws ConfigurationException {
|
||||
String name = getParameterName();
|
||||
if (name == null || !PsiNameHelper.getInstance(myProject).isIdentifier(name)) {
|
||||
throw new ConfigurationException("\'" + (name != null ? name : "") + "\' is invalid parameter name");
|
||||
if (!PsiNameHelper.getInstance(myProject).isIdentifier(name)) {
|
||||
throw new ConfigurationException("\'" + name + "\' is invalid parameter name");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-3
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package com.intellij.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInspection.LambdaCanBeMethodReferenceInspection;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.StreamApiMigrationInspection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -26,8 +25,7 @@ public class StreamApiMigrationInspectionTest extends LightQuickFixParameterized
|
||||
@Override
|
||||
protected LocalInspectionTool[] configureLocalInspectionTools() {
|
||||
return new LocalInspectionTool[]{
|
||||
new StreamApiMigrationInspection(),
|
||||
new LambdaCanBeMethodReferenceInspection()
|
||||
new StreamApiMigrationInspection()
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user