From 9df0df10bf2b1cd0613967ab85fbdcf02b94ef0e Mon Sep 17 00:00:00 2001 From: Rick Giles Date: Fri, 9 May 2003 13:17:35 +0000 Subject: [PATCH] corrected to handle octal and hex literals --- .../checkstyle/checks/MagicNumberCheck.java | 79 +++++++++++++------ .../tools/checkstyle/InputMagicNumber.java | 16 ++++ .../checks/MagicNumberCheckTest.java | 33 +++++++- 3 files changed, 103 insertions(+), 25 deletions(-) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MagicNumberCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MagicNumberCheck.java index b0d44a385..c078c0dd3 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MagicNumberCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/MagicNumberCheck.java @@ -77,30 +77,61 @@ public class MagicNumberCheck extends Check */ private boolean inIgnoreList(DetailAST aAST) { - float value = 0; //value of aAST - switch (aAST.getType()) { - case TokenTypes.NUM_DOUBLE : - value = (float) Double.parseDouble(aAST.getText()); - break; - case TokenTypes.NUM_FLOAT : - value = Float.parseFloat(aAST.getText()); - break; - case TokenTypes.NUM_INT : - value = Integer.parseInt(aAST.getText()); - break; - case TokenTypes.NUM_LONG : - // Long.parseLong requires that the text ends with neither 'L' - // nor 'l'. - String text = aAST.getText(); - if ((text.endsWith("L")) || (text.endsWith("l"))) { - text = text.substring(0, text.length() - 1); - } - value = Long.parseLong(text); - break; - default : - break; - } - return (Arrays.binarySearch(mIgnoreNumbers, value) >= 0); + final float value = parseFloat(aAST.getText(), aAST.getType()); + return (Arrays.binarySearch(mIgnoreNumbers, value) >= 0); + } + + /** + * Returns the value represented by the specified string of the specified + * type. Returns 0 for types other than float, double, int, and long. + * @param aText the string to be parsed. + * @param aType the token type of the text. Should be a constant of + * {@link com.puppycrawl.tools.checkstyle.api.TokenTypes}. + * @return the float value represented by the string argument. + */ + private float parseFloat(String aText, int aType) + { + if (aType == TokenTypes.NUM_FLOAT) { + return Float.parseFloat(aText); + } + if (aType == TokenTypes.NUM_DOUBLE) { + return (float) Double.parseDouble(aText); + } + else { + int radix = 10; + if (aText.startsWith("0x") || aText.startsWith("0X")) { + radix = 16; + aText = aText.substring(2); + } + else if (aText.charAt(0) == '0') { + radix = 8; + aText = aText.substring(1); + } + if (aType == TokenTypes.NUM_INT) { + if (aText.length() > 0) { + return (float) Integer.parseInt(aText, radix); + } + else { + return 0; + } + } + else if (aType == TokenTypes.NUM_LONG) { + // Long.parseLong requires that the text ends with neither 'L' + // nor 'l'. + if ((aText.endsWith("L")) || (aText.endsWith("l"))) { + aText = aText.substring(0, aText.length() - 1); + } + if (aText.length() > 0) { + return (float) Long.parseLong(aText, radix); + } + else { + return 0; + } + } + else { + return 0; + } + } } /** diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputMagicNumber.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputMagicNumber.java index 493df3687..0ce64834f 100644 --- a/src/testinputs/com/puppycrawl/tools/checkstyle/InputMagicNumber.java +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputMagicNumber.java @@ -54,5 +54,21 @@ public class InputMagicNumber { if (int_magic1 < 3) { int_magic1 = int_magic1 + 3; } + + //octal + int octalVar0 = 00; + int octalVar8 = 010; + int octalVar9 = 011; + + long longOctalVar8 = 010L; + long longOctalVar9 = 011l; + + //hex + int hexVar0 = 0x0; + int hexVar16 = 0x10; + int hexVar17 = 0X011; + long longHexVar0 = 0x0L; + long longHexVar16 = 0x10L; + long longHexVar17 = 0X11l; } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/MagicNumberCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/MagicNumberCheckTest.java index 22d8b4445..01f952c57 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/MagicNumberCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/MagicNumberCheckTest.java @@ -24,6 +24,14 @@ public class MagicNumberCheckTest "50:37: '3' is a magic number.", "54:26: '3' is a magic number.", "55:39: '3' is a magic number.", + "60:25: '010' is a magic number.", + "61:25: '011' is a magic number.", + "63:30: '010L' is a magic number.", + "64:30: '011l' is a magic number.", + "68:24: '0x10' is a magic number.", + "69:24: '0X011' is a magic number.", + "71:29: '0x10L' is a magic number.", + "72:29: '0X11l' is a magic number.", }; verify(checkConfig, getPath("InputMagicNumber.java"), expected); } @@ -33,7 +41,7 @@ public class MagicNumberCheckTest { final DefaultConfiguration checkConfig = createCheckConfig(MagicNumberCheck.class); - checkConfig.addAttribute("ignoreNumbers", "0, 1, 3.0"); + checkConfig.addAttribute("ignoreNumbers", "0, 1, 3.0, 8, 16"); final String[] expected = { "22:25: '2' is a magic number.", "28:35: '2' is a magic number.", @@ -46,6 +54,10 @@ public class MagicNumberCheckTest "43:31: '4' is a magic number.", "48:26: '1.5' is a magic number.", "50:29: '5' is a magic number.", + "61:25: '011' is a magic number.", + "64:30: '011l' is a magic number.", + "69:24: '0X011' is a magic number.", + "72:29: '0X11l' is a magic number.", }; verify(checkConfig, getPath("InputMagicNumber.java"), expected); } @@ -87,6 +99,17 @@ public class MagicNumberCheckTest "50:37: '3' is a magic number.", "54:26: '3' is a magic number.", "55:39: '3' is a magic number.", + "59:25: '00' is a magic number.", + "60:25: '010' is a magic number.", + "61:25: '011' is a magic number.", + "63:30: '010L' is a magic number.", + "64:30: '011l' is a magic number.", + "67:23: '0x0' is a magic number.", + "68:24: '0x10' is a magic number.", + "69:24: '0X011' is a magic number.", + "70:28: '0x0L' is a magic number.", + "71:29: '0x10L' is a magic number.", + "72:29: '0X11l' is a magic number.", }; verify(checkConfig, getPath("InputMagicNumber.java"), expected); } @@ -108,6 +131,14 @@ public class MagicNumberCheckTest "50:37: '3' is a magic number.", "54:26: '3' is a magic number.", "55:39: '3' is a magic number.", + "60:25: '010' is a magic number.", + "61:25: '011' is a magic number.", + "63:30: '010L' is a magic number.", + "64:30: '011l' is a magic number.", + "68:24: '0x10' is a magic number.", + "69:24: '0X011' is a magic number.", + "71:29: '0x10L' is a magic number.", + "72:29: '0X11l' is a magic number.", }; verify(checkConfig, getPath("InputMagicNumber.java"), expected); }