diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ImmutableCollectionModelUtils.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ImmutableCollectionModelUtils.java index b3a3ccc87476..a4e12a9a0283 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ImmutableCollectionModelUtils.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/collections/ImmutableCollectionModelUtils.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.siyeh.ipp.collections; import com.intellij.codeInsight.BlockUtils; @@ -14,6 +14,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer; import com.intellij.util.ObjectUtils; +import com.intellij.util.containers.ContainerUtil; import com.siyeh.ig.PsiReplacementUtil; import com.siyeh.ig.callMatcher.CallMapper; import com.siyeh.ig.callMatcher.CallMatcher; @@ -40,7 +41,6 @@ final class ImmutableCollectionModelUtils { if (type == null) return null; if (!CodeBlockSurrounder.canSurround(call)) return null; String assignedVariable = getAssignedVariable(call); - PsiExpression[] args = call.getArgumentList().getExpressions(); PsiMethod method = call.resolveMethod(); if (method == null) return null; PsiClassType classType = ObjectUtils.tryCast(call.getType(), PsiClassType.class); @@ -50,7 +50,8 @@ final class ImmutableCollectionModelUtils { .map(PsiUtil::resolveClassInClassTypeOnly) .anyMatch(aClass -> isNonResolvedTypeParameter(aClass, call, resolveHelper)); if (hasNonResolvedTypeParams) return null; - if ("ofEntries".equals(method.getName()) && Arrays.stream(args).anyMatch(arg -> extractPutArgs(arg) == null)) return null; + PsiExpression[] args = call.getArgumentList().getExpressions(); + if ("ofEntries".equals(method.getName()) && ContainerUtil.exists(args, arg -> extractPutArgs(arg) == null)) return null; return new ImmutableCollectionModel(call, type, method, assignedVariable); } @@ -179,8 +180,8 @@ final class ImmutableCollectionModelUtils { String assignedVariable = model.myAssignedVariable; if (assignedVariable != null) { String initializerText = model.myType.getInitializerText(model.myIsVarArgCall ? null : call.getText()); - PsiReplacementUtil.replaceExpressionAndShorten(call, initializerText, new CommentTracker()); PsiElement anchor = addUpdates(assignedVariable, model, statement); + PsiReplacementUtil.replaceExpressionAndShorten(call, initializerText, new CommentTracker()); if (myEditor != null) myEditor.getCaretModel().moveToOffset(anchor.getTextRange().getEndOffset()); } else { @@ -250,7 +251,7 @@ final class ImmutableCollectionModelUtils { private static List createUpdates(@NotNull String name, @NotNull ImmutableCollectionModel model) { boolean isMapOfEntriesCall = "ofEntries".equals(model.myCall.getMethodExpression().getReferenceName()); List updates = new ArrayList<>(); - PsiExpression[] args = model.myArgs; + PsiExpression[] args = model.myCall.getArgumentList().getExpressions(); for (int i = 0; i < args.length; i++) { PsiExpression arg = args[i]; if (model.myType != CollectionType.MAP) { @@ -297,7 +298,6 @@ final class ImmutableCollectionModelUtils { private PsiMethodCallExpression myCall; private final CollectionType myType; private final boolean myIsVarArgCall; - private final PsiExpression[] myArgs; private final String myAssignedVariable; @Contract(pure = true) @@ -308,7 +308,6 @@ final class ImmutableCollectionModelUtils { myCall = call; myType = type; myIsVarArgCall = !method.isVarArgs() || MethodCallUtils.isVarArgCall(call); - myArgs = call.getArgumentList().getExpressions(); myAssignedVariable = assignedVariable; } } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/collections/to_mutable_collection/MapOfEntriesNested.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/collections/to_mutable_collection/MapOfEntriesNested.java new file mode 100644 index 000000000000..2e85237377ce --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/collections/to_mutable_collection/MapOfEntriesNested.java @@ -0,0 +1,9 @@ +import java.util.*; +import static java.util.Map.entry; + +class MapOfEntriesNested { + Map BINDINGS = Map.ofEntries( + entry(1, "one"), + entry(2, "two") + ); +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/collections/to_mutable_collection/MapOfEntriesNested_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/collections/to_mutable_collection/MapOfEntriesNested_after.java new file mode 100644 index 000000000000..91444fa43497 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/collections/to_mutable_collection/MapOfEntriesNested_after.java @@ -0,0 +1,12 @@ +import java.util.*; +import static java.util.Map.entry; + +class MapOfEntriesNested { + Map BINDINGS; + + { + BINDINGS = new HashMap<>(); + BINDINGS.put(1, "one"); + BINDINGS.put(2, "two"); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/collections/ReplaceWithMutableCollectionIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/collections/ReplaceWithMutableCollectionIntentionTest.java index 5d73654c04db..bb93cc99d372 100644 --- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/collections/ReplaceWithMutableCollectionIntentionTest.java +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/collections/ReplaceWithMutableCollectionIntentionTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.siyeh.ipp.collections; import com.intellij.testFramework.LightProjectDescriptor; @@ -45,6 +45,7 @@ public class ReplaceWithMutableCollectionIntentionTest extends IPPTestCase { public void testMapOf() { doTest(); } public void testMapOfEntries() { doTest(); } + public void testMapOfEntriesNested() { doTest(); } public void testListOf() { doTest(); } public void testLambdaExpr() { doTest(); } public void testSingletonMap() { doTest(); }