From b437d47f9f86066ef5bd60a86aefe75196372253 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Tue, 3 Jul 2018 11:14:24 +0700 Subject: [PATCH] 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 --- .../SimplifyStreamApiCallChainsInspection.java | 16 +++++++++++++--- .../afterArrayStreamAnyMatch.java | 2 +- .../afterArrayStreamAnyMatchLambda.java | 2 +- .../afterArrayStreamAnyMatchLambdaNullCheck.java | 2 +- .../afterArrayStreamAnyMatchVarArg.java | 6 +++--- .../afterArrayStreamAnyMatchVarArgNullable.java | 10 ++++++++++ .../beforeArrayStreamAnyMatch.java | 2 +- .../beforeArrayStreamAnyMatchLambda.java | 2 +- ...beforeArrayStreamAnyMatchLambdaNullCheck.java | 2 +- .../beforeArrayStreamAnyMatchVarArg.java | 2 +- .../beforeArrayStreamAnyMatchVarArgNullable.java | 9 +++++++++ 11 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchVarArgNullable.java create mode 100644 java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchVarArgNullable.java diff --git a/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java b/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java index 1d7353dc1d2e..4054f097f609 100644 --- a/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java @@ -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); } diff --git a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatch.java b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatch.java index ac6d8c55ed4c..776cbe899ee0 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatch.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatch.java @@ -1,4 +1,4 @@ -// "Replace with Arrays.asList().contains()" "true" +// "Replace with List.contains()" "true" import java.util.Arrays; diff --git a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchLambda.java b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchLambda.java index fbc168fece34..454226b35724 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchLambda.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchLambda.java @@ -1,4 +1,4 @@ -// "Replace with Arrays.asList().contains()" "true" +// "Replace with List.contains()" "true" import java.util.Arrays; import java.util.stream.Stream; diff --git a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchLambdaNullCheck.java b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchLambdaNullCheck.java index fbc168fece34..454226b35724 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchLambdaNullCheck.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchLambdaNullCheck.java @@ -1,4 +1,4 @@ -// "Replace with Arrays.asList().contains()" "true" +// "Replace with List.contains()" "true" import java.util.Arrays; import java.util.stream.Stream; diff --git a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchVarArg.java b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchVarArg.java index 07c227b37758..8bf89822b39a 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchVarArg.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchVarArg.java @@ -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); } } \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchVarArgNullable.java b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchVarArgNullable.java new file mode 100644 index 000000000000..a7958e3c3877 --- /dev/null +++ b/java/java-tests/testData/inspection/streamApiCallChains/afterArrayStreamAnyMatchVarArgNullable.java @@ -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); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatch.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatch.java index 813d6d471a50..18e88ca501ad 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatch.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatch.java @@ -1,4 +1,4 @@ -// "Replace with Arrays.asList().contains()" "true" +// "Replace with List.contains()" "true" import java.util.Arrays; diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchLambda.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchLambda.java index 344aec62d5c0..672bcc234ed5 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchLambda.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchLambda.java @@ -1,4 +1,4 @@ -// "Replace with Arrays.asList().contains()" "true" +// "Replace with List.contains()" "true" import java.util.stream.Stream; diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchLambdaNullCheck.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchLambdaNullCheck.java index 504404139929..e4030246d5e7 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchLambdaNullCheck.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchLambdaNullCheck.java @@ -1,4 +1,4 @@ -// "Replace with Arrays.asList().contains()" "true" +// "Replace with List.contains()" "true" import java.util.stream.Stream; diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchVarArg.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchVarArg.java index 85a4e8accbee..3d13a7cd2140 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchVarArg.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchVarArg.java @@ -1,4 +1,4 @@ -// "Replace with Arrays.asList().contains()" "true" +// "Replace with List.contains()" "true" import java.util.stream.Stream; diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchVarArgNullable.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchVarArgNullable.java new file mode 100644 index 000000000000..0f4019d742ec --- /dev/null +++ b/java/java-tests/testData/inspection/streamApiCallChains/beforeArrayStreamAnyMatchVarArgNullable.java @@ -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).anyMatch(d -> key.equals(d)); + } +} \ No newline at end of file