out magic numbers
This commit is contained in:
parent
eaad55fdba
commit
5cf355fad4
|
|
@ -36,6 +36,9 @@ import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
|
|||
public class DefaultLogger
|
||||
implements AuditListener
|
||||
{
|
||||
/** cushion for avoiding StringBuffer.expandCapacity */
|
||||
private static final int BUFFER_CUSHION = 12;
|
||||
|
||||
/** where to write info messages **/
|
||||
private final PrintWriter mInfoWriter;
|
||||
/** close info stream after use */
|
||||
|
|
@ -92,7 +95,8 @@ public class DefaultLogger
|
|||
final String message = aEvt.getMessage();
|
||||
|
||||
// avoid StringBuffer.expandCapacity
|
||||
final int bufLen = fileName.length() + message.length() + 12;
|
||||
final int bufLen = fileName.length() + message.length()
|
||||
+ BUFFER_CUSHION;
|
||||
final StringBuffer sb = new StringBuffer(bufLen);
|
||||
|
||||
sb.append(fileName);
|
||||
|
|
|
|||
|
|
@ -192,6 +192,12 @@ final class PropertyCacheFile
|
|||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||
|
||||
/** mask for last byte */
|
||||
private static final int MASK_0X0F = 0x0F;
|
||||
|
||||
/** bit shift */
|
||||
private static final int SHIFT_4 = 4;
|
||||
|
||||
/**
|
||||
* Hex-encodes a byte array.
|
||||
* @param aByteArray the byte array
|
||||
|
|
@ -202,8 +208,8 @@ final class PropertyCacheFile
|
|||
final StringBuffer buf = new StringBuffer(2 * aByteArray.length);
|
||||
for (int i = 0; i < aByteArray.length; i++) {
|
||||
final int b = aByteArray[i];
|
||||
final int low = b & 0x0F;
|
||||
final int high = (b >> 4) & 0x0F;
|
||||
final int low = b & MASK_0X0F;
|
||||
final int high = (b >> SHIFT_4) & MASK_0X0F;
|
||||
buf.append(sHexChars[high]);
|
||||
buf.append(sHexChars[low]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,13 +99,16 @@ public final class TreeWalker
|
|||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** default distance between tab stops */
|
||||
private static final int DEFAULT_TAB_WIDTH = 8;
|
||||
|
||||
/** maps from token name to checks */
|
||||
private final Map mTokenToChecks = new HashMap();
|
||||
/** all the registered checks */
|
||||
private final Set mAllChecks = new HashSet();
|
||||
/** the distance between tab stops */
|
||||
private int mTabWidth = 8;
|
||||
private int mTabWidth = DEFAULT_TAB_WIDTH;
|
||||
/** cache file **/
|
||||
private PropertyCacheFile mCache = new PropertyCacheFile(null, null);
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,12 @@ import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
|
|||
public class XMLLogger
|
||||
implements AuditListener
|
||||
{
|
||||
/** decimal radix */
|
||||
private static final int BASE_10 = 10;
|
||||
|
||||
/** hex radix */
|
||||
private static final int BASE_16 = 16;
|
||||
|
||||
/** close output stream in auditFinished */
|
||||
private boolean mCloseStream;
|
||||
|
||||
|
|
@ -193,23 +199,19 @@ public class XMLLogger
|
|||
}
|
||||
|
||||
if (aEnt.charAt(1) == '#') {
|
||||
int prefixLength = 2; // "&#"
|
||||
int radix = BASE_10;
|
||||
if (aEnt.charAt(2) == 'x') {
|
||||
try {
|
||||
Integer.parseInt(aEnt.substring(3, aEnt.length() - 1), 16);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
return false;
|
||||
}
|
||||
prefixLength++;
|
||||
radix = BASE_16;
|
||||
}
|
||||
else {
|
||||
try {
|
||||
Integer.parseInt(aEnt.substring(2, aEnt.length() - 1));
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
Integer.parseInt(
|
||||
aEnt.substring(prefixLength, aEnt.length() - 1), radix);
|
||||
return true;
|
||||
}
|
||||
catch (NumberFormatException nfe) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -32,7 +32,9 @@ import java.util.Set;
|
|||
*/
|
||||
public abstract class Check extends AbstractViolationReporter
|
||||
{
|
||||
|
||||
/** default tab width for column reporting */
|
||||
private static final int DEFAULT_TAB_WIDTH = 8;
|
||||
|
||||
/** the current file contents */
|
||||
private FileContents mFileContents = null;
|
||||
|
||||
|
|
@ -42,8 +44,8 @@ public abstract class Check extends AbstractViolationReporter
|
|||
/** the object for collecting messages. */
|
||||
private LocalizedMessages mMessages;
|
||||
|
||||
/** the tab with for column reporting */
|
||||
private int mTabWidth = 8; // meaningful default
|
||||
/** the tab width for column reporting */
|
||||
private int mTabWidth = DEFAULT_TAB_WIDTH; // meaningful default
|
||||
|
||||
/** current class loader */
|
||||
private ClassLoader mLoader =
|
||||
|
|
|
|||
|
|
@ -39,6 +39,9 @@ import java.util.ResourceBundle;
|
|||
public final class LocalizedMessage
|
||||
implements Comparable
|
||||
{
|
||||
/** hash function multiplicand */
|
||||
private static final int HASH_MULT = 29;
|
||||
|
||||
/** the locale to localise messages to **/
|
||||
private static Locale sLocale = Locale.getDefault();
|
||||
|
||||
|
|
@ -111,10 +114,10 @@ public final class LocalizedMessage
|
|||
{
|
||||
int result;
|
||||
result = mLineNo;
|
||||
result = 29 * result + mColNo;
|
||||
result = 29 * result + mKey.hashCode();
|
||||
result = HASH_MULT * result + mColNo;
|
||||
result = HASH_MULT * result + mKey.hashCode();
|
||||
for (int i = 0; i < mArgs.length; i++) {
|
||||
result = 29 * result + mArgs[i].hashCode();
|
||||
result = HASH_MULT * result + mArgs[i].hashCode();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,8 +54,11 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
|||
*/
|
||||
public class FileLengthCheck extends Check
|
||||
{
|
||||
/** default maximum number of lines */
|
||||
private static final int DEFAULT_MAX_LINES = 2000;
|
||||
|
||||
/** the maximum number of lines */
|
||||
private int mMaxFileLength = 2000;
|
||||
private int mMaxFileLength = DEFAULT_MAX_LINES;
|
||||
|
||||
/** @see com.puppycrawl.tools.checkstyle.api.Check */
|
||||
public int[] getDefaultTokens()
|
||||
|
|
|
|||
|
|
@ -65,8 +65,11 @@ import com.puppycrawl.tools.checkstyle.api.Utils;
|
|||
public class LeftCurlyCheck
|
||||
extends AbstractOptionCheck
|
||||
{
|
||||
/** default maximum line length */
|
||||
private static final int DEFAULT_MAX_LINE_LENGTH = 80;
|
||||
|
||||
/** TODO: replace this ugly hack **/
|
||||
private int mMaxLineLength = 80;
|
||||
private int mMaxLineLength = DEFAULT_MAX_LINE_LENGTH;
|
||||
|
||||
/**
|
||||
* Creates a default instance and sets the policy to EOL.
|
||||
|
|
|
|||
|
|
@ -76,8 +76,11 @@ import org.apache.commons.beanutils.ConversionException;
|
|||
*/
|
||||
public class LineLengthCheck extends Check
|
||||
{
|
||||
/** default maximum number of columns in a line */
|
||||
private static final int DEFAULT_MAX_COLUMNS = 80;
|
||||
|
||||
/** the maximum number of columns in a line */
|
||||
private int mMax = 80;
|
||||
private int mMax = DEFAULT_MAX_COLUMNS;
|
||||
|
||||
/** the regexp when long lines are ignored */
|
||||
private RE mIgnorePattern;
|
||||
|
|
|
|||
|
|
@ -40,6 +40,15 @@ import java.util.Arrays;
|
|||
*/
|
||||
public class MagicNumberCheck extends Check
|
||||
{
|
||||
/** octal radix */
|
||||
private static final int BASE_8 = 8;
|
||||
|
||||
/** decimal radix */
|
||||
private static final int BASE_10 = 10;
|
||||
|
||||
/** hex radix */
|
||||
private static final int BASE_16 = 16;
|
||||
|
||||
/** the numbers to ignore in the check, sorted */
|
||||
private float[] mIgnoreNumbers = {-1, 0, 1, 2};
|
||||
|
||||
|
|
@ -96,13 +105,13 @@ public class MagicNumberCheck extends Check
|
|||
result = (float) Double.parseDouble(aText);
|
||||
}
|
||||
else {
|
||||
int radix = 10;
|
||||
int radix = BASE_10;
|
||||
if (aText.startsWith("0x") || aText.startsWith("0X")) {
|
||||
radix = 16;
|
||||
radix = BASE_16;
|
||||
aText = aText.substring(2);
|
||||
}
|
||||
else if (aText.charAt(0) == '0') {
|
||||
radix = 8;
|
||||
radix = BASE_8;
|
||||
aText = aText.substring(1);
|
||||
}
|
||||
if (aType == TokenTypes.NUM_INT) {
|
||||
|
|
|
|||
|
|
@ -54,8 +54,11 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
|||
*/
|
||||
public class MethodLengthCheck extends Check
|
||||
{
|
||||
/** default maximum number of lines */
|
||||
private static final int DEFAULT_MAX_LINES = 150;
|
||||
|
||||
/** the maximum number of lines */
|
||||
private int mMax = 150;
|
||||
private int mMax = DEFAULT_MAX_LINES;
|
||||
|
||||
/** @see com.puppycrawl.tools.checkstyle.api.Check */
|
||||
public int[] getDefaultTokens()
|
||||
|
|
|
|||
|
|
@ -50,8 +50,11 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
|||
public class ParameterNumberCheck
|
||||
extends Check
|
||||
{
|
||||
/** default maximum number of allowed parameters */
|
||||
private static final int DEFAULT_MAX_PARAMETERS = 7;
|
||||
|
||||
/** the maximum number of allowed parameters */
|
||||
private int mMax = 7;
|
||||
private int mMax = DEFAULT_MAX_PARAMETERS;
|
||||
|
||||
/**
|
||||
* Sets the maximum number of allowed parameters
|
||||
|
|
|
|||
Loading…
Reference in New Issue