#ref is not used #loc
suspicious.comparator.compare.descriptor.non.reflexive=Comparator does not return 0 for equal elements
-to.array.call.with.zero.length.array.argument.display.name=Call to 'Collection.toArray()' with zero-length array argument
-to.array.call.with.zero.length.array.argument.problem.descriptor=Call to #ref() with zero-length array argument ''{0}'' #loc
-to.array.call.with.zero.length.array.argument.quickfix=Replace argument with correctly sized array
+to.array.call.style.display.name='Collection.toArray()' call style
+to.array.call.style.problem.descriptor.zero=Call to #ref() with empty array argument ''{0}'' #loc
+to.array.call.style.problem.descriptor.presized=Call to #ref() with pre-sized array argument ''{0}'' #loc
+to.array.call.style.quickfix.family.name=Fix size of the array passed to 'toArray' call
+to.array.call.style.quickfix.make.presized=Replace argument with pre-sized array
+to.array.call.style.quickfix.make.zero=Replace argument with empty array
throwable.instance.never.thrown.runtime.exception.problem.descriptor=Runtime exception instance #ref is not thrown #loc
throwable.instance.never.thrown.checked.exception.problem.descriptor=Checked exception instance #ref is not thrown #loc
throwable.instance.never.thrown.error.problem.descriptor=Error instance #ref is not thrown #loc
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ToArrayCallWithZeroLengthArrayArgumentInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ToArrayCallWithZeroLengthArrayArgumentInspectionBase.java
deleted file mode 100644
index f8f04001924a..000000000000
--- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/ToArrayCallWithZeroLengthArrayArgumentInspectionBase.java
+++ /dev/null
@@ -1,100 +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.siyeh.ig.performance;
-
-import com.intellij.psi.*;
-import com.intellij.psi.util.InheritanceUtil;
-import com.siyeh.InspectionGadgetsBundle;
-import com.siyeh.ig.BaseInspection;
-import com.siyeh.ig.BaseInspectionVisitor;
-import com.siyeh.ig.psiutils.CollectionUtils;
-import com.siyeh.ig.psiutils.ConstructionUtils;
-import org.jetbrains.annotations.Nls;
-import org.jetbrains.annotations.NonNls;
-import org.jetbrains.annotations.NotNull;
-
-public class ToArrayCallWithZeroLengthArrayArgumentInspectionBase extends BaseInspection {
- @Override
- @Nls
- @NotNull
- public String getDisplayName() {
- return InspectionGadgetsBundle.message(
- "to.array.call.with.zero.length.array.argument.display.name");
- }
-
- @Override
- @NotNull
- protected String buildErrorString(Object... infos) {
- final PsiExpression argument = (PsiExpression)infos[1];
- return InspectionGadgetsBundle.message(
- "to.array.call.with.zero.length.array.argument.problem.descriptor",
- argument.getText());
- }
-
- @Override
- public BaseInspectionVisitor buildVisitor() {
- return new ToArrayCallWithZeroLengthArrayArgument();
- }
-
- private static class ToArrayCallWithZeroLengthArrayArgument extends BaseInspectionVisitor {
-
- @Override
- public void visitMethodCallExpression(PsiMethodCallExpression expression) {
- super.visitMethodCallExpression(expression);
- final PsiReferenceExpression methodExpression = expression.getMethodExpression();
- @NonNls final String methodName = methodExpression.getReferenceName();
- if (!"toArray".equals(methodName)) {
- return;
- }
- final PsiExpressionList argumentList = expression.getArgumentList();
- final PsiExpression[] arguments = argumentList.getExpressions();
- if (arguments.length != 1) {
- return;
- }
- final PsiExpression argument = arguments[0];
- final PsiType type = argument.getType();
- if (!(type instanceof PsiArrayType)) {
- return;
- }
- if (type.getArrayDimensions() != 1) {
- return;
- }
- if (argument instanceof PsiReferenceExpression) {
- final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)argument;
- final PsiElement element = referenceExpression.resolve();
- if (!(element instanceof PsiField)) {
- return;
- }
- final PsiField field = (PsiField)element;
- if (!CollectionUtils.isConstantEmptyArray(field)) {
- return;
- }
- }
- else if (!ConstructionUtils.isEmptyArrayInitializer(argument)) {
- return;
- }
- final PsiMethod method = expression.resolveMethod();
- if (method == null) {
- return;
- }
- final PsiClass containingClass = method.getContainingClass();
- if (!InheritanceUtil.isInheritor(containingClass, CommonClassNames.JAVA_UTIL_COLLECTION)) {
- return;
- }
- registerMethodCallError(expression, expression, argument);
- }
- }
-}
diff --git a/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml
index ee9598d76068..5c3342634ec2 100644
--- a/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml
+++ b/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml
@@ -2033,8 +2033,8 @@
groupBundle="messages.InspectionsBundle" groupKey="group.names.performance.issues" enabledByDefault="true"
level="INFORMATION" implementationClass="com.siyeh.ig.performance.TailRecursionInspection"/>
- + In older Java versions using pre-sized array was recommended as a reflection + call which is necessary to create an array of proper size was quite slow. + However since late updates of OpenJDK 6 this call was intrinsified making + the performance of the empty array version the same and sometimes even better, comparing + to the pre-sized version. Also passing pre-sized array is dangerous for concurrent or + synchronized collection as data race is possible between the size and toArray + call which may result in extra nulls at the end of the array if the collection was concurrently + shrinked during the operation. +
++ This inspection allows to follow the uniform style: either using an empty array + (which is recommended in modern Java) + or using a pre-sized array (which might be faster in older Java versions or some non-HotSpot based JVMs). +
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/to_array_call_with_zero_length_array_argument/PresizedToZero.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/to_array_call_with_zero_length_array_argument/PresizedToZero.after.java new file mode 100644 index 000000000000..1a00a97c84df --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/to_array_call_with_zero_length_array_argument/PresizedToZero.after.java @@ -0,0 +1,17 @@ +package com.siyeh.igfixes.performance.to_array_call_with_zero_length_array_argument; + +import java.util.List; + +class IntroduceVariable { + + static List