diff --git a/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java b/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java index 580158c006a4..f1e9bd5af462 100644 --- a/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java +++ b/java/java-impl/src/com/intellij/codeInspection/streamMigration/CollectMigration.java @@ -323,8 +323,8 @@ class CollectMigration extends BaseStreamApiMigration { else { PsiExpression copy = JavaPsiFacade.getElementFactory(initializer.getProject()) .createExpressionFromText(ct.text(initializer), initializer); - if (copy instanceof PsiNewExpression) { - PsiExpressionList argumentList = ((PsiNewExpression)copy).getArgumentList(); + if (copy instanceof PsiCallExpression && ConstructionUtils.isPrepopulatedCollectionInitializer(copy)) { + PsiExpressionList argumentList = ((PsiCallExpression)copy).getArgumentList(); if (argumentList != null) { PsiExpression arg = ArrayUtil.getFirstElement(argumentList.getExpressions()); if (arg != null && !(arg.getType() instanceof PsiPrimitiveType)) { @@ -850,7 +850,7 @@ class CollectMigration extends BaseStreamApiMigration { NewListTerminal(CollectTerminal upstream, PsiLocalVariable variable, String intermediate, - PsiNewExpression newListExpression, + PsiCallExpression newListExpression, PsiType resultType) { super(upstream, variable, intermediate, newListExpression); myResultType = resultType; @@ -863,8 +863,11 @@ class CollectMigration extends BaseStreamApiMigration { @Override StreamEx fusedElements() { - PsiJavaCodeReferenceElement reference = ((PsiNewExpression)myCreateExpression).getClassReference(); - return myUpstream.fusedElements().append(Objects.requireNonNull(reference).getReferenceName()); + if (myCreateExpression instanceof PsiNewExpression) { + PsiJavaCodeReferenceElement reference = ((PsiNewExpression)myCreateExpression).getClassReference(); + return myUpstream.fusedElements().append(Objects.requireNonNull(reference).getReferenceName()); + } + return myUpstream.fusedElements().append(((PsiMethodCallExpression)myCreateExpression).getMethodExpression().getReferenceName()); } @Nullable @@ -875,14 +878,19 @@ class CollectMigration extends BaseStreamApiMigration { WrapperCandidate candidate = WrapperCandidate.tryExtract(terminal, element); if (candidate == null) return null; - if (!(candidate.myCandidate instanceof PsiNewExpression)) return null; - if (!InheritanceUtil.isInheritor(candidate.myType, CommonClassNames.JAVA_UTIL_COLLECTION)) return null; - PsiNewExpression newExpression = (PsiNewExpression)candidate.myCandidate; - PsiExpressionList argumentList = newExpression.getArgumentList(); + if (!(candidate.myCandidate instanceof PsiCallExpression)) return null; + PsiClass targetClass = PsiUtil.resolveClassInClassTypeOnly(candidate.myCandidate.getType()); + if (!InheritanceUtil.isInheritor(targetClass, CommonClassNames.JAVA_UTIL_COLLECTION)) return null; + PsiCallExpression callExpression = (PsiCallExpression)candidate.myCandidate; + if (!ConstructionUtils.isPrepopulatedCollectionInitializer(callExpression)) return null; + if (CommonClassNames.JAVA_UTIL_HASH_SET.equals(targetClass.getQualifiedName()) && intermediateSteps.equals(".distinct()")) { + intermediateSteps = ""; + } + PsiExpressionList argumentList = callExpression.getArgumentList(); if (argumentList == null) return null; PsiExpression[] args = argumentList.getExpressions(); if (args.length != 1 || !terminal.isTargetReference(args[0])) return null; - return new NewListTerminal(terminal, candidate.myVar, intermediateSteps, newExpression, candidate.myType); + return new NewListTerminal(terminal, candidate.myVar, intermediateSteps, callExpression, candidate.myType); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/afterHashSetToSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/afterHashSetToSet.java new file mode 100644 index 000000000000..403ccff161fd --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/afterHashSetToSet.java @@ -0,0 +1,9 @@ +// "Fuse HashSet into the Stream API chain" "true" +import java.util.*; +import java.util.stream.*; + +class X { + void foo(Stream s) { + Set set = s.collect(Collectors.toSet()); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/afterSetsNewHashSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/afterSetsNewHashSet.java new file mode 100644 index 000000000000..3e114febc6f8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/afterSetsNewHashSet.java @@ -0,0 +1,15 @@ +// "Fuse newHashSet into the Stream API chain" "true" +package com.google.common.collect; + +import java.util.*; +import java.util.stream.*; + +class X { + void foo(Stream s) { + Set set = s.collect(Collectors.toSet()); + } +} + +class Sets { + public static native HashSet newHashSet(Iterable var0); +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/beforeHashSetToSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/beforeHashSetToSet.java new file mode 100644 index 000000000000..62af7b7df3ca --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/beforeHashSetToSet.java @@ -0,0 +1,9 @@ +// "Fuse HashSet into the Stream API chain" "true" +import java.util.*; +import java.util.stream.*; + +class X { + void foo(Stream s) { + Set set = new HashSet<>(s.collect(Collectors.toSet())); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/beforeSetsNewHashSet.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/beforeSetsNewHashSet.java new file mode 100644 index 000000000000..fb5cbe857634 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/fuseStreamOperations/beforeSetsNewHashSet.java @@ -0,0 +1,15 @@ +// "Fuse newHashSet into the Stream API chain" "true" +package com.google.common.collect; + +import java.util.*; +import java.util.stream.*; + +class X { + void foo(Stream s) { + Set set = Sets.newHashSet(s.collect(Collectors.toSet())); + } +} + +class Sets { + public static native HashSet newHashSet(Iterable var0); +} \ No newline at end of file