diff --git a/java/java-impl/src/META-INF/JavaPlugin.xml b/java/java-impl/src/META-INF/JavaPlugin.xml index dd47329822d9..6371a9c022b5 100644 --- a/java/java-impl/src/META-INF/JavaPlugin.xml +++ b/java/java-impl/src/META-INF/JavaPlugin.xml @@ -1413,11 +1413,11 @@ groupKey="group.names.language.level.specific.issues.and.migration.aids8" enabledByDefault="true" level="WARNING" implementationClass="com.intellij.codeInspection.java18api.Java8CollectionRemoveIfInspection" displayName="Loop can be replaced with Collection.removeIf()"/> - + groupKey="group.names.verbose.or.redundant.code.constructs" enabledByDefault="true" level="INFORMATION" + implementationClass="com.intellij.codeInspection.ExplicitArrayFillingInspection" + displayName="Explicit array filling"/> " + ct.text(rValue) + ");"; - PsiElement result = ct.replaceAndRestoreComments(statement, replacement); - LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); + PsiElement result; + if (myIsRhsConstant) { + String replacement = CommonClassNames.JAVA_UTIL_ARRAYS + ".fill(" + + ct.text(container.getQualifier()) + ", " + ct.text(rValue) + ");"; + result = ct.replaceAndRestoreComments(statement, replacement); + } + else { + String replacement = CommonClassNames.JAVA_UTIL_ARRAYS + ".setAll(" + + ct.text(container.getQualifier()) + ", " + loop.getCounter().getName() + "->" + ct.text(rValue) + ");"; + result = ct.replaceAndRestoreComments(statement, replacement); + LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); + } result = JavaCodeStyleManager.getInstance(project).shortenClassReferences(result); CodeStyleManager.getInstance(project).reformat(result); } diff --git a/java/java-impl/src/inspectionDescriptions/Java8ArraySetAll.html b/java/java-impl/src/inspectionDescriptions/ExplicitArrayFilling.html similarity index 66% rename from java/java-impl/src/inspectionDescriptions/Java8ArraySetAll.html rename to java/java-impl/src/inspectionDescriptions/ExplicitArrayFilling.html index 98b2a07e5015..f5033bf79eb5 100644 --- a/java/java-impl/src/inspectionDescriptions/Java8ArraySetAll.html +++ b/java/java-impl/src/inspectionDescriptions/ExplicitArrayFilling.html @@ -1,6 +1,6 @@ -Reports loops which could be replaced with Arrays.setAll() call. +Reports loops which could be replaced with Arrays.setAll() or Arrays.fill() calls.

For example:

   for (int i=0; i<array.length; i++) {
@@ -8,7 +8,6 @@ Reports loops which could be replaced with Arrays.setAll() call.
   }
 
-

This inspection only reports if the project or module is configured to use a language level of 8 or higher.

New in 2017.1

