diff --git a/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java b/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java index b9189b3d3441..d6bfcb984e31 100644 --- a/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/SimplifyStreamApiCallChainsInspection.java @@ -17,7 +17,6 @@ package com.intellij.codeInspection; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Key; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; @@ -41,7 +40,10 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.text.MessageFormat; -import java.util.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.function.Function; import java.util.stream.Stream; @@ -463,49 +465,13 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns if (ctor == null || !ctor.getModifierList().hasExplicitModifier(PsiModifier.PUBLIC)) return false; PsiParameterList list = ctor.getParameterList(); if (list.getParametersCount() != 1) return false; - PsiParameter parameter = list.getParameters()[0]; - PsiTypeElement typeElement = parameter.getTypeElement(); + PsiTypeElement typeElement = list.getParameters()[0].getTypeElement(); if (typeElement == null) return false; PsiType type = typeElement.getType(); PsiClass aClass = PsiUtil.resolveClassInClassTypeOnly(type); - if(aClass == null || !CommonClassNames.JAVA_UTIL_COLLECTION.equals(aClass.getQualifiedName())) return false; - PsiClass curClass = ctor.getContainingClass(); - LOG.assertTrue(curClass != null); - // Do not perform deep check for standard Collections - if(Objects.requireNonNull(curClass.getQualifiedName()).startsWith("java.util.")) return true; - PsiSubstitutor substitutor = TypeConversionUtil.getSuperClassSubstitutor(aClass, curClass, PsiSubstitutor.EMPTY); - PsiElementFactory factory = JavaPsiFacade.getElementFactory(ctor.getProject()); - PsiClassType collectionType = factory.createType(aClass, substitutor); - if (!(type.isAssignableFrom(collectionType))) return false; - // Check that body either contains addAll(arg) or super(arg) - return !PsiTreeUtil.processElements(ctor.getBody(), e -> { - if(e instanceof PsiMethodCallExpression) { - PsiMethodCallExpression call = (PsiMethodCallExpression)e; - PsiExpression[] args = call.getArgumentList().getExpressions(); - if(args.length == 1 && ExpressionUtils.isReferenceTo(args[0], parameter)) { - PsiReferenceExpression methodExpression = call.getMethodExpression(); - if("super".equals(methodExpression.getReferenceName())) { - return !isCollectionConstructor(call.resolveMethod()); - } - if("addAll".equals(methodExpression.getReferenceName())) { - PsiExpression qualifier = methodExpression.getQualifierExpression(); - if(qualifier == null || qualifier instanceof PsiThisExpression) { - return false; - } - } - } - } - return true; - }); + return aClass != null && CommonClassNames.JAVA_UTIL_COLLECTION.equals(aClass.getQualifiedName()); } - private static final Key> HAS_COLLECTION_CONSTRUCTOR = Key.create("HasCollectionConstructor"); - private static final ParameterizedCachedValueProvider HAS_COLLECTION_CONSTRUCTOR_PROVIDER = psiClass -> { - boolean hasCollectionConstructor = - Stream.of(psiClass.getConstructors()).anyMatch(SimplifyStreamApiCallChainsInspection::isCollectionConstructor); - return CachedValueProvider.Result.create(hasCollectionConstructor, psiClass); - }; - @Nullable private static String collectorToCollection(PsiMethodCallExpression call) { PsiMethod method = call.resolveMethod(); @@ -525,10 +491,12 @@ public class SimplifyStreamApiCallChainsInspection extends BaseJavaBatchLocalIns PsiMethod ctor = (PsiMethod)element; if(ctor.getParameterList().getParametersCount() == 0) { PsiClass aClass = ctor.getContainingClass(); - if (aClass != null && - CachedValuesManager.getManager(aClass.getProject()) - .getParameterizedCachedValue(aClass, HAS_COLLECTION_CONSTRUCTOR, HAS_COLLECTION_CONSTRUCTOR_PROVIDER, false, aClass)) { - return aClass.getQualifiedName(); + if (aClass != null) { + String name = aClass.getQualifiedName(); + if(name != null && name.startsWith("java.util.") && + Stream.of(aClass.getConstructors()).anyMatch(SimplifyStreamApiCallChainsInspection::isCollectionConstructor)) { + return name; + } } } } diff --git a/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeAddAll.java b/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeAddAll.java deleted file mode 100644 index 4fb25baaf003..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeAddAll.java +++ /dev/null @@ -1,18 +0,0 @@ -// "Replace with 'Test.MyType' constructor" "true" - -import java.util.*; -import java.util.stream.*; - -class Test { - static class MyType extends ArrayList { - public MyType() {} - - public MyType(Collection coll) { - super(coll); - } - } - - public static void test(List s) { - new MyType(s).contains("abc"); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeAddAllCtor.java b/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeAddAllCtor.java deleted file mode 100644 index 610c7c27cee6..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeAddAllCtor.java +++ /dev/null @@ -1,18 +0,0 @@ -// "Replace with 'Test.MyType' constructor" "true" - -import java.util.*; -import java.util.stream.*; - -class Test { - static class MyType extends ArrayList { - public MyType() {} - - public MyType(Collection coll) { - this.addAll(coll); - } - } - - public static void test(List s) { - new MyType(s).contains("abc"); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeGeneric.java b/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeGeneric.java deleted file mode 100644 index 8b73ffd5cef4..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains/afterStreamToCollectionMyTypeGeneric.java +++ /dev/null @@ -1,18 +0,0 @@ -// "Replace with 'Test.MyType' constructor" "true" - -import java.util.*; -import java.util.stream.*; - -class Test { - static class MyType extends ArrayList { - public MyType() {} - - public MyType(Collection coll) { - super(coll); - } - } - - public static void testMy(List s) { - new MyType(s).contains("abc"); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyType.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyType.java deleted file mode 100644 index 70e4bdfba45b..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyType.java +++ /dev/null @@ -1,14 +0,0 @@ -// "Replace with 'Test.MyType' constructor" "false" - -import java.util.*; -import java.util.stream.*; - -class Test { - static class MyType extends ArrayList { - - } - - public static void test(List s) { - s.stream().collect(Collectors.toCollection(MyType::new)).contains("abc"); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAll.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAll.java index 770e70aa5ed4..e17abadf148c 100644 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAll.java +++ b/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAll.java @@ -1,4 +1,4 @@ -// "Replace with 'Test.MyType' constructor" "true" +// "Replace with 'Test.MyType' constructor" "false" import java.util.*; import java.util.stream.*; diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAllCtor.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAllCtor.java deleted file mode 100644 index 8cf3c518c408..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAllCtor.java +++ /dev/null @@ -1,18 +0,0 @@ -// "Replace with 'Test.MyType' constructor" "true" - -import java.util.*; -import java.util.stream.*; - -class Test { - static class MyType extends ArrayList { - public MyType() {} - - public MyType(Collection coll) { - this.addAll(coll); - } - } - - public static void test(List s) { - s.stream().collect(Collectors.toCollection(MyType::new)).contains("abc"); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAllPrivate.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAllPrivate.java deleted file mode 100644 index 30ff25be3cee..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeAddAllPrivate.java +++ /dev/null @@ -1,18 +0,0 @@ -// "Replace with 'Test.MyType' constructor" "false" - -import java.util.*; -import java.util.stream.*; - -class Test { - static class MyType extends ArrayList { - public MyType() {} - - private MyType(Collection coll) { - super(coll); - } - } - - public static void test(List s) { - s.stream().collect(Collectors.toCollection(MyType::new)).contains("abc"); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeEmptyCtor.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeEmptyCtor.java deleted file mode 100644 index cd00f309e4fb..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeEmptyCtor.java +++ /dev/null @@ -1,17 +0,0 @@ -// "Replace with 'Test.MyType' constructor" "false" - -import java.util.*; -import java.util.stream.*; - -class Test { - static class MyType extends ArrayList { - public MyType() {} - - public MyType(Collection coll) { - } - } - - public static void test(List s) { - s.stream().collect(Collectors.toCollection(MyType::new)).contains("abc"); - } -} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeGeneric.java b/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeGeneric.java deleted file mode 100644 index 8b207a2fe13b..000000000000 --- a/java/java-tests/testData/inspection/streamApiCallChains/beforeStreamToCollectionMyTypeGeneric.java +++ /dev/null @@ -1,18 +0,0 @@ -// "Replace with 'Test.MyType' constructor" "true" - -import java.util.*; -import java.util.stream.*; - -class Test { - static class MyType extends ArrayList { - public MyType() {} - - public MyType(Collection coll) { - super(coll); - } - } - - public static void testMy(List s) { - s.stream().collect(Collectors.toCollection(MyType::new)).contains("abc"); - } -} \ No newline at end of file