From 73ec35d35bcf970c588278cb18300dfdf814a704 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Mon, 4 Oct 2010 18:09:30 +1100 Subject: [PATCH] Fine tune the patch. --- .../checks/coding/MagicNumberCheck.java | 108 +++++++++--------- src/xdocs/releasenotes.xml | 6 + 2 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java index 5886d061b..06302cd81 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java @@ -22,9 +22,7 @@ import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.ScopeUtils; import com.puppycrawl.tools.checkstyle.api.TokenTypes; - import com.puppycrawl.tools.checkstyle.checks.CheckUtils; - import java.util.Arrays; /** @@ -84,63 +82,12 @@ public class MagicNumberCheck extends Check }; } - /** - * Determines whether or not the given AST is in a valid hash code method. - * A valid hash code method is considered to be a method of the signature - * {@code public int hashCode()}. - * - * @param aAST the AST from which to search for an enclosing hash code - * method definition - * - * @return {@code true} if {@code aAST} is in the scope of a valid hash - * code method - */ - private boolean isInHashCodeMethod(DetailAST aAST) - { - // if not in a code block, can't be in hashCode() - if (!ScopeUtils.inCodeBlock(aAST)) { - return false; - } - - // find the method definition AST - DetailAST methodDefAST = aAST.getParent(); - while (methodDefAST != null - && methodDefAST.getType() != TokenTypes.METHOD_DEF) - { - methodDefAST = methodDefAST.getParent(); - } - - if (methodDefAST == null) { - return false; - } - - // chech for 'hashCode' name - final DetailAST identAST = - methodDefAST.findFirstToken(TokenTypes.IDENT); - System.out.println(identAST); - if (!"hashCode".equals(identAST.getText())) { - return false; - } - - // check for no arguments - final DetailAST paramAST = - methodDefAST.findFirstToken(TokenTypes.PARAMETERS); - if (paramAST.getChildCount() != 0) { - return false; - } - - // we are in a 'public int hashCode()' method! - return true; - } - @Override public void visitToken(DetailAST aAST) { - if (inIgnoreList(aAST)) { - return; - } - - if (mIgnoreHashCodeMethod && isInHashCodeMethod(aAST)) { + if (inIgnoreList(aAST) + || (mIgnoreHashCodeMethod && isInHashCodeMethod(aAST))) + { return; } @@ -224,6 +171,55 @@ public class MagicNumberCheck extends Check text); } + /** + * Determines whether or not the given AST is in a valid hash code method. + * A valid hash code method is considered to be a method of the signature + * {@code public int hashCode()}. + * + * @param aAST the AST from which to search for an enclosing hash code + * method definition + * + * @return {@code true} if {@code aAST} is in the scope of a valid hash + * code method + */ + private boolean isInHashCodeMethod(DetailAST aAST) + { + // if not in a code block, can't be in hashCode() + if (!ScopeUtils.inCodeBlock(aAST)) { + return false; + } + + // find the method definition AST + DetailAST methodDefAST = aAST.getParent(); + while ((null != methodDefAST) + && (TokenTypes.METHOD_DEF != methodDefAST.getType())) + { + methodDefAST = methodDefAST.getParent(); + } + + if (null == methodDefAST) { + return false; + } + + // Check for 'hashCode' name. + final DetailAST identAST = + methodDefAST.findFirstToken(TokenTypes.IDENT); + if (!"hashCode".equals(identAST.getText())) { + return false; + } + + // Check for no arguments. + final DetailAST paramAST = + methodDefAST.findFirstToken(TokenTypes.PARAMETERS); + if (0 != paramAST.getChildCount()) { + return false; + } + + // we are in a 'public int hashCode()' method! The compiler will ensure + // the method returns an 'int' and is public. + return true; + } + /** * Decides whether the number of an AST is in the ignore list of this * check. diff --git a/src/xdocs/releasenotes.xml b/src/xdocs/releasenotes.xml index 71f597d13..9a72d67f3 100755 --- a/src/xdocs/releasenotes.xml +++ b/src/xdocs/releasenotes.xml @@ -44,6 +44,12 @@ that checks there is only one statement per line. Thanks to Alexander Jesse for patch #1144994, which was the inspiration. +
  • + Enhanced MagicNumber + to support the parameter ignoreHashCodeMethod to ignore + magic numbers in hashCode() methods. Thanks to + Daniel Solano Gómez for patch #3050788. +
  • Bug fixes: