From e28ebec1e0158a27217ea2fe71cf39000157edea Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Fri, 31 Dec 2010 17:50:32 +1100 Subject: [PATCH] apply patch #3132984 - Ignore magic numbers to annotations in MagicNumberCheck --- .../checks/coding/MagicNumberCheck.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) 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 72598efd3..318d9ee2f 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java @@ -70,6 +70,8 @@ public class MagicNumberCheck extends Check private double[] mIgnoreNumbers = {-1, 0, 1, 2}; /** Whether to ignore magic numbers in a hash code method. */ private boolean mIgnoreHashCodeMethod; + /** Whether to ignore magic numbers in annotation. */ + private boolean mIgnoreAnnotation = true; @Override public int[] getDefaultTokens() @@ -85,6 +87,10 @@ public class MagicNumberCheck extends Check @Override public void visitToken(DetailAST aAST) { + if (mIgnoreAnnotation && isInAnnotation(aAST)) { + return; + } + if (inIgnoreList(aAST) || (mIgnoreHashCodeMethod && isInHashCodeMethod(aAST))) { @@ -263,4 +269,38 @@ public class MagicNumberCheck extends Check { mIgnoreHashCodeMethod = aIgnoreHashCodeMethod; } + + /** + * Set whether to ignore Annotations. + * @param aIgnoreAnnotation decide whether to ignore annotations + */ + public void setIgnoreAnnotation(boolean aIgnoreAnnotation) + { + mIgnoreAnnotation = aIgnoreAnnotation; + } + + /** + * Determines if the column displays a token type of annotation or + * annotation member + * + * @param aAST the AST from which to search for annotations + * + * @return {@code true} if the token type for this node is a annotation + */ + private boolean isInAnnotation(DetailAST aAST) + { + if (aAST.getParent() == null || aAST.getParent().getParent() == null) { + return false; + } + + if (TokenTypes.ANNOTATION == aAST.getParent().getParent().getType() + || TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR == aAST.getParent() + .getParent().getType()) + { + return true; + } + else { + return false; + } + } }