Use constants for arrays initialized with zero length. #1555
Fixes `ZeroLengthArrayInitialization` inspection violation Description: >Reports on allocations of arrays with known lengths of zero. Since array lengths in Java are non-modifiable, it is almost always possible to share zero-length arrays, rather than repeatedly allocating new zero-length arrays. Such sharing may provide useful optimizations in program runtime or footprint. Note that this inspection does not report zero-length arrays allocated as static final fields, as it is assumed that those arrays are being used to implement array sharing.
This commit is contained in:
parent
e8c6134660
commit
07ffe4625f
|
|
@ -24,6 +24,8 @@ import java.util.Arrays;
|
|||
import java.util.List;
|
||||
import java.util.SortedSet;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.Utils;
|
||||
|
||||
/**
|
||||
|
|
@ -39,7 +41,7 @@ public abstract class AbstractFileSetCheck
|
|||
private MessageDispatcher messageDispatcher;
|
||||
|
||||
/** The file extensions that are accepted by this filter */
|
||||
private String[] fileExtensions = {};
|
||||
private String[] fileExtensions = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
/** Collects the error messages */
|
||||
private final LocalizedMessages messageCollector = new LocalizedMessages();
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ package com.puppycrawl.tools.checkstyle.checks.coding;
|
|||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import antlr.collections.AST;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
|
@ -101,7 +103,7 @@ public class IllegalInstantiationCheck
|
|||
@Override
|
||||
public int[] getAcceptableTokens() {
|
||||
// Return an empty array to not allow user to change configuration.
|
||||
return new int[] {};
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
package com.puppycrawl.tools.checkstyle.checks.coding;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.Utils;
|
||||
import com.puppycrawl.tools.checkstyle.api.Check;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
|
|
@ -74,7 +76,7 @@ public class IllegalTokenCheck
|
|||
|
||||
@Override
|
||||
public int[] getRequiredTokens() {
|
||||
return new int[0];
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ package com.puppycrawl.tools.checkstyle.checks.coding;
|
|||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.Utils;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck;
|
||||
|
|
@ -75,7 +77,7 @@ public class IllegalTokenTextCheck
|
|||
|
||||
@Override
|
||||
public int[] getDefaultTokens() {
|
||||
return new int[0];
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -85,7 +87,7 @@ public class IllegalTokenTextCheck
|
|||
|
||||
@Override
|
||||
public int[] getRequiredTokens() {
|
||||
return new int[0];
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -408,7 +408,7 @@ public class MagicNumberCheck extends Check {
|
|||
*/
|
||||
public void setIgnoreNumbers(double... list) {
|
||||
if (list.length == 0) {
|
||||
ignoreNumbers = new double[0];
|
||||
ignoreNumbers = ArrayUtils.EMPTY_DOUBLE_ARRAY;
|
||||
}
|
||||
else {
|
||||
ignoreNumbers = new double[list.length];
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
package com.puppycrawl.tools.checkstyle.checks.coding;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.Check;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
|
@ -58,7 +60,7 @@ public class SimplifyBooleanExpressionCheck
|
|||
public int[] getAcceptableTokens() {
|
||||
// Return empty list to prevent user changing tokens in the
|
||||
// configuration.
|
||||
return new int[] {};
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
package com.puppycrawl.tools.checkstyle.checks.coding;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.Check;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
|
|
@ -186,7 +188,7 @@ public class UnnecessaryParenthesesCheck extends Check {
|
|||
@Override
|
||||
public int[] getRequiredTokens() {
|
||||
// Check can work with any of acceptable tokens
|
||||
return new int[] {};
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -260,8 +260,7 @@ public class DescendantTokenCheckTest extends BaseCheckTestSupport {
|
|||
checkConfig.addAttribute("maximumNumber", "1");
|
||||
checkConfig.addAttribute("maximumMessage", "What are you doing?");
|
||||
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig,
|
||||
getPath("coding" + File.separator + "InputReturnFromFinallyCheck.java"),
|
||||
|
|
|
|||
|
|
@ -43,8 +43,7 @@ public class SuppressWarningsHolderTest extends BaseCheckTestSupport {
|
|||
public void testOnComplexAnnotations() throws Exception {
|
||||
Configuration checkConfig = createCheckConfig(SuppressWarningsHolder.class);
|
||||
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig, getPath("InputSuppressWarningsHolder.java"), expected);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import static com.puppycrawl.tools.checkstyle.checks.annotation.SuppressWarnings
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
|
||||
|
|
@ -788,9 +789,7 @@ public class SuppressWarningsCheckTest extends BaseCheckTestSupport {
|
|||
public void testUncheckedInConstant() throws Exception {
|
||||
DefaultConfiguration checkConfig = createCheckConfig(SuppressWarningsCheck.class);
|
||||
|
||||
String[] expected = {
|
||||
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig, getPath("annotation" + File.separator
|
||||
+ "SuppressWarningsConstants.java"), expected);
|
||||
|
|
@ -800,8 +799,7 @@ public class SuppressWarningsCheckTest extends BaseCheckTestSupport {
|
|||
public void testValuePairAnnotation() throws Exception {
|
||||
DefaultConfiguration checkConfig = createCheckConfig(SuppressWarningsCheck.class);
|
||||
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig, getPath("annotation" + File.separator
|
||||
+ "SuppressWarningsValuePair.java"), expected);
|
||||
|
|
|
|||
|
|
@ -256,8 +256,7 @@ public class LeftCurlyCheckTest extends BaseCheckTestSupport {
|
|||
public void testIgnoreEnumsOptionTrue() throws Exception {
|
||||
checkConfig.addAttribute("option", LeftCurlyOption.EOL.toString());
|
||||
checkConfig.addAttribute("ignoreEnums", "true");
|
||||
final String[] expectedWhileTrue = {
|
||||
};
|
||||
final String[] expectedWhileTrue = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
verify(checkConfig, getPath("InputLeftCurlyEnums.java"), expectedWhileTrue);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalThrowsCheck.M
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
@ -104,9 +105,7 @@ public class IllegalThrowsCheckTest extends BaseCheckTestSupport {
|
|||
DefaultConfiguration checkConfig = createCheckConfig(IllegalThrowsCheck.class);
|
||||
checkConfig.addAttribute("ignoreOverriddenMethods", "true");
|
||||
|
||||
String[] expected = {
|
||||
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig, getPath("coding" + File.separator
|
||||
+ "InputIllegalThrowsCheckIgnoreOverriddenMethods.java"), expected);
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import static com.puppycrawl.tools.checkstyle.checks.coding.PackageDeclarationCh
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
@ -57,8 +58,7 @@ public class PackageDeclarationCheckTest extends BaseCheckTestSupport {
|
|||
public void testCorrectFile() throws Exception {
|
||||
DefaultConfiguration checkConfig = createCheckConfig(PackageDeclarationCheck.class);
|
||||
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig, getPath("coding/InputPackageDeclaration.java"), expected);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -494,8 +494,7 @@ public class CustomImportOrderCheckTest extends BaseCheckTestSupport {
|
|||
public void testDefaultConfiguration() throws Exception {
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(CustomImportOrderCheck.class);
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
try {
|
||||
createChecker(checkConfig);
|
||||
|
|
|
|||
|
|
@ -557,9 +557,7 @@ public class JavadocMethodCheckTest extends BaseCheckTestSupport {
|
|||
@Test
|
||||
public void testSkipCertainMethods() throws Exception {
|
||||
checkConfig.addAttribute("ignoreMethodNamesRegex", "^foo.*$");
|
||||
String[] expected = {
|
||||
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
verify(checkConfig, getPath("javadoc/InputJavadocMethodIgnoreNameRegex.java"), expected);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,10 +23,10 @@ import static com.puppycrawl.tools.checkstyle.checks.metrics.BooleanExpressionCo
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import antlr.CommonHiddenStreamToken;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
|
|
@ -55,8 +55,7 @@ public class BooleanExpressionComplexityCheckTest extends BaseCheckTestSupport {
|
|||
checkConfig.addAttribute("max", "5");
|
||||
checkConfig.addAttribute("tokens", "BXOR,LAND,LOR");
|
||||
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig, getPath("metrics" + File.separator + "BooleanExpressionComplexityCheckTestInput.java"), expected);
|
||||
}
|
||||
|
|
@ -66,8 +65,7 @@ public class BooleanExpressionComplexityCheckTest extends BaseCheckTestSupport {
|
|||
DefaultConfiguration checkConfig =
|
||||
createCheckConfig(BooleanExpressionComplexityCheck.class);
|
||||
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig, getPath("metrics" + File.separator + "InputBooleanExpressionComplexityNPE.java"), expected);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,10 +24,10 @@ import static org.junit.Assert.fail;
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import antlr.CommonHiddenStreamToken;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
|
|
@ -57,8 +57,7 @@ public class ClassDataAbstractionCouplingCheckTest extends BaseCheckTestSupport
|
|||
public void testDefaultConfiguration() throws Exception {
|
||||
DefaultConfiguration checkConfig =
|
||||
createCheckConfig(ClassDataAbstractionCouplingCheck.class);
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
try {
|
||||
createChecker(checkConfig);
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import static org.junit.Assert.fail;
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
@ -54,8 +55,7 @@ public class ClassFanOutComplexityCheckTest extends BaseCheckTestSupport {
|
|||
|
||||
checkConfig.addAttribute("max", "0");
|
||||
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
verify(checkConfig, getPath("Input15Extensions.java"), expected);
|
||||
}
|
||||
|
|
@ -64,8 +64,7 @@ public class ClassFanOutComplexityCheckTest extends BaseCheckTestSupport {
|
|||
public void testDefaultConfiguration() throws Exception {
|
||||
DefaultConfiguration checkConfig =
|
||||
createCheckConfig(ClassFanOutComplexityCheck.class);
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
try {
|
||||
createChecker(checkConfig);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import static org.junit.Assert.fail;
|
|||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
|
@ -71,8 +72,7 @@ public class JavaNCSSCheckTest extends BaseCheckTestSupport {
|
|||
@Test
|
||||
public void testDefaultConfiguration() throws Exception {
|
||||
DefaultConfiguration checkConfig = createCheckConfig(JavaNCSSCheck.class);
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
try {
|
||||
createChecker(checkConfig);
|
||||
|
|
|
|||
|
|
@ -22,11 +22,11 @@ package com.puppycrawl.tools.checkstyle.checks.metrics;
|
|||
import static com.puppycrawl.tools.checkstyle.checks.metrics.NPathComplexityCheck.MSG_KEY;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import antlr.CommonHiddenStreamToken;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
|
|
@ -75,8 +75,7 @@ public class NPathComplexityCheckTest extends BaseCheckTestSupport {
|
|||
public void testDefaultConfiguration() throws Exception {
|
||||
DefaultConfiguration checkConfig =
|
||||
createCheckConfig(NPathComplexityCheck.class);
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
try {
|
||||
createChecker(checkConfig);
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ public class ModifierOrderCheckTest
|
|||
ModifierOrderCheck modifierOrderCheckObj = new ModifierOrderCheck();
|
||||
int[] actual = modifierOrderCheckObj.getDefaultTokens();
|
||||
int[] expected = {TokenTypes.MODIFIERS};
|
||||
int[] unexpectedEmptyArray = {};
|
||||
int[] unexpectedEmptyArray = ArrayUtils.EMPTY_INT_ARRAY;
|
||||
int[] unexpectedArray = {
|
||||
TokenTypes.MODIFIERS,
|
||||
TokenTypes.OBJBLOCK,
|
||||
|
|
@ -89,7 +89,7 @@ public class ModifierOrderCheckTest
|
|||
ModifierOrderCheck modifierOrderCheckObj = new ModifierOrderCheck();
|
||||
int[] actual = modifierOrderCheckObj.getAcceptableTokens();
|
||||
int[] expected = {TokenTypes.MODIFIERS};
|
||||
int[] unexpectedEmptyArray = {};
|
||||
int[] unexpectedEmptyArray = ArrayUtils.EMPTY_INT_ARRAY;
|
||||
int[] unexpectedArray = {
|
||||
TokenTypes.MODIFIERS,
|
||||
TokenTypes.OBJBLOCK,
|
||||
|
|
|
|||
|
|
@ -123,7 +123,7 @@ public class RedundantModifierTest
|
|||
public void testGetRequiredTokens() {
|
||||
RedundantModifierCheck redundantModifierCheckObj = new RedundantModifierCheck();
|
||||
int[] actual = redundantModifierCheckObj.getRequiredTokens();
|
||||
int[] expected = {};
|
||||
int[] expected = ArrayUtils.EMPTY_INT_ARRAY;
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -200,7 +200,7 @@ public class RegexpCheckTest extends BaseCheckTestSupport {
|
|||
createCheckConfig(RegexpCheck.class);
|
||||
checkConfigFalse.addAttribute("format", illegalFalse);
|
||||
checkConfigFalse.addAttribute("illegalPattern", "true");
|
||||
final String[] expectedFalse = {};
|
||||
final String[] expectedFalse = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
verify(checkConfigFalse, getPath("InputSemantic.java"), expectedFalse);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -22,10 +22,10 @@ package com.puppycrawl.tools.checkstyle.checks.sizes;
|
|||
import static com.puppycrawl.tools.checkstyle.checks.sizes.ExecutableStatementCountCheck.MSG_KEY;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import antlr.CommonHiddenStreamToken;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport;
|
||||
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
|
|
@ -146,8 +146,7 @@ public class ExecutableStatementCountCheckTest
|
|||
public void testDefaultConfiguration() throws Exception {
|
||||
DefaultConfiguration checkConfig =
|
||||
createCheckConfig(ExecutableStatementCountCheck.class);
|
||||
String[] expected = {
|
||||
};
|
||||
String[] expected = ArrayUtils.EMPTY_STRING_ARRAY;
|
||||
|
||||
try {
|
||||
createChecker(checkConfig);
|
||||
|
|
|
|||
Loading…
Reference in New Issue