mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] Java8ListReplaceAllInspection (IDEA-270920)
GitOrigin-RevId: 1e79021fc4860398bc7bab047a5ec692e137d9dc
This commit is contained in:
committed by
intellij-monorepo-bot
parent
c18e93ed5e
commit
01cc4df97e
@@ -301,6 +301,8 @@ java.8.collection.removeif.inspection.description=The loop can be replaced with
|
||||
java.8.collection.removeif.inspection.fix.name=Replace the loop with 'Collection.removeIf'
|
||||
java.8.list.sort.inspection.description=Collections.sort could be replaced with List.sort
|
||||
java.8.list.sort.inspection.fix.name=Replace with List.sort
|
||||
java.8.list.replaceall.inspection.description=The loop can be replaced with 'List.replaceAll'
|
||||
java.8.list.replaceall.inspection.fix.name=Replace the loop with 'List.replaceAll'
|
||||
|
||||
wrap.with.optional.parameter.text=Wrap {0, choice, 1#1st|2#2nd|3#3rd|4#{0,number}th} argument using ''java.util.Optional''
|
||||
wrap.with.optional.single.parameter.text=Wrap using 'java.util.Optional'
|
||||
|
||||
@@ -1575,6 +1575,11 @@
|
||||
groupKey="group.names.language.level.specific.issues.and.migration.aids8" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="com.intellij.codeInspection.java18api.Java8CollectionRemoveIfInspection"
|
||||
key="inspection.java.8.collection.remove.if.display.name" bundle="messages.JavaBundle"/>
|
||||
<localInspection groupPathKey="group.path.names.java.language.level.specific.issues.and.migration.aids" language="JAVA" shortName="Java8ListReplaceAll"
|
||||
groupBundle="messages.InspectionsBundle"
|
||||
groupKey="group.names.language.level.specific.issues.and.migration.aids8" enabledByDefault="true" level="WARNING"
|
||||
implementationClass="com.intellij.codeInspection.java18api.Java8ListReplaceAllInspection"
|
||||
key="inspection.java.8.collection.remove.if.display.name" bundle="messages.JavaBundle"/>
|
||||
<localInspection groupPath="Java" language="JAVA" shortName="ExplicitArrayFilling"
|
||||
groupBundle="messages.InspectionsBundle"
|
||||
groupKey="group.names.verbose.or.redundant.code.constructs" enabledByDefault="true" level="WARNING"
|
||||
|
||||
+6
-2
@@ -163,8 +163,12 @@ public class UseBulkOperationInspection extends AbstractBaseJavaLocalInspectionT
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiExpression findIterableForIndexedLoop(PsiForStatement loop, PsiExpression getElementExpression) {
|
||||
CountingLoop countingLoop = CountingLoop.from(loop);
|
||||
public static PsiExpression findIterableForIndexedLoop(PsiForStatement loop, PsiExpression getElementExpression) {
|
||||
return findIterableForIndexedLoop(CountingLoop.from(loop), getElementExpression);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiExpression findIterableForIndexedLoop(CountingLoop countingLoop, PsiExpression getElementExpression) {
|
||||
if (countingLoop == null ||
|
||||
countingLoop.isIncluding() ||
|
||||
countingLoop.isDescending() ||
|
||||
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInspection.java18api;
|
||||
|
||||
import com.intellij.codeInsight.PsiEquivalenceUtil;
|
||||
import com.intellij.codeInsight.daemon.QuickFixBundle;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.codeInspection.bulkOperation.UseBulkOperationInspection;
|
||||
import com.intellij.codeInspection.util.IteratorDeclaration;
|
||||
import com.intellij.codeInspection.util.LambdaGenerationUtil;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Ref;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.VariableKind;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.util.InlineUtil;
|
||||
import com.intellij.refactoring.util.LambdaRefactoringUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.siyeh.ig.callMatcher.CallMatcher;
|
||||
import com.siyeh.ig.psiutils.*;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class Java8ListReplaceAllInspection extends AbstractBaseJavaLocalInspectionTool {
|
||||
private static final CallMatcher LIST_SET = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_LIST, "set").parameterTypes("int", "E");
|
||||
private static final CallMatcher LIST_GET = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_LIST, "get").parameterTypes("int");
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
|
||||
return new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitMethodCallExpression(PsiMethodCallExpression call) {
|
||||
PsiForStatement forStatement = PsiTreeUtil.getParentOfType(call, PsiForStatement.class);
|
||||
if (forStatement == null) return;
|
||||
PsiJavaToken endToken = forStatement.getRParenth();
|
||||
if (endToken == null) return;
|
||||
PsiExpression qualifier = PsiUtil.skipParenthesizedExprDown(ExpressionUtils.getEffectiveQualifier(call.getMethodExpression()));
|
||||
if (qualifier == null) return;
|
||||
PsiExpression iterable = findIterable(call);
|
||||
if (iterable == null) return;
|
||||
if (!PsiEquivalenceUtil.areElementsEquivalent(qualifier, iterable) &&
|
||||
!(qualifier instanceof PsiQualifiedExpression && iterable instanceof PsiQualifiedExpression)) {
|
||||
return;
|
||||
}
|
||||
holder.registerProblem(forStatement, new TextRange(0, endToken.getTextOffset() - forStatement.getTextOffset() + 1),
|
||||
QuickFixBundle.message("java.8.list.replaceall.inspection.description"),
|
||||
new ReplaceWithReplaceAllQuickFix(call));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiExpression findIterable(PsiMethodCallExpression call) {
|
||||
if (!LIST_SET.test(call)) return null;
|
||||
PsiForStatement forStatement = PsiTreeUtil.getParentOfType(call, PsiForStatement.class);
|
||||
if (forStatement == null) return null;
|
||||
PsiStatement body = forStatement.getBody();
|
||||
if (body == null) return null;
|
||||
PsiStatement lastStatement = ArrayUtil.getLastElement(ControlFlowUtils.unwrapBlock(body));
|
||||
PsiElement parent = RefactoringUtil.getParentStatement(call, false);
|
||||
if (parent == null) return null;
|
||||
if (!PsiEquivalenceUtil.areElementsEquivalent(lastStatement, parent)) return null;
|
||||
CountingLoop loop = CountingLoop.from(forStatement);
|
||||
if (loop == null || !ExpressionUtils.isReferenceTo(call.getArgumentList().getExpressions()[0], loop.getCounter())) return null;
|
||||
Predicate<PsiVariable> variableAllowedPredicate = variable -> PsiEquivalenceUtil.areElementsEquivalent(variable, loop.getCounter());
|
||||
if (!LambdaGenerationUtil.canBeUncheckedLambda(forStatement.getBody(), variableAllowedPredicate)) return null;
|
||||
PsiMethodCallExpression listGetCall = getListGetCall(body);
|
||||
if (listGetCall == null) return null;
|
||||
Ref<Integer> counter = new Ref<>(0);
|
||||
PsiTreeUtil.processElements(body, e -> {
|
||||
if (ExpressionUtils.isReferenceTo(ObjectUtils.tryCast(e, PsiExpression.class), loop.getCounter())) {
|
||||
counter.set(counter.get() + 1);
|
||||
}
|
||||
return counter.get() <= 2;
|
||||
});
|
||||
if (counter.get() != 2) return null;
|
||||
return UseBulkOperationInspection.findIterableForIndexedLoop(loop, listGetCall);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiMethodCallExpression getListGetCall(@NotNull PsiStatement body) {
|
||||
Ref<PsiMethodCallExpression> getElementExpression = new Ref<>();
|
||||
boolean isSoleGelElementExpression = PsiTreeUtil.processElements(body, e -> {
|
||||
PsiMethodCallExpression maybeListGet = ObjectUtils.tryCast(e, PsiMethodCallExpression.class);
|
||||
return !LIST_GET.test(maybeListGet) || getElementExpression.setIfNull(maybeListGet);
|
||||
}) && !getElementExpression.isNull();
|
||||
if (!isSoleGelElementExpression) return null;
|
||||
return getElementExpression.get();
|
||||
}
|
||||
|
||||
private static class ReplaceWithReplaceAllQuickFix implements LocalQuickFix {
|
||||
private final SmartPsiElementPointer<PsiMethodCallExpression> myCallPointer;
|
||||
|
||||
private ReplaceWithReplaceAllQuickFix(@NotNull PsiMethodCallExpression call) {
|
||||
SmartPointerManager manager = SmartPointerManager.getInstance(call.getProject());
|
||||
myCallPointer = manager.createSmartPsiElementPointer(call);
|
||||
}
|
||||
|
||||
@Nls
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return QuickFixBundle.message("java.8.list.replaceall.inspection.fix.name");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
PsiMethodCallExpression call = myCallPointer.getElement();
|
||||
if (call == null) return;
|
||||
PsiExpression qualifier = ExpressionUtils.getEffectiveQualifier(call.getMethodExpression());
|
||||
if (qualifier == null) return;
|
||||
PsiExpression iterable = findIterable(call);
|
||||
if (iterable == null) return;
|
||||
PsiElement parent = RefactoringUtil.getParentStatement(iterable, false);
|
||||
if (parent == null) return;
|
||||
CommentTracker ct = new CommentTracker();
|
||||
String bulkMethodParameterText = calculateReplaceAllLambdaExpressionText(call, ct);
|
||||
String text = ct.text(qualifier) + ".replaceAll(" + bulkMethodParameterText + ");";
|
||||
PsiElement result = ct.replaceAndRestoreComments(parent, text);
|
||||
LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result);
|
||||
simplifyToExpressionLambda(result);
|
||||
result = JavaCodeStyleManager.getInstance(project).shortenClassReferences(result);
|
||||
CodeStyleManager.getInstance(project).reformat(result);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String calculateReplaceAllLambdaExpressionText(PsiMethodCallExpression listSetCall, CommentTracker ct) {
|
||||
PsiForStatement forStatement = PsiTreeUtil.getParentOfType(listSetCall, PsiForStatement.class);
|
||||
if (forStatement == null) return null;
|
||||
PsiStatement body = forStatement.getBody();
|
||||
if (body == null) return null;
|
||||
PsiMethodCallExpression listGetCall = getListGetCall(body);
|
||||
if (listGetCall == null) return null;
|
||||
PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(listGetCall, PsiDeclarationStatement.class);
|
||||
PsiLocalVariable var = IteratorDeclaration.getDeclaredVariable(declarationStatement);
|
||||
String paramName;
|
||||
if (var != null && var.getInitializer() == listGetCall) {
|
||||
paramName = var.getName();
|
||||
new CommentTracker().deleteAndRestoreComments(declarationStatement);
|
||||
}
|
||||
else {
|
||||
paramName = new VariableNameGenerator(body, VariableKind.PARAMETER).byExpression(listGetCall).generate(true);
|
||||
PsiElement element = new CommentTracker().replaceAndRestoreComments(listGetCall, paramName);
|
||||
PsiLocalVariable variable =
|
||||
IteratorDeclaration.getDeclaredVariable(PsiTreeUtil.getParentOfType(element, PsiDeclarationStatement.class));
|
||||
inlineVariable(variable);
|
||||
}
|
||||
String text = "return " + ct.textWithComments(listSetCall.getArgumentList().getExpressions()[1]) + ";";
|
||||
PsiElement result = new CommentTracker().replaceAndRestoreComments(listSetCall.getParent(), text);
|
||||
String codeBlockText = body instanceof PsiBlockStatement ? ct.text(body) : "{ " + ct.text(result) + " }";
|
||||
return paramName + " -> " + codeBlockText;
|
||||
}
|
||||
|
||||
private static void inlineVariable(@Nullable PsiLocalVariable variable) {
|
||||
if (variable == null) return;
|
||||
final Collection<PsiReference> references = ReferencesSearch.search(variable).findAll();
|
||||
PsiExpression initializer = variable.getInitializer();
|
||||
if (initializer == null || references.size() != 1) return;
|
||||
InlineUtil.inlineVariable(variable, initializer, (PsiJavaCodeReferenceElement)references.iterator().next());
|
||||
variable.delete();
|
||||
}
|
||||
|
||||
private static void simplifyToExpressionLambda(@NotNull PsiElement element) {
|
||||
PsiExpressionStatement expressionStatement = ObjectUtils.tryCast(element, PsiExpressionStatement.class);
|
||||
if (expressionStatement == null) return;
|
||||
PsiMethodCallExpression call = ObjectUtils.tryCast(expressionStatement.getExpression(), PsiMethodCallExpression.class);
|
||||
if (call == null) return;
|
||||
PsiExpression arg = ArrayUtil.getFirstElement(call.getArgumentList().getExpressions());
|
||||
PsiLambdaExpression lambdaExpression = ObjectUtils.tryCast(arg, PsiLambdaExpression.class);
|
||||
if (lambdaExpression == null) return;
|
||||
LambdaRefactoringUtil.simplifyToExpressionLambda(lambdaExpression);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,7 +73,7 @@ public final class IteratorDeclaration extends IterableTraversal {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiLocalVariable getDeclaredVariable(PsiStatement statement) {
|
||||
public static PsiLocalVariable getDeclaredVariable(PsiStatement statement) {
|
||||
if (!(statement instanceof PsiDeclarationStatement)) return null;
|
||||
PsiDeclarationStatement declaration = (PsiDeclarationStatement)statement;
|
||||
PsiElement[] elements = declaration.getDeclaredElements();
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<html>
|
||||
<body>
|
||||
Reports loops which can be collapsed into a single <code>Liar.replaceAll</code> call.
|
||||
<p>Example:</p>
|
||||
<pre><code>
|
||||
for (int i = 0; i < strings.size(); i++) {
|
||||
String str = strings.get(i).toLowerCase();
|
||||
strings.set(i, str);
|
||||
}
|
||||
</code></pre>
|
||||
<p>After the quick-fix is applied:</p>
|
||||
<pre><code>
|
||||
strings.replaceAll(String::toLowerCase);
|
||||
</code></pre>
|
||||
<!-- tooltip end -->
|
||||
<p>
|
||||
This inspection only reports if the language level of the project or module is 8 or higher.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(s -> s.trim().toLowerCase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(String::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(this::modifyString);
|
||||
}
|
||||
|
||||
String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(this::modifyString);
|
||||
}
|
||||
|
||||
String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(String::trim);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(String::toLowerCase);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(Main::modifyString);
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(e -> e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(e -> e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
strings.replaceAll(Main::modifyString);
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Main extends ArrayList<String> {
|
||||
void modifyStrings(List<String> strings) {
|
||||
super.replaceAll(Main::modifyString);
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Main extends ArrayList<String> {
|
||||
void modifyStrings(List<String> strings) {
|
||||
this.replaceAll(Main::modifyString);
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
if (Math.random() > 0.5) break;
|
||||
strings.set(i, strings.get(i).toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, strings.get(i).trim().toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
class Main extends ArrayList<String> {
|
||||
void modifyStrings(List<String> strings) throws IOException {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, modifyString(strings.get(i)));
|
||||
}
|
||||
}
|
||||
|
||||
static String modifyString(String str) throws IOException {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, new String(strings.get(i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
if (Math.random() > 0.5) continue;
|
||||
strings.set(i, strings.get(i).toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
int j = 0;
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, strings.get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
int j = 0;
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(j, strings.get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings1, List<String> strings2) {
|
||||
for (int i = 0; i < strings1.size(); i++)<caret> {
|
||||
strings2.set(i, strings1.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings1, List<String> strings2) {
|
||||
for (int i = 0; i < strings1.size(); i++)<caret> {
|
||||
strings1.set(i, strings2.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret>
|
||||
strings.set(i, modifyString(strings.get(i)));
|
||||
}
|
||||
|
||||
String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, modifyString(strings.get(i)));
|
||||
}
|
||||
}
|
||||
|
||||
String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, strings.get(i).trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
String str = strings.get(i).toLowerCase();
|
||||
strings.set(i, str);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
String str = strings.get(i);
|
||||
strings.set(i, modifyString(str));
|
||||
}
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, strings.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, strings.get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
int j = 0;
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
j++;
|
||||
strings.set(i, modifyString(strings.get(i)));
|
||||
}
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
if (Math.random() > 0.5) return;
|
||||
strings.set(i, strings.get(i).toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "false"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, strings.get(i).toLowerCase());
|
||||
System.out.println("bar");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.*;
|
||||
|
||||
class Main {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < strings.size(); i++)<caret> {
|
||||
strings.set(i, modifyString(strings.get(i)));
|
||||
}
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Main extends ArrayList<String> {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < super.size(); i++)<caret> {
|
||||
super.set(i, modifyString(super.get(i)));
|
||||
}
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Replace the loop with 'List.replaceAll'" "true"
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class Main extends ArrayList<String> {
|
||||
void modifyStrings(List<String> strings) {
|
||||
for (int i = 0; i < this.size(); i++)<caret> {
|
||||
this.set(i, modifyString(this.get(i)));
|
||||
}
|
||||
}
|
||||
|
||||
static String modifyString(String str) {
|
||||
return str.repeat(2);
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.codeInspection;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.java18api.Java8ListReplaceAllInspection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class Java8ListReplaceAllInspectionTest extends LightQuickFixParameterizedTestCase {
|
||||
@Override
|
||||
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
|
||||
return new LocalInspectionTool[]{new Java8ListReplaceAllInspection()};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/inspection/java8ListReplaceAll";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user