Revert "[java-inspection] IDEA-314915 Suggest removing unnecessary stream().toList()"

This reverts commit a4d4bb4fe0a1084ee63c49f8e5d767b8ad39bd13.

GitOrigin-RevId: f342d8912b7e5a8a8b3217f1475e4a5034074e4c
This commit is contained in:
Mikhail Pyltsin
2023-11-17 11:27:37 +00:00
committed by intellij-monorepo-bot
parent 076b33952b
commit 68665950d5
6 changed files with 16 additions and 208 deletions
@@ -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<CallChainSimplification> 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
@@ -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<String, Integer> map) {
Iterable<String> it = map.keySet();
for (String s : it) {
System.out.println(s);
}
}
void testCollection(Map<String, Integer> map) {
Collection<String> it = map.keySet();
for (String s : it) {
System.out.println(s);
}
}
Collection<String> testReturnVariable(Map<String, Integer> map) {
Collection<String> it = map.keySet().stream().toList();
for (String s : it) {
System.out.println(s);
}
return it;
}
void testPassVariableAsParameter(Map<String, Integer> map) {
Collection<String> it = map.keySet().stream().toList();
for (String s : it) {
System.out.println(s);
}
sink(it);
}
private void sink(Collection<String> it) {
}
}
@@ -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<String, Integer> map) {
Iterable<String> it = map.keySet().stream().toLi<caret>st();
for (String s : it) {
System.out.println(s);
}
}
void testCollection(Map<String, Integer> map) {
Collection<String> it = map.keySet().stream().toList();
for (String s : it) {
System.out.println(s);
}
}
Collection<String> testReturnVariable(Map<String, Integer> map) {
Collection<String> it = map.keySet().stream().toList();
for (String s : it) {
System.out.println(s);
}
return it;
}
void testPassVariableAsParameter(Map<String, Integer> map) {
Collection<String> it = map.keySet().stream().toList();
for (String s : it) {
System.out.println(s);
}
sink(it);
}
private void sink(Collection<String> it) {
}
}
@@ -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";
}
}
@@ -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;
@@ -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=<html>\n<body>\nThere are possible side effects found in {0}<br>\nYou can:\n<br>\n-\\&nbsp;<b>Remove</b> variable usages along with all expressions involved, or<br>\n-\\&nbsp;<b>Transform</b> expressions assigned to variable into the statements on their own.<br>\n<div style="padding-left: 0.6cm;">\n That is,<br>\n <table border="0">\n <tr>\n <td><code>{1};</code></td>\n </tr>\n </table>\n becomes: <br>\n <table border="0">\n <tr>\n <td><code>{2};</code></td>\n </tr>\n </table>\n</div>\n</body>\n</html>