quick fix batch application: getWorkingQuickFix uses QuickFix#getFamilyName to find suitable fixes (IDEA-155841)

This commit is contained in:
Dmitry Batkovich
2016-07-26 11:19:10 +03:00
parent 734701f51d
commit 261d017030
2 changed files with 10 additions and 33 deletions
@@ -22,7 +22,6 @@ import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
@@ -129,10 +128,10 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
}
else if (REPLACE_TRIVIAL_FOREACH || !isTrivial(body, statement.getIterationParameter())) {
final List<LocalQuickFix> fixes = new ArrayList<LocalQuickFix>();
fixes.add(new ReplaceWithForeachFix());
fixes.add(new ReplaceWithForeachCallFix("forEach"));
if (extractIfStatement(body) != null) {
//for .stream()
fixes.add(new ReplaceWithForeachOrderedFix());
fixes.add(new ReplaceWithForeachCallFix("forEachOrdered"));
}
holder.registerProblem(iteratedValue, "Can be replaced with foreach call",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
@@ -285,22 +284,12 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
return mapperCall instanceof PsiReferenceExpression && ((PsiReferenceExpression)mapperCall).resolve() == parameter;
}
private static class ReplaceWithForeachFix extends ReplaceWithForeachCallFix {
@Override
protected String getForEachMethodName() {
return "forEach";
}
}
private static class ReplaceWithForeachCallFix implements LocalQuickFix {
private final String myForEachMethodName;
private static class ReplaceWithForeachOrderedFix extends ReplaceWithForeachCallFix {
@Override
protected String getForEachMethodName() {
return "forEachOrdered";
protected ReplaceWithForeachCallFix(String forEachMethodName) {
myForEachMethodName = forEachMethodName;
}
}
private static abstract class ReplaceWithForeachCallFix implements LocalQuickFix {
protected abstract String getForEachMethodName();
@NotNull
@Override
@@ -311,7 +300,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
@NotNull
@Override
public String getFamilyName() {
return "Replace with " + getForEachMethodName();
return "Replace with " + myForEachMethodName;
}
@Override
@@ -336,7 +325,7 @@ public class StreamApiMigrationInspection extends BaseJavaBatchLocalInspectionTo
body = thenBranch;
}
buffer.append(".").append(getForEachMethodName()).append("(");
buffer.append(".").append(myForEachMethodName).append("(");
final String functionalExpressionText = createForEachFunctionalExpressionText(project, body, parameter);
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
@@ -61,26 +61,14 @@ public class LocalQuickFixWrapper extends QuickFixAction {
@Nullable
private QuickFix getWorkingQuickFix(@NotNull QuickFix[] fixes) {
final QuickFix exactResult = getWorkingQuickFix(fixes, true);
return exactResult != null ? exactResult : getWorkingQuickFix(fixes, false);
}
@Nullable
private QuickFix getWorkingQuickFix(@NotNull QuickFix[] fixes, boolean exact) {
for (QuickFix fix : fixes) {
if (!checkFix(exact, myFix, fix)) continue;
if (myFix instanceof IntentionWrapper && fix instanceof IntentionWrapper) {
if (!checkFix(exact, ((IntentionWrapper)myFix).getAction(), ((IntentionWrapper)fix).getAction())) continue;
if (fix.getFamilyName().equals(myFix.getFamilyName())) {
return fix;
}
return fix;
}
return null;
}
private static <T> boolean checkFix(boolean exact, T thisFix, T fix) {
return exact ? thisFix.getClass() == fix.getClass() : thisFix.getClass().isInstance(fix);
}
@Override
protected boolean applyFix(@NotNull RefEntity[] refElements) {
return true;