\ No newline at end of file diff --git a/java/java-tests/testData/inspection/explicitArrayFilling/afterConstantRhs.java b/java/java-tests/testData/inspection/explicitArrayFilling/afterConstantRhs.java new file mode 100644 index 000000000000..d785d05ba357 --- /dev/null +++ b/java/java-tests/testData/inspection/explicitArrayFilling/afterConstantRhs.java @@ -0,0 +1,14 @@ +// "Replace loop with 'Arrays.fill' method call" "true" +import java.util.Arrays; +import java.util.List; + +public class Test { + + public static void fill(T[] data, T value) { + /*comment*/ + /*in body*/ + /*in lvalue*/ + /*in rvalue*/ + Arrays.fill(data, value); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/java8ArraysSetAll/afterFieldComments.java b/java/java-tests/testData/inspection/explicitArrayFilling/afterFieldComments.java similarity index 80% rename from java/java-tests/testData/inspection/java8ArraysSetAll/afterFieldComments.java rename to java/java-tests/testData/inspection/explicitArrayFilling/afterFieldComments.java index c507775fcaea..f6244ae7b80c 100644 --- a/java/java-tests/testData/inspection/java8ArraysSetAll/afterFieldComments.java +++ b/java/java-tests/testData/inspection/explicitArrayFilling/afterFieldComments.java @@ -1,6 +1,6 @@ import java.util.Arrays; -// "Replace loop with Arrays.setAll" "true" +// "Replace loop with 'Arrays.setAll' method call" "true" public class Test { private Object[] data; diff --git a/java/java-tests/testData/inspection/java8ArraysSetAll/afterSimple.java b/java/java-tests/testData/inspection/explicitArrayFilling/afterSimple.java similarity index 80% rename from java/java-tests/testData/inspection/java8ArraysSetAll/afterSimple.java rename to java/java-tests/testData/inspection/explicitArrayFilling/afterSimple.java index 1430a3b0afdd..9b20af4876e8 100644 --- a/java/java-tests/testData/inspection/java8ArraysSetAll/afterSimple.java +++ b/java/java-tests/testData/inspection/explicitArrayFilling/afterSimple.java @@ -1,4 +1,4 @@ -// "Replace loop with Arrays.setAll" "true" +// "Replace loop with 'Arrays.setAll' method call" "true" import java.util.Arrays; import java.util.List; diff --git a/java/java-tests/testData/inspection/explicitArrayFilling/beforeConstantRhs.java b/java/java-tests/testData/inspection/explicitArrayFilling/beforeConstantRhs.java new file mode 100644 index 000000000000..e0498b478352 --- /dev/null +++ b/java/java-tests/testData/inspection/explicitArrayFilling/beforeConstantRhs.java @@ -0,0 +1,13 @@ +// "Replace loop with 'Arrays.fill' method call" "true" +import java.util.Arrays; +import java.util.List; + +public class Test { + + public static void fill(T[] data, T value) { + for(int idx = 0; (/*comment*/data).length > idx; idx+=1) { + /*in body*/ + data[/*in lvalue*/idx] = /*in rvalue*/value; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/java8ArraysSetAll/beforeFieldComments.java b/java/java-tests/testData/inspection/explicitArrayFilling/beforeFieldComments.java similarity index 82% rename from java/java-tests/testData/inspection/java8ArraysSetAll/beforeFieldComments.java rename to java/java-tests/testData/inspection/explicitArrayFilling/beforeFieldComments.java index a73a869dff7e..30c9ac4e1a16 100644 --- a/java/java-tests/testData/inspection/java8ArraysSetAll/beforeFieldComments.java +++ b/java/java-tests/testData/inspection/explicitArrayFilling/beforeFieldComments.java @@ -1,4 +1,4 @@ -// "Replace loop with Arrays.setAll" "true" +// "Replace loop with 'Arrays.setAll' method call" "true" public class Test { private Object[] data; diff --git a/java/java-tests/testData/inspection/java8ArraysSetAll/beforeSimple.java b/java/java-tests/testData/inspection/explicitArrayFilling/beforeSimple.java similarity index 82% rename from java/java-tests/testData/inspection/java8ArraysSetAll/beforeSimple.java rename to java/java-tests/testData/inspection/explicitArrayFilling/beforeSimple.java index ff1f4719ddcd..f9c9f323a844 100644 --- a/java/java-tests/testData/inspection/java8ArraysSetAll/beforeSimple.java +++ b/java/java-tests/testData/inspection/explicitArrayFilling/beforeSimple.java @@ -1,4 +1,4 @@ -// "Replace loop with Arrays.setAll" "true" +// "Replace loop with 'Arrays.setAll' method call" "true" import java.util.Arrays; import java.util.List; diff --git a/java/java-tests/testData/inspection/java8ArraysSetAll/beforeStartFrom1.java b/java/java-tests/testData/inspection/explicitArrayFilling/beforeStartFrom1.java similarity index 81% rename from java/java-tests/testData/inspection/java8ArraysSetAll/beforeStartFrom1.java rename to java/java-tests/testData/inspection/explicitArrayFilling/beforeStartFrom1.java index 66acdf8bf558..c458ac4ca798 100644 --- a/java/java-tests/testData/inspection/java8ArraysSetAll/beforeStartFrom1.java +++ b/java/java-tests/testData/inspection/explicitArrayFilling/beforeStartFrom1.java @@ -1,4 +1,4 @@ -// "Replace loop with Arrays.setAll" "false" +// "Fix all 'Explicit array filling' problems in file" "false" import java.util.Arrays; import java.util.List; diff --git a/java/java-tests/testData/inspection/java8ArraysSetAll/beforeUnsupportedType.java b/java/java-tests/testData/inspection/explicitArrayFilling/beforeUnsupportedType.java similarity index 81% rename from java/java-tests/testData/inspection/java8ArraysSetAll/beforeUnsupportedType.java rename to java/java-tests/testData/inspection/explicitArrayFilling/beforeUnsupportedType.java index 97df535ea14b..fb865d52a06c 100644 --- a/java/java-tests/testData/inspection/java8ArraysSetAll/beforeUnsupportedType.java +++ b/java/java-tests/testData/inspection/explicitArrayFilling/beforeUnsupportedType.java @@ -1,4 +1,4 @@ -// "Replace loop with Arrays.setAll" "false" +// "Fix all 'Explicit array filling' problems in file" "false" import java.util.Arrays; import java.util.List; diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/ExplicitArrayFillingInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/ExplicitArrayFillingInspectionTest.java new file mode 100644 index 000000000000..fe5273d2c406 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/ExplicitArrayFillingInspectionTest.java @@ -0,0 +1,20 @@ +// 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. +package com.intellij.java.codeInspection; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; +import com.intellij.codeInspection.ExplicitArrayFillingInspection; +import com.intellij.codeInspection.LocalInspectionTool; +import org.jetbrains.annotations.NotNull; + +public class ExplicitArrayFillingInspectionTest extends LightQuickFixParameterizedTestCase { + @NotNull + @Override + protected LocalInspectionTool[] configureLocalInspectionTools() { + return new LocalInspectionTool[]{new ExplicitArrayFillingInspection()}; + } + + @Override + protected String getBasePath() { + return "/inspection/explicitArrayFilling"; + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/java18api/Java8ArraySetAllInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/java18api/Java8ArraySetAllInspectionTest.java deleted file mode 100644 index 5727b0164f17..000000000000 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/java18api/Java8ArraySetAllInspectionTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.java18api; - -import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; -import com.intellij.codeInspection.LocalInspectionTool; -import com.intellij.codeInspection.java18api.Java8ArraySetAllInspection; -import org.jetbrains.annotations.NotNull; - -public class Java8ArraySetAllInspectionTest extends LightQuickFixParameterizedTestCase { - @NotNull - @Override - protected LocalInspectionTool[] configureLocalInspectionTools() { - return new LocalInspectionTool[]{new Java8ArraySetAllInspection()}; - } - - @Override - protected String getBasePath() { - return "/inspection/java8ArraysSetAll"; - } -} diff --git a/platform/platform-resources-en/src/messages/InspectionsBundle.properties b/platform/platform-resources-en/src/messages/InspectionsBundle.properties index 4563ff2b3442..fef77f3326d0 100644 --- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties +++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties @@ -835,9 +835,8 @@ inspection.replace.with.bulk.message=Iteration can be replaced with bulk ''{0}'' inspection.replace.with.bulk.fix.name=Replace iteration with bulk ''{0}'' call inspection.replace.with.bulk.fix.family.name=Replace with bulk method call inspection.replace.with.bulk.wrap.arrays=Use Arrays.asList() to wrap arrays - -inspection.replace.loop.with.arrays.setall.message=Loop can be replaced with Arrays.setAll -inspection.replace.loop.with.arrays.setall.fix.family.name=Replace loop with Arrays.setAll +explicit.array.filling.inspection.fix.text=Replace loop with ''Arrays.{0}'' method call +explicit.array.filling.inspection.description=Can be replaced with single ''Arrays.{0}'' method call inspection.simplify.collector.message=Can be simplified using ''{0}'' collector inspection.simplify.collector.fix.family.name=Simplify cascaded collector