mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
SimplifyStreamApiCallChainsInspection: use List.of instead of Arrays.asList where possible
Fixes second part of IDEA-194872 Suggest conversion "Arrays.toList" to "List.of" when list not modified
This commit is contained in:
+13
-3
@@ -6,6 +6,7 @@ import com.intellij.codeInsight.Nullability;
|
||||
import com.intellij.codeInsight.NullableNotNullManager;
|
||||
import com.intellij.codeInsight.intention.impl.StreamRefactoringUtil;
|
||||
import com.intellij.codeInspection.dataFlow.DfaUtil;
|
||||
import com.intellij.codeInspection.dataFlow.NullabilityUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
@@ -35,6 +36,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.intellij.psi.CommonClassNames.*;
|
||||
import static com.intellij.util.ObjectUtils.tryCast;
|
||||
@@ -1622,12 +1624,12 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Replace with Arrays.asList().contains()";
|
||||
return "Replace with List.contains()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "Can be replaced with Arrays.asList().contains()";
|
||||
return "Can be replaced with List.contains()";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1645,7 +1647,15 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal
|
||||
CommentTracker ct = new CommentTracker();
|
||||
PsiReferenceParameterList typeParameters = qualifierCall.getMethodExpression().getParameterList();
|
||||
String typeParametersText = typeParameters == null ? "" : ct.text(typeParameters);
|
||||
PsiElement result = ct.replaceAndRestoreComments(call, JAVA_UTIL_ARRAYS + "." + typeParametersText + "asList" +
|
||||
String factory;
|
||||
if (PsiUtil.isLanguageLevel9OrHigher(call) && MethodCallUtils.isVarArgCall(qualifierCall) &&
|
||||
Stream.of(qualifierArgs.getExpressions())
|
||||
.allMatch(e -> NullabilityUtil.getExpressionNullability(e, true) == Nullability.NOT_NULL)) {
|
||||
factory = JAVA_UTIL_LIST + "." + typeParametersText + "of";
|
||||
} else {
|
||||
factory = JAVA_UTIL_ARRAYS + "." + typeParametersText + "asList";
|
||||
}
|
||||
PsiElement result = ct.replaceAndRestoreComments(call, factory +
|
||||
ct.text(qualifierArgs) + ".contains(" + ct.text(value) + ")");
|
||||
return JavaCodeStyleManager.getInstance(result.getProject()).shortenClassReferences(result);
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Replace with Arrays.asList().contains()" "true"
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Replace with Arrays.asList().contains()" "true"
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Replace with Arrays.asList().contains()" "true"
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
+3
-3
@@ -1,10 +1,10 @@
|
||||
// "Replace with Arrays.asList().contains()" "true"
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public boolean find(String key) {
|
||||
return Arrays.asList("foo", "bar", "baz").contains(key);
|
||||
return List.of("foo", "bar", "baz").contains(key);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public boolean find(String key, String addVal) {
|
||||
return Arrays.asList("foo", "bar", "baz", addVal).contains(key);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Replace with Arrays.asList().contains()" "true"
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Replace with Arrays.asList().contains()" "true"
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Replace with Arrays.asList().contains()" "true"
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Replace with Arrays.asList().contains()" "true"
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Replace with List.contains()" "true"
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public boolean find(String key, String addVal) {
|
||||
return Stream.of("foo", "bar", "baz", addVal).anyMat<caret>ch(d -> key.equals(d));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user