From 68665950d5045b56af056d3a85aa8197f5db75dd Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Thu, 16 Nov 2023 13:43:39 +0100 Subject: [PATCH] Revert "[java-inspection] IDEA-314915 Suggest removing unnecessary stream().toList()" This reverts commit a4d4bb4fe0a1084ee63c49f8e5d767b8ad39bd13. GitOrigin-RevId: f342d8912b7e5a8a8b3217f1475e4a5034074e4c --- ...SimplifyStreamApiCallChainsInspection.java | 84 +------------------ .../afterDeleteStreamToList.java | 41 --------- .../beforeDeleteStreamToList.java | 41 --------- ...mApiCallChainsInspectionFixJava21Test.java | 40 --------- ...yStreamApiCallChainsInspectionFixTest.java | 16 +++- .../resources/messages/JavaBundle.properties | 2 - 6 files changed, 16 insertions(+), 208 deletions(-) delete mode 100644 java/java-tests/testData/inspection/streamApiCallChains21/afterDeleteStreamToList.java delete mode 100644 java/java-tests/testData/inspection/streamApiCallChains21/beforeDeleteStreamToList.java delete mode 100644 java/java-tests/testSrc/com/intellij/java/codeInspection/SimplifyStreamApiCallChainsInspectionFixJava21Test.java diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java b/java/java-impl-inspections/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java index b6261e7b2657..be3cd74b5162 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java @@ -87,8 +87,6 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal instanceCall(JAVA_UTIL_STREAM_BASE_STREAM, "map", "mapToInt", "mapToDouble", "mapToLong").parameterCount(1); private static final CallMatcher STREAM_MATCH = anyOf(STREAM_ANY_MATCH, STREAM_NONE_MATCH, STREAM_ALL_MATCH); - private static final CallMatcher STREAM_TO_LIST = instanceCall(JAVA_UTIL_STREAM_BASE_STREAM, "toList").parameterCount(0); - private static final CallMatcher COLLECTORS_TO_LIST = staticCall(JAVA_UTIL_STREAM_COLLECTORS, "toList", "toUnmodifiableList") .parameterCount(0); @@ -131,8 +129,7 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal CollectorToListSize.handler(), IterateTakeWhileFix.handler(), FilterAndMapUseSameMethodChainFix.handler(), - ReplaceWithOrElseThrowFix.handler(), - StreamToListChainFix.handler() + ReplaceWithOrElseThrowFix.handler() ).registerAll(SimplifyMatchNegationFix.handlers()); private static final Logger LOG = Logger.getInstance(SimplifyStreamApiCallChainsInspection.class); @@ -2196,85 +2193,6 @@ public class SimplifyStreamApiCallChainsInspection extends AbstractBaseJavaLocal } } - private static class StreamToListChainFix implements CallChainSimplification { - @NotNull - private final String myTermination; - - private StreamToListChainFix(@NotNull String termination) { myTermination = termination; } - - @Override - public String getName() { - return JavaBundle.message("simplify.stream.to.list.fix.name", myTermination); - } - - @Override - public String getMessage() { - return JavaBundle.message("simplify.stream.to.list.fix.message", myTermination); - } - - @Override - public boolean keepsStream() { - return false; - } - - @Override - public PsiElement simplify(PsiMethodCallExpression element) { - final CommentTracker ct = new CommentTracker(); - PsiMethodCallExpression qualifierCall = getQualifierMethodCall(element); - if (qualifierCall == null) { - return null; - } - PsiMethodCallExpression root = getQualifierMethodCall(qualifierCall); - if (root == null) { - return null; - } - return ct.replace(element, root); - } - - public static CallHandler handler() { - return CallHandler.of(anyOf(STREAM_TO_LIST) , call -> { - PsiMethodCallExpression qualifierCall = getQualifierMethodCall(call); - if (!COLLECTION_STREAM.test(qualifierCall)) { - return null; - } - String referenceName = call.getMethodExpression().getReferenceName(); - if (referenceName == null) { - return null; - } - - PsiType expectedTypeByParent = PsiTypesUtil.getExpectedTypeByParent(call); - PsiType erasuredParentType = TypeConversionUtil.erasure(expectedTypeByParent); - if (erasuredParentType != null && erasuredParentType.equalsToText(JAVA_LANG_ITERABLE)) { - return new StreamToListChainFix(referenceName); - } - PsiMethodCallExpression root = getQualifierMethodCall(qualifierCall); - if (root == null) { - return null; - } - if (root.getType() == null || !(call.getParent() instanceof PsiVariable variable && call.equals(variable.getInitializer()))) { - return null; - } - PsiType lType = variable.getType(); - if (!TypeConversionUtil.isAssignable(lType, root.getType()) || - !MismatchedCollectionQueryUpdateInspection.isUnmodified(call)) { - return null; - } - final PsiElement context = - PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class, PsiMethod.class, PsiLambdaExpression.class, - PsiCatchSection.class, PsiForStatement.class, PsiForeachStatement.class); - if (context == null) { - return null; - } - if (VariableAccessUtils.variableIsReturned(variable, context) || - VariableAccessUtils.variableIsPassedAsMethodArgument(variable, context)) { - return null; - } - return new StreamToListChainFix(referenceName); - }); - } - } - - private static class FilterAndMapUseSameMethodChainFix implements CallChainSimplification { @Nls diff --git a/java/java-tests/testData/inspection/streamApiCallChains21/afterDeleteStreamToList.java b/java/java-tests/testData/inspection/streamApiCallChains21/afterDeleteStreamToList.java deleted file mode 100644 index e0e67e28d184..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains21/afterDeleteStreamToList.java +++ /dev/null @@ -1,41 +0,0 @@ -// "Fix all 'Stream API call chain can be simplified' problems in file" "true" -import java.util.Collection; -import java.util.Map; -import java.util.stream.Collectors; - -public class Main { - - void testSimple(Map map) { - Iterable it = map.keySet(); - for (String s : it) { - System.out.println(s); - } - } - - void testCollection(Map map) { - Collection it = map.keySet(); - for (String s : it) { - System.out.println(s); - } - } - - Collection testReturnVariable(Map map) { - Collection it = map.keySet().stream().toList(); - for (String s : it) { - System.out.println(s); - } - return it; - } - - void testPassVariableAsParameter(Map map) { - Collection it = map.keySet().stream().toList(); - for (String s : it) { - System.out.println(s); - } - sink(it); - } - - private void sink(Collection it) { - - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains21/beforeDeleteStreamToList.java b/java/java-tests/testData/inspection/streamApiCallChains21/beforeDeleteStreamToList.java deleted file mode 100644 index 2aac5240f28b..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains21/beforeDeleteStreamToList.java +++ /dev/null @@ -1,41 +0,0 @@ -// "Fix all 'Stream API call chain can be simplified' problems in file" "true" -import java.util.Collection; -import java.util.Map; -import java.util.stream.Collectors; - -public class Main { - - void testSimple(Map map) { - Iterable it = map.keySet().stream().toList(); - for (String s : it) { - System.out.println(s); - } - } - - void testCollection(Map map) { - Collection it = map.keySet().stream().toList(); - for (String s : it) { - System.out.println(s); - } - } - - Collection testReturnVariable(Map map) { - Collection it = map.keySet().stream().toList(); - for (String s : it) { - System.out.println(s); - } - return it; - } - - void testPassVariableAsParameter(Map map) { - Collection it = map.keySet().stream().toList(); - for (String s : it) { - System.out.println(s); - } - sink(it); - } - - private void sink(Collection it) { - - } -} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/SimplifyStreamApiCallChainsInspectionFixJava21Test.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/SimplifyStreamApiCallChainsInspectionFixJava21Test.java deleted file mode 100644 index 965bc926989e..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/SimplifyStreamApiCallChainsInspectionFixJava21Test.java +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -package com.intellij.java.codeInspection; - -import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; -import com.intellij.codeInspection.LocalInspectionTool; -import com.intellij.codeInspection.SimplifyStreamApiCallChainsInspection; -import com.intellij.openapi.projectRoots.Sdk; -import com.intellij.pom.java.LanguageLevel; -import com.intellij.testFramework.IdeaTestUtil; -import com.intellij.testFramework.LightProjectDescriptor; -import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; -import org.jetbrains.annotations.NotNull; - -public class SimplifyStreamApiCallChainsInspectionFixJava21Test extends LightQuickFixParameterizedTestCase { - - @Override - protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() { - return new LocalInspectionTool[]{new SimplifyStreamApiCallChainsInspection()}; - } - - @Override - protected Sdk getProjectJDK() { - return IdeaTestUtil.getMockJdk21(); - } - - @Override - protected LanguageLevel getLanguageLevel() { - return LanguageLevel.JDK_21; - } - - @Override - protected @NotNull LightProjectDescriptor getProjectDescriptor() { - return LightJavaCodeInsightFixtureTestCase.JAVA_21; - } - - @Override - protected String getBasePath() { - return "/inspection/streamApiCallChains21"; - } -} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/SimplifyStreamApiCallChainsInspectionFixTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/SimplifyStreamApiCallChainsInspectionFixTest.java index 9366d4f87b60..98ff281ce215 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/SimplifyStreamApiCallChainsInspectionFixTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/SimplifyStreamApiCallChainsInspectionFixTest.java @@ -1,4 +1,18 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.intellij.java.codeInspection; import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; diff --git a/java/openapi/resources/messages/JavaBundle.properties b/java/openapi/resources/messages/JavaBundle.properties index c2097813f9ef..9676284921fb 100644 --- a/java/openapi/resources/messages/JavaBundle.properties +++ b/java/openapi/resources/messages/JavaBundle.properties @@ -1468,8 +1468,6 @@ simplify.stream.replace.support.with.collection.fix.name=Replace with ''{0}.{1}( simplify.stream.replace.support.with.collection.fix.message=Can be replaced with ''{0}.{1}()'' call simplify.stream.swap.filter.and.map.fix.name=Swap 'filter()' and 'map()' simplify.stream.swap.filter.and.map.fix.message='filter()' and 'map()' can be swapped -simplify.stream.to.list.fix.name=Delete ''stream().{0}()'' -simplify.stream.to.list.fix.message=''stream().{0}()'' call can be deleted simplify.stream.inspection.iterate.take.while.fix.name=Replace with three-arg 'iterate()' simplify.stream.inspection.iterate.take.while.fix.message=Can be replaced with three-arg 'iterate()' side.effects.pattern.message=\n\nThere are possible side effects found in {0}
\nYou can:\n
\n-\\ Remove variable usages along with all expressions involved, or
\n-\\ Transform expressions assigned to variable into the statements on their own.
\n
\n That is,
\n \n \n \n \n
{1};
\n becomes:
\n \n \n \n \n
{2};
\n
\n\n